How do you configure automation for 5 hours from switch or after a reboot HA

so i have issues… my code works if you press button to turn on and off hot water tank… and my automation shuts it off 5 hours to save electricity… but if i reboot Ha during the time its on it wont work it will keep it on…
i tried adding trigger home assistant and then condition of hot water tank switch on for 5 hours
so once hA boots and hot water tank is on for 5 hours it would shut off
but it didnt work…
how can i add that it still work?

here is the code i use for the switch… i did tried just switch and a condition of 5 hours but that didnt work … i tested it by using hot water tank switch on state for 5 seconds but it never turned off… so wasnt able to get conditions to work right…

so i want to be able to press button or when you reboot home assistant… that the automation checks if hot water tank was on 5 hours then turn it off

alias: Turn off  Hot Water Tank after 5 hours
description: ''
trigger:
  - platform: state
    entity_id: switch.mitchs_hot_water_tank
    for:
      hours: 5
      minutes: 0
      seconds: 0
      milliseconds: 0
    to: 'on'
  - platform: homeassistant
    event: start
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.mitchs_hot_water_tank
mode: single

i tried this to work but it wont work i tried the hours to 5 or 0 below didnt help

alias: Turn off  Hot Water Tank after 5 hours
description: ''
trigger:
  - platform: state
    entity_id: switch.mitchs_hot_water_tank
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  - platform: homeassistant
    event: start
condition:
  - condition: device
    type: is_on
    device_id: 9d241cf92747c4bef591d585192ff2de
    entity_id: switch.mitchs_hot_water_tank
    domain: switch
    for:
      hours: 5
      minutes: 0
      seconds: 0
      milliseconds: 0
action:
  - service: switch.turn_off
    target:
      entity_id: switch.mitchs_hot_water_tank
mode: single

Here is an example automation that will be more reliable way of ensuring a set of actions occurs that will with stand HA restarts, etc

  - alias: Automatic Door Lock
    trigger:
      - platform: state
        entity_id: lock.door
        to: 'unlocked'
      - platform: time
        at: input_datetime.door_lock
      - platform: homeassistant
        event: start
      - platform: event
        event_type: automation_reloaded
    action:
      - choose:
          - conditions: "{{ trigger.platform == 'state' }}"
            sequence:
              - service: input_datetime.set_datetime
                entity_id: input_datetime.door_lock
                data:
                  datetime: >
                    {{ (now() + timedelta(minutes = 30)).timestamp() | timestamp_local  }}
          - conditions: 
              - "{{ trigger.platform == 'homeassistant' or trigger.platform == 'event' }}"
              - "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.door_lock')) < 120 }}"
              - "{{ is_state('lock.door', 'unlocked') }}"
            sequence:
              - service: lock.lock
                entity_id: lock.door
          - conditions:
              - "{{ trigger.platform == 'time' }}"
              - "{{ is_state('lock.door', 'unlocked') }}"
            sequence:
              - service: lock.lock
                entity_id: lock.door

just substitute your entities, etc in the above and change the “minutes=30” in the timedelta functron to what you want it to be (5 hours = 300 minutes)

it seems a bit complicated and you will need to first create an appropriately named input_datetime but it’s the most reliable method for ensuring things operate as expected.

@finity

I think this would also work in your automation:

    action:
      - choose:
          - conditions: "{{ trigger.platform == 'state' }}"
            sequence:
              - service: input_datetime.set_datetime
                entity_id: input_datetime.door_lock
                data:
                  datetime: >
                    {{ (now() + timedelta(minutes = 30)).timestamp() | timestamp_local  }}
        default: 
          - condition: template
            value_template: >
              {{ now().timestamp() >= as_timestamp(states('input_datetime.door_lock')) and
                  is_state('lock.door', 'unlocked') }}
          - service: lock.lock
            target:
              entity_id: lock.door

The default consolidates the last two conditions in your original example. I think I got the logic right (please let me know if I got it wrong). When triggered by restart/reload/time, if the current time is greater than the input_datetime (and the door is unlocked), then lock the door.

1 Like

the only thing it looks like it is missing is the “grace period” of 120 seconds.

But you would know better than me since I (mostly) took this from one of your suggestions in the other thread with tinkerer’s edit.

(EDIT: actually it’s his edit and later mine that added the two additional conditions. I’ll need to look at it more closely but I think you’re probably right. Sometimes I like to get a bit more verbose to increase my own readability and since it was a evolving automation I didn’t catch the overlap.)

ah ok ill give them a try and see how it works i appreciate it…

for now what i did was 2 automations one that when HA boots up and condition is hot water tank on shut off 5 hours from then… or the button press but thats if ha doesnt reboot… be nice a better intergration automation not so complicated

but ill check out that other link and stuff