Manual vs Automation of switching lights ON/OFF

Hello -
I have the following automation to turn on and off my backyard lights (after 3 minutes) based on motion from my Ring camera. However, when I manually turn on the light with, the automation kick on and turn off the lights.

Is there a way to bypass automation when the wall switch is manually turned off/on?

Thx!


- alias: Light on - motion
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_motion
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') < 0 }}'
  action:
    service: switch.turn_on
    entity_id: switch.backyard_lights

- alias: Light off - 3 minutes
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.backyard_lights
    to: 'on'
    for:
      minutes: 3
  action:
    service: switch.turn_off
    entity_id: switch.backyard_lights

Yes, you need to add in an input boolean:

  1. The automation to turn the lights on turns on the boolean
  2. The automation to turn the lights off only turns them off if the boolean is on
  3. Any time the light turns off, the boolean turns off.

That gives you a flag (the boolean) to let the system know that it is controlling the lights, not a human. I’ve got written examples here.

1 Like

Thanks for the guidance.

After looking at your site, I came up with the lines in my config files. The problem i’m having is the boolean switch turns on when the “alias: Light on - motion” automation kicks on but the boolean switch does not turn off when the " Light off - 3 minutes" automation kicks off.

Any additional help would be much appreciated.

Configuration.yaml

input_boolean:
  your_switch_auto_on:
    name: "Automatically turned on your switch"
    initial: off

Automation.yaml

- alias: Light on - motion
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_motion
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') < 0 }}'
  action:
  - service: switch.turn_on
    entity_id: switch.backyard_lights
  - service: input_boolean.turn_on
    entity_id: input_boolean.your_switch_auto_on


