Automation: Is this the best way to trigger something after 1 hour?

I have an Eve Door & Window sensor integrated into HA to notify me when I left the bathroom window open for an hour:

trigger:
  - type: opened
    platform: device
    device_id: 8668f13c77a839cd3120fee49a572f29
    entity_id: c96638c7b97eade9bc412aba9968b435
    domain: binary_sensor
    for:
      hours: 1
      minutes: 0
      seconds: 0

It works as expected. I’m just wondering if this is the best way to do this? I mean, what is the automation doing during that hour? Hopefully not using a lot of CPU.

Thanks.

It is the second best way.

Every trigger you create adds a listener to the event loop. This uses an imperceptible amount of CPU resources.

The only improvement you could make would be to stop using device ids. Why and how to avoid device_ids in automations and scripts

3 Likes

Also be aware that if HA restarts or you reload the automation during that hour the automation actions won’t run after the hour is up.

they will only run after the trigger goes from false to true again (i.e. when the window goes back to closed then is reopened for 1 hour).

I generally avoid long time delays like that for reliability reasons.

3 Likes

I also tend to avoid long running automations. Perhaps using timer would be a better solution? Since timers are (if enabled so) restored after HA restart.

1 Like

Reloading automations only reloads the changed automations so that is less of an issue.

1 Like

If you restart HA, the countdown will be gone.

It’s better if you create a timer and another automation that will do something when timer finishes.

Look at my example: I want to turn off heating after 1h.

# configuration.yaml

timer:
  turn_off_heating_timer:
    name: Turn off heating in 1h
    duration: "01:00:00"
# automations.yaml

- id: '12345'
  alias: 'Furnace: turn off heating after timer'
  description: ''
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.turn_off_heating_timer
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.furnace_heating
  mode: single

To activate the timer (start the 1h countdown) just run the timer.start service with your timer’s entity_id.

So, your first automation should be:

  1. trigger: when the window’s state changes from closed to open
  2. action: start the timer

In the process, you get red of the “device_id” issue others have mentioned here.

And the second automation:

  1. trigger: when timer finishes
  2. action: whatever you’re doing now (notification?)

Don’t forget about the third automation:

  1. trigger: when window’s state changes from open to closed
  2. action: stop the timer

If you don’t like having 3 separate automations, you can combine them into one with choose action.

2 Likes

Hm…that’s strange… before i posted above i tested timer and it resumed countdown after HA restart…
But, as said, “restore” must be checked in timer’s settings

Timers do resume after restart if they are setup to do so.

But, for ‘time’ in an automation does not.

1 Like

Thanks everyone! I’ll work my way through the suggestions :+1:

The only improvement you could make would be to stop using device ids.

Is this something that should (can?) be improved in the GUI to create automations then?

It’s all available in the automation editor. You just haven’t looked past the first option.

1 Like