Automation guidance

Just checking - I’m an automation newbie.
All I want is to turn on a device when a time is met - then turn off after two hours.
does the following yaml work?

alias: ozone generator
description: ""
trigger:
  - platform: time
    at: "12:00:00"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.sonoff_10017274a8_1
  - delay:
      hours: 2
      minutes: 0
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_10017274a8_1
mode: single

It will work, but prolonged waiting is not efficient and can cause reliability issues since they do not survive restarts. A common method to accomplish this type of automation more efficiently and reliably is to have multiple triggers, then use an If/Then or Choose action to determine which service to call based on which trigger fired.

The following is a basic example. There are many other ways to accomplish the same goal using other methods such as schedule helpers, templating, or timer helpers.

alias: ozone generator
description: ""
trigger:
  - id: 'on'
    platform: time
    at: "12:00:00"
  - id: 'off'
    platform: time
    at: "14:00:00"
condition: []
action:
  - if:
      - condition: trigger
        id: 'on'
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.sonoff_10017274a8_1
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.sonoff_10017274a8_1
mode: single

Shorter, making use of a template:

alias: ozone generator
trigger:
  - id: 'on'
    platform: time
    at: "12:00:00"
  - id: 'off'
    platform: time
    at: "14:00:00"
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.sonoff_10017274a8_1

Or you could set up a schedule helper to allow for UI configuration of the times you want, then:

alias: ozone generator
trigger:
  - platform: state
    entity_id: schedule.ozone
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.sonoff_10017274a8_1
2 Likes

Hey thanks for those hints. Drew you mention prolonged waits do not survive restarts? Not sure what you mean by this. Are you saying that a restart of HA will affect my automation timing?
Pat

Not timing but the automation that is set to wait will be terminated and when HA reboots it has no memory of anything from before the restart.

So that’s why you shouldn’t use long delays in automations.
I usually limit myself to a few minutes at the most.
But also depending on how important the automation is.

With the recently added options for choosing how YAML is reloaded, this is less of an issue now than it used to be. However, restarts and reboots essentially negating wait, delay, and for is a common source of frustration for newer users.

As @Hellis81 noted, how much you need to worry about an automation’s reliability really depends directly on how important its outcome is. Since excessive ozone can be harmful to people, pets, plants, and equipment you may want to consider extra protections to guarantee that your generator is turned off in a timely manor.

Drew,
Thanks for your help. the ozone generator is in my new cold plunge construction. The timer is set for midday so that when our normal plunge happens - about 6pm then there should have been dissipation of gas suitable for use. When we open the lid to the plunge it is open for about 10 minutes before use as well.
Pat

Yet another variation that ensures the ozone generator is set to the correct state on startup. It mitigates the possibility where Home Assistant may have been offline when it was scheduled to trigger at 12:00 or 14:00 (thereby failing to set the device in the correct state).

alias: example
trigger:
  - platform: time
    at:
      - '12:00:00'
      - '14:00:00'
    variables:
      cmd: "{{ iif(now().hour == 12, 'on', 'off') }}"
  - platform: homeassistant 
    event: start
    variables:
      cmd: "{{ iif(12 <= now().hour < 14, 'on', 'off') }}"
action:
  - service: 'switch.turn_{{ cmd }}'
    target:
      entity_id: switch.sonoff_10017274a8_1

Taras,
indeed I had exactly that issue. A power cut about 12.15 stopped the automation form resuming. I then tried to put your example in but during run I get error

Error: Template rendered invalid service: switch.turn_

I copied your code - saw that the if command had two i’s (was that typo) and only changed the alias

Pat

You can’t use an automation’s Run command to test it when it references triggers variables.

The Run command only executes an automation’s actions. It doesn’t exercise the automation’s trigger. A trigger variable is defined only if the trigger occurs (and the Run command doesn’t trigger anything).

Reference: Testing your automation

Correct. That’s an Immediate If.

1 Like

OK - getting some understanding now. When I put in your code though I would have expected HA to very quickly recognise that the conditions set of time between 12 and 1400 hours was met and the switch should have been triggered? Or do I have to wait for a restart/refresh ???
Pat

Taras,
have repasted your code and will see what happens tomorrow then. Still learning about code - does your example only run the switch between the hours of 1200 - 1400? Or should the variables cmd line read

platform: homeassistant 
    event: start
    variables:
      cmd: "{{ iif(12 <= now().hour > 14, 'on', 'off') }}"

with a greater than symbol before 14?

Pat

This means the current hour must be greater or equal to 12 and less than 14. The values 12 and 13 meet that requirement, which represent the hours when the device should be on.

12 <= now().hour < 14

This means the current hour must be greater or equal to 12 and greater than 14. Any value greater or equal to 15 meets the requirement but that’s not the hours when the device should be on.

12 <= now().hour > 14

Taras
thanks for that - your’e a champion.
Pat

An automation does nothing until it’s triggered. The example posted above is triggered at two times (12:00 and 14:00) and when Home Assistant restarts.

You could add an additional trigger to make it work how you think it should:

  - platform: event
    event_type: automation_reloaded

Taras,
I wanted to reduce the run time of the ozone generator and so I changed two values to reflect this so that the run time should have been only 30 minutes. But the generator was switched on and then just kept running?

alias: ozone generator
trigger:
  - platform: time
    at:
      - "12:00:00"
      - "12:30:00"
    variables:
      cmd: "{{ iif(now().hour == 12, 'on', 'off') }}"
  - platform: homeassistant
    event: start
    variables:
      cmd: "{{ iif(12 <= now().hour < 12.30, 'on', 'off') }}"
action:
  - service: switch.turn_{{ cmd }}
    target:
      entity_id: switch.sonoff_10017274a8_1

Have I made the right changes?

Now that the ending hour is the same as the starting hour, we have to also check minutes.

alias: ozone generator
trigger:
  - platform: time
    at:
      - "12:00:00"
      - "12:30:00"
  - platform: homeassistant
    event: start
action:
  - service: "switch.turn_{{ iif(now().hour == 12 and now().minute < 30, 'on', 'off') }}"
    target:
      entity_id: switch.sonoff_10017274a8_1

Taras,
aaah thanks for that. worked a treat
Pat

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.