Automation not triggering when device has been on for x hours.

I am trying to make an automation that triggers when a reptile light is on for x hours. Depending on the season (autumn-12 hours, summer-14 hours, winter-10 hours, spring-10 hours). I created four separate automation that should trigger at those intervals and check the season… if the season matches, it turns off the reptile light. I cannot get this automation to run reliably for some reason. See a sample automation below. Any suggestions?

alias: Auto Reptile Lamp (Autumn)
description: ""
triggers:
  - type: turned_on
    device_id: 370a6a645b4d32ab53451dd3328aa2f3
    entity_id: bd56cc0f938eed87d94f7c2c6d9aff3e
    domain: switch
    trigger: device
    for:
      hours: 12
      minutes: 0
      seconds: 0
conditions:
  - condition: state
    entity_id: sensor.season
    state: autumn
actions:
  - type: turn_off
    device_id: 370a6a645b4d32ab53451dd3328aa2f3
    entity_id: bd56cc0f938eed87d94f7c2c6d9aff3e
    domain: switch
mode: single

Hello Profile - adaughe2 - Home Assistant Community,

So while it is possible to wait long periods of time in a wait or delay, it is generally not recommended. This is because HA restarts and many other possible things can happen which interrupt this 'wait’ and then it hangs as you describe.

The suggested method is have a trigger or an automation for turning it on, and another trigger or another automation for turning it off.

Your trigger happens, then waits 12 hours, so id HA reboots or there is some kind of interruption, the trigger resets.

You may want to look at a time pattern for these, they work differently…

Use entity triggers and actions instead of devices. They’re easier to read and debug, clearer, and have fewer caveats.

triggers:
  - trigger: state
    entity_id: switch.YOUR_SWITCH_ID
    to: 'on'
    for:
      hours: 12

and

actions:
  - action: switch.turn_off
    target:
      entity_id: switch.YOUR_SWITCH_ID

Have a look at the history graph for that switch. Has it been consistently on for 12 hours, or does it go unavailable occasionally?

Lots of alternatives, as SG suggests. My solution would be to set a datetime helper to the time you want to turn it off, covering all seasons:

alias: Set reptile lamp off time
triggers:
  - trigger: state
    entity_id: switch.YOUR_SWITCH_ID
    to: 'on'
actions:
  - action: input_datetime.set_datetime
    target:
      entity_id: input_datetime.reptile_lamp_off
    data:
      timestamp: >
        {{ now().timestamp() + (86400 *
           {'autumn': 12,
            'summer': 14,
            'winter': 10,
            'spring': 10}.get(states('sensor.season'),10)) }}

and then (including a trigger/condition to cover the case where you restarted HA at the same time as the lamp should go off):

alias: Reptile lamp off
triggers:
  - trigger: time
    at: input_datetime.reptile_lamp_off
  - trigger: homeassistant
    event: start
conditions:
  - condition: time
    after: input_datetime.reptile_lamp_off
actions:
  - action: switch.turn_off
    target:
      entity_id: switch.YOUR_SWITCH_ID

Testing that template:

I came to the conclusion that the “for” parameter doesn’t always trigger an automation. I used your suggested code and made us of a helper. Looks like it works as intended. Much appreciated!