Presence rule with reed switch state & time

Sorry guys, I wasn’t clear.

The rule above is for when I’m not home.

Vincent, re: bed I’m talking about using a Time cron but I think the expire timer will be fine! I guess I want the expire timer to work around the 8pm mark when really I’m not venturing back out there, so a reminder to close it if its open.

Thomas, I think the expire is the ticket!

rule "garage door left opened after 8pm"
when
    Time cron "0 0 20 ? 0 0 0"
then
    if ((Kris_Mobile.state == ON || Jodie_Mobile.state == ON) && Garage_status.state == OPEN) {
        logInfo("Garage Status : " + Garage_status.state)
        sendBroadcastNotification("Garage Door is Open")
    }
end
2 Likes

By the way one of the = in your first rule should be ==

1 Like

Thanks :wink:

Doesnt like that Cron time Vincent…

My fault:
Time cron "0 0 20 ? * * *"

Wouldnt it be best between 8pm and 6am?

As, after 8pm could be 7pm :stuck_out_tongue: the next day…

Unfortunately it doesnt work. No logs, despite the garage being open and my mobile being On.

Bookmark this:
https://www.freeformatter.com/cron-expression-generator-quartz.html

Yep, ive got that. I need to do a time range…

What do you want the rule to do?

I use the following Rule. Note this is made generic for all my doors.

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") {
    msg.append(" and it is night")
    alert = true
  }
  if(vPresent.state == OFF) {
    msg.append(" and no one is home")
    alert = true
  }
  msg.append("!")

  // Alert if necessary
  if(alert){
    aAlert.sendCommand(msg.toString)
  }
  // Log the message if we didn't alert
  else {
    logInfo(logName, msg.toString)
  }
end

rule "Timer expired for a door"
when
  Member of gDoorsTimers received command OFF
then
  val doorName = transform("MAP", "en.map", triggeringItem.name)

  aAlert.sendCommand(doorName + " has been open for over an hour")

  if(vTimeOfDay.state.toString == "NIGHT" || vTimeOfDay.state.toString == "BED") {
        triggeringItem.sendCommand(ON) // reschedule the timer
  }
end

See [Deprecated] Design Pattern: Time Of Day (vTimeOfDay), Generic Presence Detection (naming scheme of Items so I can update and command related Items based on triggeringItem.name), Design Pattern: Separation of Behaviors (centralized alerting), Design Pattern: Human Readable Names in Messages (converting Item names to more human friendly names for use in alerts and logs) and Design Pattern: Expire Binding Based Timers (door open timers). These tow Rules are a great example showing how DPs are not intended to be used in isolation.

All of my doors are a member of gDoorSensors. Any time one of them changes the first Rule triggers.

First I update a DateTime Item to record when the door opened/closed.

If the door opened I start a Timer. If the door closed I cancel the Timer.

Finally, I generate a message to log and potentially alert. I will generate an alert immediately if it is night or bed time or no one is home.

The second Rule triggers when a door Timer expires and generates an alert. If it is night or bed time I reschedule the timer.

The one thing the Rules above don’t show is how to handle OH restarts. All of my door sensors are DIY and will respond with the door’s current state when I send a certain message. So I have a System started Rule that publishes that message which causes the sensors to publish their current states and if those states differ from what was restoreOnStartup the Rules above run.

Remember, Rules are event triggered, not state triggered. When you set up a cron trigger, you are scheduling events based on time. So if you do something like

0 0 20-23,0-6 ? * * * // I don't know if this is correct but the intent is to be between 20:00 and 06:100:

what you are doing is scheduling an event to occur at 20:00, 21:00, 22:00 and so on.

2 Likes

Thank you, ill absorb that over my morning coffee:D

Hi Rich

Took me 6hrs but I got your DPs working for Presence, Time of Day & Human Readable names working :smiley:

I slightly modified your rule, I think you made reference to vPresense , I modified this to gPresenceSensors and changed your alert to a broadcast notification to my mobile.

By posting updates to my Doors whilst away, Im getting the alerts just fine.

Ill do some testing! Thank you :slight_smile:

1 Like

That’s good. The DPs are there to be customized to your needs. I used vPresence because I don’t want the alerts if my presence is flapping because I’m in the backyard or just at the edge of detection. You may not need that and switching to gPresenceSensors is the exact right approach.

Once you get a role like this working and more importantly you understand how it works there is nothing you can’t accomplish in OH. Congratulations and good luck!

1 Like

Thanks Rich! ill do some testing when I’m home tonight :slight_smile: but so far so good…great work on writing your DP’s

Hi Rich how am I able to modify the Evening/Night? Its 10.50PM and its showing evening :wink: The rule notification triggers on NIGHT or BED…

Change the start time that indicates it’s evening/night.

You need to modify the from trigger and then modify the line that calculates the start time for the time periods you want to change.

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