- alias: Light off - 3 minutes
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.backyard_lightsPreformatted text
    to: 'on'
    for:
      minutes: 1
  condition:
  - condition: state
    entity_id: input_boolean.your_switch_auto_on
    state: 'on'
  - condition: state
    entity_id: switch.backyard_lights
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.backyard_lights
```Preformatted text

image

I figured it out. I forgot to add the 3 switch automation to turned the boolean switch off. @Tinkerer thanks for your help.

1 Like

Hello -
I recently upgraded HASSIO environment to the latest version and now I’m having problems with Ring Camera automation. When Ring detects motion, my backyard lights come on but that do not automatically turn off after the set time in the script. Any help trouble-shoot this issue would be much appreciated. Below are the lines in my automation.yaml. Thanks!

- alias: Light on - motion
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_motion
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') < 0 }}'
  action:
  - service: switch.turn_on
    entity_id: switch.backyard_lights
  - service: input_boolean.turn_on
    entity_id: input_boolean.your_switch_auto_on
    
- alias: Light off - 3 minutes
  id: Backyard Light
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.backyard_lights
    to: 'on'
    for:
      minutes: 1
  condition:
  - condition: state
    entity_id: input_boolean.your_switch_auto_on
    state: 'on'
  - condition: state
    entity_id: switch.backyard_lights
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.backyard_lights
    
- alias: "Switch turned off"
  initial_state: "on"
  trigger:
    - platform: state
      entity_id: switch.backyard_lights
      to: 'off'
  condition:
    - condition: state
      entity_id: input_boolean.your_switch_auto_on
      state: 'on'
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.your_switch_auto_on

I’d suggest you to post you question in a separate topic as it’s about an issue with your automation/system configuration and it’s different to the original question asked 7 months ago :wink:

I can only say that
a) you don’t need the 2nd condition in your “Light off - 3 minutes” automation
b) that a) automation has minutes: 1 so it triggers after 1 minute of the light being on
c) remove the condition from “Switch turned off” automation - turn that flag off anyway.
d) remove all initial_state (unless you know what it does)

1 Like

Thanks for sharing the example.
Could you explain why it’s needed to have a separate 3rd automation - won’t it work if in the 2nd one we turn off the input_boolean as the last action?
I presume that people are copying code from there - could you take a look at my comments as well?

I’ve tried several different ways to resolve this with no luck. This automation worked prior to me upgrading to the latest version of hassio. Did something change between the versions?

Thanks.

Why do the first two automations have the same id?
Make them different.

Just for fun, I’ll add my own version of a “motion-activated light that automatically turns off but only if it was activated by motion”.

- alias: 'Light Auto On'
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_motion
    to: 'on'
  condition:
    - condition: template
      value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}"
    - condition: state
      entity_id: switch.backyard_lights
      state: 'off'
  action:
    - service: homeassistant.turn_on
      entity_id: 
        - input_boolean.your_switch_auto_on
        - switch.backyard_lights

    
- alias: 'Light Auto Off'
  trigger:
    platform: state
    entity_id: input_boolean.your_switch_auto_on
    to: 'on'
    for:
      minutes: 3
  action:
    - service: homeassistant.turn_off
      entity_id:
        - input_boolean.your_switch_auto_on
        - switch.backyard_lights
1 Like

lol. I don’t use ids as they are useless unless (I presume) one uses Automation Editor - if you have id and alias, the latter is always used so what’s the point in using id?
experimented just yesterday with aliases - they are used to generate object_id and act as friendly_name in the same time (with spaces converted to underscores etc) and there is no way to set a friendly_name it via customise.yaml (didn’t work for me) so basically there is no way to give your automation a nice name and know the exact name of your automation (to use it in service call, for example) as to be able to do that one need to know the HA conversion rules (which may change in the future as it’s internal thing).
I might be wrong though but that’s how I see it now.

if you use templates, how about reducing conditions in the on automation to

- condition: template
    value_template: >
      {{
        state_attr('sun.sun', 'elevation') < 0 and
        is_state('switch.backyard_lights', 'off')
      }}

?

UPDATE: however, it depends on what camp are you in according to this discussion :wink:

Sure, why not.

As for the id, it is, as you mentioned, used by the Automation Editor. That’s the identifier it uses to uniquely identify each automation. I don’t use the Automation Editor but I imagine it may be unhappy when it encounters two or more automations using the same identifier. I don’t know if it has any bearing on JohnB’s reported problem but, in the interests of disambiguation, it would be best if the automations have unique identifiers.

I’ll try giving them a unique id to see if that soles the problem. The lights turn on on motion from the ring camera, but do not turn off after 1/3 minutes. I used 1 minute for testing.

try one of the proposed solutions. if it doesn’t work, create a new topic as I suggested earlier.

So that no matter how the switch/light/whatever is turned off the boolean is turned off. Otherwise if it’s turned off manually, or by another automation, it’ll remain on.

And why do we need this feature (remain on) considering it was created to indicate the fact an automation turned the light on? Shouldn’t it be tied to that automation as it might cause very weird side effects…

Do it your way and see how it works for you :man_shrugging:

The way I’ve documented it is how I use it - works just fine for me with no weird side effects :wink:

I will. The thing is people copy your example so if there is a problem, it becomes their as well :wink:

I didn’t say it doesn’t work, just pointed out the 3rd automation should be incorporated into the 2nd so it’s less code/less room for error/easier to understand and maintain - win-win-win situation.

There will be no weird side effects unless something like this happens

So potentially it might get stuck and disrupt your light automation.

My point is simple - if the flag is for lights only, it should be controlled by lights automations only or you’re asking for troubles.
If it’s controlled only by lights automation, it’s enough tu turn it off when the automation switches the light off as it’s over.

Yes, there are cases when a flag is used to indicate that one of automations is controlling the lights and that means other automations should wait/not trigger, but it’s not the case here so again, you’re asking for troubles or overcomplicating your config.

But I’m not insisting :wink:

I like your solution. I changed it a bit because yours always shuts off after 3 minutes, even if the motion sensor is on. My turn off routine is :


    trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_kitchen_occupancy
      to: 'off'
    condition:
      condition: state
      entity_id: input_boolean.wled
      state: 'on'
    action:
    - service: homeassistant.turn_off
      entity_id: 
        - light.wled
        - input_boolean.wled

This then turns off after the motion sensor is off.