Trouble Keeping the Lights On - Automation Conflict

Hello,

I have two automations that are conflicting. I want the garage light to come on when the door opens and then go off at the sooner of an expiration of a 5min timer since the door was opened or when the door closes.

The problem occurs when I open the door, go inside, and shut it. The light goes off, of course, so I turn it back on. So far, so good. About five minutes later the light goes off again! That’s the problem. Last time this happened, I was halfway down the ladder carrying something in my arms. Any ideas on how to better improve the automation w/o helper objects (if possible)?

alias: Garage Interior Light  On For 5 Min When Garage Door Open
description: ""
trigger:
  - platform: state
    entity_id:
      - cover.garage_door1
    to: open
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.garage_interior_lights_light
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.garage_interior_lights_light
mode: single

alias: Garage Interior Lights Off When Garage Door Closes
description: ""
trigger:
  - platform: state
    entity_id:
      - cover.garage_door1
    to: closed
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.garage_interior_lights_light
mode: single

Thanks,
Jason

Without a motion sensor in your garage, the automation can’t determine if it’s occupied or not. Without knowing the garage’s occupancy status, only the door’s status, there’s not much you can do to prevent the automation from turning off the light when it’s occupied.

1 Like

I disagree. I just need to interrupt the automation with the timer when the switch is flipped back on before it has expired. How do I do that?

I’ve just started trying to move automations across from node-red, so a bit new to this. There’s probably a more elegant way to do this but…

You could create a timer helper, instead of the delay and turn_off actions in your first automation, set the timer to 5 minutes.

Use the timer ending event as an additional trigger in your second automation, when you turn off the light also cancel the timer, so if the door closes before the timer is up, the timer will be cancelled.

1 Like

To prevent the garage light from turning off after a certain time when the door is closed, you can use a template sensor and a delay action in your automation.

Here is an example automation that uses a template sensor and a delay action to keep the garage light on when the door is closed:


- alias: "Garage Light Control"
  trigger:
    - platform: state
      entity_id: binary_sensor.garage_door
      to: "on"
  action:
    - service: light.turn_on
      entity_id: light.garage_light
    - delay:
        minutes: 5
    - service: light.turn_off
      entity_id: light.garage_light
      condition:
        - condition: template
          value_template: "{{ not is_state('binary_sensor.garage_door', 'on') }}"

In this automation, the template sensor is used to check the state of the garage door binary sensor, and the delay action is used to wait for 5 minutes before turning the garage light off. The condition parameter is then used in the service call to turn the garage light off, and it will only execute if the garage door binary sensor is not in the “on” state.

This will allow the garage light to stay on when the door is closed, and it will only turn off after 5 minutes if the door remains closed. You can customize this automation by changing the entity IDs, the delay time, and the value_template of the template sensor to match your setup.

Your automation is using a delay, which offers no means of cancellation, as opposed to an actual timer which does (timer.cancel).

You can try redesigning it to use a timer but I think the challenge you may face is ensuring it is cancelled only for the correct circumstances.

The example you posted fails to pass the configuration check because the final Template Condition is malformed. Even if you correct it (the initial condition key word is missing a leading hyphen), its position in action doesn’t achieve anything. Perhaps you meant to put it before the light.turn_off service call?

There’s no “template sensor” in the automation you posted. It contains a Template Condition.

Thanks for the responses, everyone. Sorry for the delay. I ended up taking a different approach after giving your suggestions further thought.

I believe I have solved it with three automation: One to just turn on the light when the door opens. A second to just turn off the light when the door closes. Then, I’m using the following to turn off the light five minutes after the door opens but only if the light is on. This condition shuts it down if the door closes (and light goes off). Then, turning the light back on stays on.

alias: Garage Interior Light  On For 5 Min When Garage Door Open
description: ""
trigger:
  - platform: state
    entity_id:
      - cover.garage_door1
    to: open
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: light.garage_interior_lights_light
    state: "on"
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.garage_interior_lights_light
mode: single

Thanks for the help.

I do this in node red with my light automations. I create global variables (guess you could use input_select) with true/false or on/off. You set it on off with the automation when operating the light then use it as condition together with light state to know if the light is on because of the automation or not.

This single automation can replace your 3 automations.
What will happen is that when you open the door the light will go on, then the automation will wait for you to close the door, if you do it within 5 minutes it will turn off the light, but it will also do it after a timeout of 5 minutes if the door is still open.
No matter if you close the door or the timeout expires it will only turn of the light once until you open the door again and the process repeats.
To better explain, the automation waits for you to close the door OR the timeout to pass so it can run it’s final action which is turn of the light. No matter what comes first. After that final action the automation cycle is done and no more actions will be performed.
So if you flip the switch to turn on the light it will stay on as long as you do not open the door which will trigger the automation again.

description: ""
mode: restart
trigger:
  - platform: state
    entity_id:
      - cover.garage_door1
    to: open
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.garage_interior_lights_light
  - wait_for_trigger:
      - platform: state
        entity_id:
          - cover.garage_door1
        to: closed
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.garage_interior_lights_light

Super interesting. Thanks.

This solution has something worth mentioning. If the light is turned on outside of this automation, the light will not turn off when the door shuts. Fundamentally, the automation makes complete sense and I would agree that Home Assistant should mind its own business if it didn’t turn the light on in the first place. However, habits die hard. I keep leaving the light on with the door shut. :slight_smile:

You could solve it in the same automation with another trigger and trigger id’s, let me know if needed :slight_smile: