OpenHAB auto off countdown timer rule + PIR + light sensor

Some time ago I published post how to configure auto off timer for lights. There was concept of switching off after time which I set up in Sitemap. After digging through community forum I found post about:

Design Pattern : Expire Binding based Countdown timer

It required expire binding and instead of setting separate local variable for timer it uses switch Item with expire extension to count down our time.

It is worth to start from the example on the website to have better understanding how it works. Later we may add additional elements such as global Settings option with time, motion detector, additional conditions for light sensor etc.

Basic setup

Basic pre-setup – add expire binding – either in PaperUI or in addons.cfg add expire1:

timer.items

timer.sitemap

and finally rule in timer.rule:

Links to other useful concepts with examples:

Design Pattern: Expire Binding Based Timers

Design Pattern: Motion Sensor Timer

Advanced setup

Here is my setup. I will try to simplify and remove unnecessarily elements. It is important to implement previous basic sample to get good understanding what is going on. My example uses elements from previous post. Could be also useful to get better understanding.

Settings

default.sitemap

My kitchen controls – in the middle timer how much time left, and one below group of all lights.

Kitchen
Kichen lighs group

default.sitemap

In further files I use only the one for kitchen as there are multiple elements like motion detector, switches, timers, light sensor. I do not put things file as any of you can have different type and technology for this sensors while items could be the same.

timer.items

timer.rules

What happens? If light is below 350 lux then once PIR detects move turn on one of the lights sonoffwall5_p1. Turning on any of the lights set countdown timer myCounter_kitchen to the values set in settings to myTimer_kitchen. If any of lights are ON and PIR detects move also reset myCounter_kitchen to myTimer_kitchen value. Due to expire1 binding and setting counter counts down every minute.

You may also like...

2 Responses

  1. Simon Thorsted says:

    Advanced setup, have errors the log says ?
    “no viable alternative at input ‘&'”

    can you please change or just make it workj ? i love the ider ,but i can`t code myself !

    • gg says:

      Sorry for that. Ia have pain to find out good code snippet parser for wordpress. It replace & and < > with some other tags.
      Please let me know which exact code does not work, I will send you over email.


      // Rule to manage countdown
      rule "Countdown tick"
      when
      Item myCounter received command
      then
      var cmmd = (receivedCommand as Number).intValue // integers only
      var count = 0
      if (myCounter.state != NULL) { // avoid 1st time run error
      count = (myCounter.state as Number).intValue
      }
      if (cmmd == -1 && count > 0) { // decrement counter, do not go below zero
      if (count == 1) {
      // do actions for counter expiry, as we about to zero it now
      testLamp.sendCommand(OFF)
      }
      myCounter.postUpdate(count - 1)
      } else if (cmmd >= count || cmmd < -1) { // new or refreshed target
      if (cmmd < -1) { // force override
      cmmd = 0 - cmmd // make it positive
      }
      myCounter.postUpdate(cmmd) // nb we still update even if equal value - resets expire binding
      // do startup/continue actions
      if (testLamp.state != ON) {
      testLamp.sendCommand(ON)
      }
      } else if (cmmd == 0) { // cancel countdown
      myCounter.postUpdate(0)
      // do optional cancel actions
      testLamp.sendCommand(OFF)
      }
      end
      // rules just for test simulation buttons

Leave a Reply

Your email address will not be published. Required fields are marked *