WTH there is no easy way to turn on a device for X amount of time

Thanks . Used the delay for just 90 seconds however I'm now trying to get to grips with timers :frowning: . I 've set up a automation that starts a timer, but I cannot get an automation running ( or entered at the moment) to run at timer finish.


alias: Turn off Hall lights when the timer finishes
description: "Turn off Hall lights"
triggers:
  - trigger: event
    target:
      entity_id: timer.hall_light_timer
      to: idle
actions:
  - action: light.turn_off
    target:
      entity_id: light.smart_lighting
  - action: light.turn_off
    target:
      entity_id: light.smart_lighting_2


but when I attempt to save I get
"Message malformed: extra keys not allowed @ data['target']"
Can anyone help please as I don't have a clue as to what is wrong.
Thanks in advance

The right way to specify the trigger is immediately above your post.

1 Like

Thanks, the reason i didn't use that method was I had read somewhere that the timer.finished event was somewhat hit and miss , and the to: idle was more reliable ! Using your code, at least it went in :slight_smile:

You know, @dtrott posted another approach recently that uses a Template sensor as a way to turn something off after some amount of time. But, it also handles the "survive a HA restart" w/o using a timer -- and in some ways works better than a timer because you don't have to handle the situation where the timer finishes while HA is shut down. The caveat being that the device could end up on longer than expected.

- binary_sensor:
    - name: "Pump Running"
      unique_id: pump_running
      state: >
        {{ is_state("switch.pump", "on") }}
      delay_on:
        minutes: 3

I use it a lot and it never misses, so I have no clue why that was implied unless that person failed to turn on the option to restore the timer after a reload.

What IS tricky is to respond to state change. That is because you can cancel a timer, which is stop without fire. This isn't possible with a state change. A timer can also show it is running, you can test for it running, etc. Things only timers do easily.

2 Likes

I'm with you on the timers. And if you use yaml/packages it's just two lines to create them. I just thought it was an interesting approach.

Just to be clear (to others), even with restore, if the timer finishes when HA is shut down, it won't be restored on restart. So, if it's critical important[1] that the device gets turned off:

  - id: handle_timer_finished_while_shutdown
    alias: Turn off the important thing on HA restart
    triggers:
      - trigger: homeassistant
        event: start
    conditions:
      - condition: state
        entity_id: switch.important_device
        state: "on"
      - condition: state
        entity_id: timer.important_device_turn_off
        state: idle
    actions:
      - action: switch.turn_off
        target:
          entity_id: switch.important_device

[1] if it's critical maybe don't depend completely on HA...

1 Like