Checking device state after restart (power cut etc) and tuning in/off if past rule time?

I have a smart switch that turns a pump on and off using rules at various times (I.e on at 8am, off at 4pm).

When we have a power cut or have turned the switch on/off for whatever reason, I’d like for openHAB to check item state and turn on or off when the switch powers up depending where it should be in the rules. Would rather not wait for the next cron entry to get back into the correct state.
I’ve read heaps re rules etc but am missing how to do this, or if it’s possible.


I can’t give you any specific examples of how to accomplish this, as you haven’t given much information to go on. Here is what I am doing for most of my devices:

I am using a number of Sonoff devices with the Espurna firmware. Every 5 minutes they publish a heartbeat, which includes the relay state, to MQTT. The Item I have linked to the relay channel receives an update when this happens, so you can use the Item example_item received update trigger for your rule to catch that and verify that it is set correctly. I am also using { autoupdate="false" } in my Items file to prevent commands to the Item from changing its state. That means that only updates from the device will change the Item state, so if the command is not received the Item in openHAB does not change.

Hopefully that helps. If you can provide specifics I might be able to provide a more specific answer.

Two tasks are needed I guess:

  1. Ensure that the OpenHAB item reflects the actual power status, i.e. when the switch is physically turned off / unplugged and plugged back in, OpenHAB needs to know the new status. With Tasmota this is a bit tricky, but doable.

  2. You need a way to keep track of the “desired state” of that item indepently of the actual state. When they differ, apply the desired state to the actual item. This can be checked by a cron every minute perhaps. You can achieve this by having a dummy item, e.g. Pump_State (for the desired state) and a real item Pump_Power (for the actual state). Whenever you change Pump_Power (from your schedule), also change the Pump_State. In fact, you could just change Pump_State, and write another rule to make Pump_Power follow the state of Pump_State.

Note this can be done using groups so the rules will apply generically to others with the same group.

This way, when someone turns the pump on, outside your scheduled time, your regular cron check will turn it back off :smiley:

I hope this makes sense

Another way to do this would be to set up a “watchdog” timer when the pump is supposed to be on, and for every minute, it would check the power status and ensure it’s turned on. When the pump is scheduled to go off, you can then cancel this watchdog timer. This way when someone turns on the pump after it’s scheduled to go off, it won’t get turned off :). This is simpler than having a separate “desired state” item above.

1 Like