Turn a switch off after a defined set of time (simple switch automation)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

This is my first blueprint(!) and, having tested it, I created a blueprint of the code on this post: Turn light off after X minutes, no matter how it was turned on

blueprint:
  name: Turn off Switch after Time has elapsed
  description: Given a switch entity, watch for it turning on, and then turn it off after a certain period of time.
  domain: automation
  input:
    switch:
      name: Switch
      description: Select the switch to monitor and control
      selector:
        entity:
          domain: switch
    time:
      name: Duration
      description: The duration to leave the switch on for
      selector:
        duration:

trigger:
  - platform: state
    entity_id: !input switch
    to: 'on'
    for: !input time
condition: []
action:
  - service: switch.turn_off
    data: {}
    entity_id: !input switch
mode: single

Source: Turn off Switch after Time has elapsed.yaml · GitHub

If you have any improvements, please let me know, either in this thread, or as a comment on the github gist :slight_smile:

All credit to john2014 for the initial post and sheminasalam for fixing their issues!

Edits:
2022-10-07 Updated link to new gist due to the wrong selector being used (time instead of Duration). Added link to the gist.

The same, but this time for Lights.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: Turn off Light after Time has elapsed
  description: Given a light entity, watch for it turning on, and then turn it off after a certain period of time.
  domain: automation
  input:
    light:
      name: Light
      description: Select the light to monitor and control
      selector:
        entity:
          domain: light
    time:
      name: Duration
      description: The duration to leave the light on for
      selector:
        duration:

trigger:
  - platform: state
    entity_id: !input light
    to: 'on'
    for: !input time
condition: []
action:
  - service: light.turn_off
    data: {}
    entity_id: !input light
mode: single

Source: Turn Off Light After Time.yaml · GitHub

Edits:
2022-10-07 Updated link to new gist due to the wrong selector being used (time instead of Duration). Added link to the gist.

domain can accept a list of domains. If you specify both light and switch, a single blueprint will be able to handle both. Just remember to change light.turn_off to homeassistant.turn_off (because it’s able to turn off lights and switches).

2 Likes

Thanks, I’ll get that sorted out later!

thanks for this. can i ask what happens if i turn off in between the duration period and turn on again? does the timer reset?

for example i have a switch for a duration of 1hr. say i turn it off at 30th min and turn on again at 45th min. does the 1hr clock reset?

So this was super helpful. I was wondering if this can be modified to include multiple lights/switches. My use case is that I have 7 different bath fans. I only want them to run for 15 minutes after they get turned on. My family tends to leave them running indefinitely. I could duplicate the automation 7 times with each entity, but I was wanting to create one automation and just list all 7 devices.