Set maximum state time for a switch - possible? ideas?

Use case:
I have created an irrigation schedule using AppDaemon (still on V3…;(
If HA/AD/Irrigation script restart while the switch is on, the irrigation switch stays on, usually till next day (can be over 10h…)
It happened yesterday again. My wife threats to…

So, what’s the best solution for such a case?
I thought of having a switch property of maximum time in a state - so once defined it will change the state after the time passes.
Is it possible? What will be the best way to implement it?

Another idea was to access the switch’s history on every irrigation AD script loading and check if ‘max’ time has passed, but I don’t know how to access the switch’s history.

Meanwhile I set the switch to always move to off when script loads - so worst case I lost one day irrigation process, but I don’t like this solution system wise :wink:

Any other idea will be most welcome.

Thanks.

You could do it with an automation.

trigger:
  platform: state
  entity_id: switch.your_irrigation_switch
  to: 'on'
  for:
    hours: 1 # or whatever max time you want
action:
  service: switch.turn_off
  entity_id: switch.your_irrigation_switch

At most this will switch off 1 hour after a restart.

123 Tarras wrote a post on restoring timers after a restart. I’m still looking for it…

EDIT: found it

1 Like

Thank you @tom_l for the automation idea and the link to @123 great post!