Automation: Automatically turn off lights if left on too long without turning off other lights

So this has probably happened to a lot of people. You turn on the light in one room. You leave the room to do something else and forget to turn off the light in the other room. So I want an automation that keeps track of my lights in my house. I know I could put a motion sensor or something like that but this time I want it to trigger based on how long the lights have been turned on.

There are 10+ lights that I want to monitor. I want them to turn off if left on for more than two hours. I can on top of my head think of a few ways of doing this but none of them are particularly elegant. here’s an example of how i could do this:

- alias: Turn off ligts if on for more than two hours
  trigger:
    platform: state
    entity_id: light.light1, light.light2, light.light3
    from: 'off'
    to: 'on'
    for:
      hours: 2
      minutes: 0
      seconds: 0
  action:
    service: light.turn_off
    data:
      entity_id: light.light1
    service: light.turn_off
    data:
      entity_id: light.light2
    service: light.turn_off
    data:
      entity_id: light.light3

But the problem with the above code is that it would turn off lights that have not been on for more than two hours. I guess I could make one automation for each and ever light but that feels heavy.

is there a way to detect what light that triggered the automation and then only turn off that particular light and leave the rest of them on? At least until they too have been on for too long?

Turn off the light that triggered the automation with a data_template and trigger.to_state.entity_id

Hi

A couple of suggestiuons.

Firstly, you can compress your code a little by reducing the trigger to:

trigger:
    platform: state
    entity_id: light.light1, light.light2, light.light3
    to: 'on'
    for:
      hours: 2

Secondly, an automation is aware of what entity has triggered it!

So, you can use something like:

action:
  service: light.turn_off
  data_template:
    entity_id: trigger.entity_id

This “should” work

I have used it in automatons to notify me of what sensors have triggered motion in my house.

But what happens if you are in a room for more than two hours?

I know. That is not perfect but I could have a different trigger for a certain room or use a sensor or only have the two hours being active during off hours with a condition added to the automation. None of this is perfect but forgetting the lights on for the whole night is not perfect either :slight_smile:

Do you by any chance use Zigbee2mqtt or Deconz? If yes get yourself either an Ikea or Philips remote. Put this on your nightstand, and make one of the buttons trigger a scene (in this case a scene that turns everything off). This way if you go to bed you just simply press the button and the scene will turn everything off. Obviously this has no effect during the day, but your last post talked about when you are going to bed. :joy::rofl:.

You could also get some cheap motion sensors (like xiaomi ones) or even do it very fancy by using nfc or ibeacons. Though that will make stuff really complicated and a lot more prone to errors.

Thanks. Worked nicely. :slight_smile:

1 Like

Thanks for your reply. I got it working just fine now :slight_smile:

Here is my final code. Works awesome. Thanks guys!

- alias: Turn off lights if on for too long
  trigger:
    - platform: state
      entity_id: light.light1, light.light2
      to: 'on'
      for: '02:00:00'
    - platform: state
      entity_id: light.light3
      to: 'on'
      for: '00:30:00'
  action:
  - service: light.turn_off
    data_template:
      entity_id: "{{ trigger.entity_id }}"
1 Like

Thanks for your suggestion Jimz011.

The problem with using sensors, as you suggest, is that they too have issues.
The sensor cause a problem when it doesn’t work properly and you also need to keep changing out batteries and it’s not always easy to know when they need to be replaced (not all sensors report battery levels properly). You become dependent on the sensor and you start getting used to not turning off the lights when you leave the rooms. Making it so much more of an annoyance when it’s not working, only for you to realize several hours or days later that the lights have been on all this time. :slight_smile:

The Remote is a good idea but that solves some other problem than the one I’m trying to solve in this thread. :slight_smile:

I guess this thread brings light on to the whole problem for me with automations. I dont really like making them unless they are bulletproof. I want them to help me always and not just some of the time. It’s very annoying when you start changing your behavior only to realize the automation rule did not factor in some situation. But in this case I’ve had problems with lights turned on for several days because our house is too big to realize it. :slight_smile:

Here’s what I use:

########################################################
## Turn off light after XX minutes
########################################################
- alias: Turn off Light after timeout
  initial_state: True

  trigger:
    - platform: state
      entity_id:
        - switch.basement_stairs_switch
        - switch.upstairs_hallway_switch
        - switch.mudroom_light_switch
        - switch.master_closet_switch
      from: 'off'
      to: 'on'
      for:
        minutes: 15

    - platform: state
      entity_id:
        - group.connors_bedroom_lights
        - group.kenzies_bedroom_lights
        - group.master_bathroom_lights
        - group.basement_lights
      from: 'off'
      to: 'on'
      for:
        minutes: 30

    - platform: state
      entity_id:
        - light.gym_pot_lights
        - light.guest_bedroom_ceiling_fan_light
        - light.basement_bedroom_main_lights
        - group.basement_game_lights
        - group.master_bedroom_lights
        - light.sunroom_ceiling_fan
        - light.dining_room_chandelier
      from: 'off'
      to: 'on'
      for:
        minutes: 120

  action:
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ trigger.entity_id }}"

    - service: logbook.log
      data_template:
        name: >-
          CLC Timed Off:
        message: >-
          {{ trigger.entity_id }}
1 Like

I have motion sensors that I haven’t had to change the battery in for over a year, the others that are in high traffic areas get changed every six months. I’d hardly call that an annoyance. They are cheap RF 433mhz that don’t report battery status.

Set yourself a push notification that runs every six months to remind you to change the batteries.

BTW, another idea that actually works quite well - I have an automation, that turns off all lights except the light that triggered the automation, if there is only 1 person in the house. So basically if you go from room to room, it turns off the lights behind you. I do not do that if more people are home. And I do not do that on all lights (in the living/dining rooms, I might want to have more than 1 light on, but when I get to the bedroom or office, I know that I can turn everything else off.
Works brilliantly of you have kids,

3 Likes

That maybe of interest for me. Do you mind sharing this code?

I made a blueprint from it: Single user lights

2 Likes

Hey! I know this is an old thread, but I came up with a different way of turning off forgotten devices that is easier to use and modify :slight_smile:
Basically I mark devices that I want turned off after some period of time with labels “Auto Off 2h”, “Auto Off 4h”, etc. And then I have an automation that, every 10 minutes, runs the script:

alias: Turn off forgotten devices
sequence:
  - data_template:
      entity_id: >
        {% set ns = namespace(entities='input_boolean.dummy_toggle')%} {% set
        ids = label_entities("auto_off_2h") | expand |
                     selectattr('state','eq','on')|
                     selectattr('last_changed', '<', now()-timedelta(hours = 2)) |
                     map(attribute='entity_id')|list %}
        {% for i in ids %}{% set ns.entities = ns.entities + ',' +i %}{% endfor
        %} {% set ids = label_entities("auto_off_4h") | expand |
                      selectattr('state','eq','on')|
                      selectattr('last_changed', '<', now()-timedelta(hours = 4)) |
                      map(attribute='entity_id')|list %}
        {% for i in ids %}{% set ns.entities = ns.entities + ',' +i %}{% endfor
        %} {{ ns.entities }}
    action: homeassistant.turn_off
mode: single

(That dummy toggle is there to make sure there is always something in that list to avoid errors when nothing needs to be turned off - but maybe there is a more elegant way to do that)