Checking delay automation with set_datetime

I noticed that there was some push back against using a delay in an automation. And after hearing the reason that it can break the automation (restart and other events when HA will startup again)
I kinda agree and started tinkering with set_datetime templates.

Now i created an automation for boosting the living room for a hour (in this test its still 20 seconds)
It seems to work and i can spam the button without it breaking. But more experienced eyes might spy a glaring error or small adjustment for better stability.

If you could have a look:

alias: Booster Woonkamer
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.woonkamer_booster_toogle
    id: Woonkamer_Boost_On
    to: 'on'
  - platform: state
    entity_id: input_boolean.woonkamer_booster_toogle
    to: 'off'
    id: Woonkamer_Boost_Off
  - platform: time
    at: input_datetime.woonkamer_booster_time
    id: Delay_Boost_Reached
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Woonkamer_Boost_On
        sequence:
          - service: scene.create
            data:
              scene_id: woonkamer_setpoint
              snapshot_entities: climate.woonkamer
          - service: climate.set_temperature
            data:
              temperature: 21
            target:
              entity_id: climate.woonkamer
          - service: input_datetime.set_datetime
            data:
              time: >-
                {{ (now().strftime('%s') | int + 20) |
                timestamp_custom('%H:%M:%S', False)}}
            target:
              entity_id: input_datetime.woonkamer_booster_time
      - conditions:
          - condition: trigger
            id: Woonkamer_Boost_Off
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.woonkamer_setpoint
            data: {}
      - conditions:
          - condition: time
            after: input_datetime.woonkamer_booster_time
          - condition: state
            entity_id: automation.booster_woonkamer
            state: 'on'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.woonkamer_setpoint
            data: {}
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.woonkamer_booster_toogle
    default: []
mode: restart

Looks good.

The only thing I would do differently would be the template.

I’m quite sure this works:

{{ now() + timedelta(seconds=20) }}

I’m fairly sure this will return a datetime object that the entity will accept.

That was an error on my side. I picked the automation instead of the boolean.
As you said i needed to check if the boolean was on.
So i changed this to:

          - condition: state
            entity_id: input_boolean.woonkamer_booster_toogle
            state: 'on'

As the time trigger would even go when i switched the boost off.

@Hellis81
That one throws up an error.

rror while executing automation automation.booster_woonkamer: Invalid time specified: {'[object Object]': None} for dictionary value @ data['time']
Error while executing automation automation.booster_woonkamer: Invalid time specified: 2022-02-15 21:10:02.585036+01:00 for dictionary value @ data['time']

And when i paste that code into the dev tools it shows the entire date/time string. Maybe if i adjust my helper to be an date/time instead of time.

If you’re interested, here’s the approach I would use. An important element of this version is:

mode: queued

because when the Time Trigger turns off the input_boolean, it triggers itself to perform the balance of the action. The reason for leveraging this ability is to eliminate calling the same service (scene.turn_on) in two different places.

alias: Booster Woonkamer
mode: queued
trigger:
  - platform: state
    entity_id: input_boolean.woonkamer_booster_toogle
  - platform: time
    at: input_datetime.woonkamer_booster_time
condition: []
action:
  - choose:
      - conditions: "{{ trigger.id == 'state' }}"
        sequence:
          - choose:
              - conditions: "{{ trigger.to_state.state == 'on' }}"
                sequence:
                  - service: scene.create
                    data:
                      scene_id: woonkamer_setpoint
                      snapshot_entities: climate.woonkamer
                  - service: climate.set_temperature
                    target:
                      entity_id: climate.woonkamer
                    data:
                      temperature: 21
            default:
              - service: scene.turn_on
                target:
                  entity_id: scene.woonkamer_setpoint
          - service: input_datetime.set_datetime
            target:
              entity_id: input_datetime.woonkamer_booster_time
            data:
              timestamp: "{{ (now() + timedelta(minutes=60 * iif(trigger.to_state.state == 'on', 1, -1))).timestamp() }}"
    default:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.woonkamer_booster_toogle