How to calculate the difference in time

  • Platform information:
    • Hardware: Raspberry Pi 3
    • openHAB version: 3

I use the channel last_updated (Date_Time) of a HUE motion sensor.
Now I want to send a command to another Thing after 5 minutes of inactivity of the HUE motion sensor.
So to say: if the difference between NOW and the last update of the motion sensor is greater then 5 minutes I want to switch of a plug!

I can’t figure out how to to write the rule…?

I would create a rule the will run once the motion sensor changes it stays.

If it turns off / detecting no motion anymore, simply start a 5 minutes timer.
Than you do not need to do any math.

If the motion sensor will detect any motion again, you can cancel the timer

You may not need much of a rule for this at all. There can be many ways to go about this so first I’ll present a high level approach.

  1. Configure Expire on the Item linked to the last_updated Channel so that after five minutes it sets the Item back to something we can use to trigger a rule. Note that Expire now has the ability to only respond to changes but you need to be running 3.3 or later.

  2. Create a rule that triggers when that Item changes to the state Expire sets it to do what ever needs to be done when the five minutes is up.

After you think about it, you don’t even need the last_updated Channel. You can use the Channel that represents the motion itself potentially, depending on how that Channel works.

See Design Pattern: Motion Sensor Timer for several ways to implement this sort of thing.