Presence rule with reed switch state & time

Hi @rlkoshak

ive been struggling with this one for a few days, perhaps you can give me some guidance. Im trying to use Alexa alerts when home (presense = ON) and broadcasts when I’m away.

I have this, but it just fails to send the commands to the amazon echo group, so I can only assume ive got something wrong. Initially I thought it was the ‘!’ so i removed that but the issue remains - it wont send any alerts to the echos

Any suggestions?

rule "Keep track of the last time a door was opened or closed"
when
  Member of gDoorSensors changed
then
  if(previousState == NULL) return;

  val name = triggeringItem.name
  val state = triggeringItem.state

  // Update the time stamp
  postUpdate(name+"_LastUpdate", now.toString)

  // Set the timer if the door is open, cancel if it is closed
  if(state == OPEN) sendCommand(name+"_Timer", "ON")
  else postUpdate(name+"_Timer", "OFF")

  // Set the message
  val msg = new StringBuilder
  msg.append(transform("MAP", "en.map", name) + " was ")
  msg.append(if(state == OPEN) "opened" else "closed")

  var alert = false
  if(vTimeOfDay.state.toString == "NIGHT" || vTimeOfDay.state.toString == "BED" || vTimeOfDay.state.toString == "EVENING") {
    msg.append(" and it is Night")
    alert = true
  }
  if(gPresenceSensors.state == OFF) {
    msg.append(" and no one is Home")
    alert = true
  }
  // Alert if necessary
  if(alert && gPresenceSensors.state == ON) {
    gAllEchoAlerts.sendCommand(msg.toString)
  }
  // Alert if necessary
  if(alert && gPresenceSensors.state == OFF) {
    sendBroadcastNotification(msg.toString)
  }
  // Log the message if we didn't alert
  else {
    logInfo("Door Sensor", msg.toString)
  }
end

Thanks!

Sorry for the late reply. I was away from home and technology for awhile but am back now.

The first thing to do always is add some logging so you can tell what parts of the Rule are executing.

Looking at the logic you have it sending the message to the Echos when alert is true and presence is ON. Thus, this condition will only be true if it is NIGHT, or BED time. Are you testing this at NIGHT or BED time?

Similarly, the broadcast will always run when presence is OFF because presence == OFF is one of the conditions that set alert to true.

1 Like

Dont be sorry! it appears it was more an Echo binding issue than the rule :wink: It works just fine now