Presence rule with reed switch state & time

Hi All

Just looking for some pointers. I have a garage door that I want to ensure im reminder to shut if I leave it open.

Ive got a rule working fine that checks for my Mobile presense and alerts me if its open and Im not home but Im looking for something when I’m actually home, and go to bed.

Would some kind of ‘House Arm’ system work to check the state of doors, lights etc before I go to bed be a better way of doing this?

This rule works OK when I’m not home


rule "Garage Door Alert with Presence"
when
        Item Garage_status changed
then
        if (Kris_Mobile.state == OFF && Jodie_Mobile.state = OFF && Garage_status.state == OPEN) {
        logInfo("Garage Status : " + Garage_status.state)
         sendBroadcastNotification("Garage Door is Open")
}
end


How will openHAB know that you go to bed?

Hi Kris,

at the Moment the rule is only fired if the “Garage_status” changes. this rule won’t “remind” you as it is event-Bound to your Garage-item.

There are plenty of possibilities! :wink: You can concentrate on either time-based rules or timer-based rules.

as for the first: you can Always have a rule be fired on let’s say 21PM (if that’s your bed time?)
as for the second: if you want to be reminded 20mins after the Garage was opened, you could have an expire-item, which in return can trigger a rule to remind you it wasn’t closed.

  1. time-based rules
…
when
	Time cron "0 0 21 1/1 * ? *"
...
  1. expire-based rules
  • create a item (e.g. Garage_Notification) and let this one expire after 20 mins:
    Switch Garage_Reminder "Garage Reminder" { expire="20m,command=OFF" }
  • now you can have a rule When Garage_Reminder changed to OFF

There are more than those two ways to handle the reminder, depending on your exact use case(s), but feel free to try and ask again…

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.