Automation help for switch state

Hi,

i have absolute no idea where to start, but this is what i need:

If a Switch was turned on 6 hours ago and the state was NOT off during the same (every other state needs to be ignored, even unavaiable because its a battery zigbee device) it should be automatically tuned off. This should avoid to keep water running in my garden for longer then 6 hours, which happens from time2time :smiley:

You can do something like this.

Create a helper timer. The use the below automation. This will start the timer when switched on, cancel it if switched off and turn off the water switch when 6 hours has elapsed after switch on. You may need to tweak a bit but should do what you want.

alias: Cancel Watering After 6 Hours
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.watering
    id: SwitchOn
    to: "on"
  - platform: state
    entity_id:
      - switch.watering
    to: "off"
    id: SwitchOff
  - platform: event
    event_type: timer.finished
    id: TimerFinish
condition: []
action:
  - if:
      - condition: trigger
        id: SwitchOn
    then:
      - service: timer.start
        data:
          duration: "06:00:00"
        target:
          entity_id: timer.water_timer
  - if:
      - condition: trigger
        id: SwitchOff
    then:
      - service: timer.cancel
        data: {}
        target:
          entity_id: timer.water_timer
  - if:
      - condition: trigger
        id: TimerFinish
    then:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.watering
mode: single
1 Like

Does it still work when I restart HomeAssistant? The main problem is, that a lots of automations getting canceled:(

Due to not being native speaker, I don’t understand the docs:


Timers will be restored to their correct state and time on Home Assistant startup and restarts when configured with the restore option.
However, automations using the timer.finished event will not trigger if the timer expires when Home Assistant is not running.

Another Question:

platform: event
event_type: timer.finished
id: TimerFinish

How can i specify the timer which finished? i dont wanna cancel a swpific switch for a specific timer and not if ANY timer has finished :slight_smile:

So yes, if restore option is used it will continue after HA reboot. However, if it finished when HA is mid reboot it will not trigger the integration.

In the event data will be the timer that fired it. You can use event_data to filter for only the correct timer. Eg.

- platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.water_timer
    id: TimerFinish

Thanks man, seems to work.

update: i added the „from“ state to off, so after restart the unknown doesn’t trigger a timer restart