Turn something on for a set time

I have found several threads with a similar task, but all suggested solutions looked unduly complicated. Given some trigger and conditions I want to turn something on for a set time and then off. Sounds obvious and simple to me but I can’t find a simple solution. Is there none?
Just so there are no misunderstandings: I can do the “turn on” bit but not the “and off again half an hour later” part.
Danke

Two ways you can do this. The first is to say “when X has been turned on for Y minutes” and once that trigger is true then execute an action to turn off the light. The second is to trigger when the light comes on and then add a delay to your actions for Y minutes to then turn it off.

Method 1:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - light.armoir
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.armoir

Method 2:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - light.armoir
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.armoir

Turn on switch at 08:00.
Turn off switch at 08:30.

alias: example
trigger:
  - id: 'on'
    platform: time
    at: '08:00:00'
  - id: 'off'
    platform: time
    at: '08:30:00'
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.whatever

If Home Assistant is restarted at any time between 08:00 and 08:30, this technique ensures the switch is turned off at 08:30. Various other techniques may be reset by a restart and fail to turn off the switch.

The best method depends on the requirements of your application.

1 Like

Thank you to both of you. So there really is no “turn on for” but I have to write a sepate turn-on and turn-off, although Taras’ solution cleverly combines them. I appreciate the reliability aspect but I’ll stick to Colorado’s solution nonetheless. That way I can have one turn-off for several ons with different triggers and conditions.
I’m quite surprised a “turn on for” routine is not supplied. this is such a frequent requirement for all kinds of appliances.
Danke.

1 Like

Putting a delay inside an automation has the disadvantage of not surviving a restart of HA.

There’s also the option to use a timer helper. It can be set to survive a restart, and can also be shown in the UI, tested for, restarted, canceled, etc.

Think of the challenge in reverse. If something has been on for 10 minutes, turn it off. That what the State Trigger achieves in CO_4x4’s first example. It triggers after the light’s state changes to on and remains unchanged for at least 10 minutes.

Of course, if Home Assistant is restarted during that 10 minutes, the automation is reset and never turns off the light. The delay statement in the second example suffers the same fate.

That’s why it’s prudent to limit the use of for and delay to short periods of time in order to reduce the odds of being reset by a restart. For longer time periods, there are other techniques that can survive a restart (thereby making the automation more robust and reliable). What I posted is one way but there are others (such as Edwin_D’s suggestion to use a timer entity). Which is best depends on the application.

The following may interest you:

Evolution of an automation. Turn a light on/off at sunset/sunrise - Share your Projects! - Home Assistant Community (home-assistant.io)

FWIW, it’s fairly common for one automation to use multiple triggers to control equipment.

I searched the documentation. Neither “timer helper” nor “timer” and “helper” alone tunrned up anything.
I’m coming from e.g. LaTeX and am used to really good comprehensive handbooks, preferably on paper. When I build something in HA using the visual editor and try to look up the terms I find in the yaml, I come up blank. To me this is quite disconcerting.

Thank you. I just found it was my mistake. I need to allow scripts from algolia for the search to work, so no wonder I found nothing. I hate pages loading third party scripts in the background.