Don't run automation when a light switch has manually been pressed

Hi everyone,

I am very new to HA and still learning. I have some kitchen cabinet lights which are controlled by a shelly rgbw switch. I also have an Aqara presense sensor. At the moment I have it set so if there is motion in front of the kitchen ( sensor can’t see into the kitchen so I can’t use it after the person is out of sight ) and it is between 8:30pm and 7:00am, then the lights turn on. After 30 seconds the lights automatically turn off.

What I am trying to do is have this automation not run if the light switch for the cabinet lights has been manually turned on. I have no clue how to go about doing it. Adding a conditional at the start of the YAML file does not work. Any help will be appreciated.

Here is my config right now.

alias: Kitchen Cabinets on/off with motion
description: ""
trigger:
  - alias: When Kitchen Area Motion Is Detected
    platform: state
    entity_id:
      - binary_sensor.lounge_presence_sensor_kitchen
    to: "on"
    id: Motion Detected in Kitchen
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.lounge_presence_sensor_kitchen
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition:
  - condition: time
    after: "20:30:00"
    before: "07:00:00"
action:
  - if:
      - condition: trigger
        id:
          - Motion Detected in Kitchen
    then:
      - service: light.turn_on
        target:
          entity_id: light.kitchen_cabinets
        data: {}
    else:
      - service: light.turn_off
        target:
          entity_id: light.kitchen_cabinets
        data: {}
mode: single

I’m sure there are multiple solution for this but what I would do

1’st create a toggle off Setting → Device & Services → helpers. Then I’d split your automation to have one for when motion changes to on and one for when it changes to off.

In the automation for on, in the primary condition section, I would add a second condition and would be a requirement for the light to be off before the automation would run. In the event the automation ran I’d have two actions, first turn the light on and second turn the helper on.

Then in the second automation that triggered when the motion state was off for 30 seconds, the condition would check to see if the helper was on, thus only run if the helper was turned on my the motion on automation. In the event the helper was on, the automation would have two actions to turn both the light and the condition off.

You’re just starting out, but what you’re asking for is a bit advanced.

This explains how to use context.id in an automation to determine whether a switch was physically pressed or not.

I can’t help much beyond providing that link though - still have to delve into using context.id in my automations.

1 Like

You should detach the shelly switch from the light.
as well as create a binary helper

Then you create an automation with 3 triggers, mode restart.
One for switch on (trigger_id: switched_on)
Second one switch off (trigger_id: switched_off)
Third trigger for motion (trigger_id: motion)

So something like this:

alias: Automatic on motion between 20:30 and 07:00, manual when turned on manual
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.detached_shelly_kitchen
    from: "off"
    to: "on"
    id: switch_on
  - platform: state
    entity_id:
      - switch.detached_shelly_kitchen
    from: "on"
    to: "off"
    id: switch_off
  - platform: state
    entity_id:
      - binary_sensor.lounge_presence_sensor_kitchen
    from: "off"
    to: "on"
    id: motion
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - switch_on
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.kitchenlight_manual_helper
      - conditions:
          - condition: trigger
            id:
              - switch_off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.kitchenlight_manual_helper
      - conditions:
          - condition: trigger
            id:
              - motion
          - condition: time
            after: "20:30:00"
            before: "07:00:00"
          - condition: state
            entity_id: input_boolean.kitchenlight_manual_helper
            state: "off"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
mode: restart
1 Like

Hi aceindy,

This looks like it is about to work… but isn’t working for me. The light is turning on when I activate the sensor, but upon “clearing” it, the light continues to stay on.

Shelly is not hooked up to any switch, the switch was the shelly itself ( as you suggested). I added the helper as a toggle like you said with the correct name ( kitchenlight_manual_helper ). I assume the issue is due to the helper is not turning back to “off” after the detection goes from “detected” to “clear”. Does this mean a 4th option would be required?

Cheers!

The shelly needs to be in detached mode.
Detached means that the switch itself does not control the light.
You will then end up with 2 entities:

  • switch.detached_shelly_kitchen
  • light.kitchen_cabinets

In your current config, does the kitchenlight_manual_helper ever get set/un-set ?
it should whenever the shelly detached switch is operated…

If the helper remains ‘on’ all the time, the 3rd option will never have the condition met.

The helper gets set to “on” when presense is detected. However, it does not turn to “off” once the presense sensor is clear.

I am not sure what ‘detatched’ mode is. Is this a physical switch on the Shelly, or is it an app setting? Or is it a HA setting even?

EDIT: Just found the mode in the app. Updated it, but the results are the same.

That is wrong; it should set when the switch is operated.

When detached, the switch connected to the shelly won’t do anything, except tell HA it was activated.
Then HA will operate the relay (= light).

Sorry, I think I need to reword this as I screwed up the wording the first time and it is not clear. Here is the setup.

Shelly is hardwired into the power. There is no physical “switch” to turn Shelly ( kitchen cabinet lights ) on/off. The only way Shelly ( the kitchen cabinet lights ) can be manually turned on/off is either by asking Alexa to switch them on/off, or by clicking a button in Home Assistant.

Shelly is hooked up to RGBW strips being powered by a 24v power supply.

I just tested again and can confirm, when the presense sensor is activated, the helper turns on and will not turn off. Here is the code from the YAML file.

alias: Turn on/off kitchen cabinet lights based on presense sensor v2
description: ""
trigger:
  - platform: state
    entity_id:
      - light.kitchen_cabinets
    from: "off"
    to: "on"
    id: switch_on
  - platform: state
    entity_id:
      - light.kitchen_cabinets
    from: "on"
    to: "off"
    id: switch_off
  - platform: state
    entity_id:
      - binary_sensor.lounge_presence_sensor_kitchen
    from: "off"
    to: "on"
    id: motion
condition: null
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - switch_on
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.kitchenlight_manual_helper
      - conditions:
          - condition: trigger
            id:
              - switch_off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.kitchenlight_manual_helper
      - conditions:
          - condition: trigger
            id:
              - motion
          - condition: time
            after: "20:30:00"
            before: "07:00:00"
          - condition: state
            entity_id: input_boolean.kitchenlight_manual_helper
            state: "off"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
mode: restart

Sorry about the confusion. I should have worded it better, mate.

Then there is no way to detect that the light was switched on manually…you need a detached switch from the shelly (or any other button; could also be a zigbee or rf433 switch)
We are talking about the ‘I’ input connected to the switch (shown on top here):

Using the light state itself will end up in a dead-lock situation (as you noticed) :wink:

Ah damn! I was hoping this could be done with a virtual switch. That’s okay, you can’t win them all.

Thank you very much for your help with this :slight_smile: Very much appreciated!

Cheers!

Possible…you could make another helper (toggle), and use that as a virtual switch to operate the light.
But I figured manual would be ‘outside’ HA :wink:

That may actually be okay, since I will eventually put a tablet to control HA things like switches, etc. But that won’t be for a while. If it’s not a lot of work for you, would you be able to post up the code for it? That way I can come back to this thread when I get the tablet. It may also be helpful to others in the same situation as me.

Either way, thanks heaps for the help :slight_smile:

In that case think you don’t even have to turn the helper on/off (as it is already toggled from HA)

So then it becomes something like this:

alias: Automatic on motion between 20:30 and 07:00, manual when turned on manual
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.kitchenlight_manual_helper
    from: "off"
    to: "on"
    id: switch_on
  - platform: state
    entity_id:
      - input_boolean.kitchenlight_manual_helper
    from: "on"
    to: "off"
    id: switch_off
  - platform: state
    entity_id:
      - binary_sensor.lounge_presence_sensor_kitchen
    from: "off"
    to: "on"
    id: motion
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - switch_on
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
      - conditions:
          - condition: trigger
            id:
              - switch_off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
      - conditions:
          - condition: trigger
            id:
              - motion
          - condition: time
            after: "20:30:00"
            before: "07:00:00"
          - condition: state
            entity_id: kitchenlight_manual_helper
            state: "off"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.kitchen_cabinets
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.kitchen_cabinets
mode: restart

Here as long as the kitchenlight_manual_helper equals ‘on’ , the motion will not be active:

          - condition: state
            entity_id: kitchenlight_manual_helper
            state: "off"

And when the kitchenlight_manual_helper equals ‘off’, this condition still needs to be met:

          - condition: time
            after: "20:30:00"
            before: "07:00:00"

If not, it still won’t pass :wink:

2 Likes

You’re a champ. Thanks very much!

Welcome…don’t forget to mark as solution (after you gor it to work)

Yup, I marked your previous post as the solution as in my case it will not be possible. Thanks again!

Perfect. This approach works really nicely for me. I added in a few more conditions (sunset → sunrise) but otherwise it’s pretty much exactly what I’m looking for.

Might have to try and turn it into a Blueprint as I’ll want to do the same thing in a few places. Only issue is some are lights and others are switches, which may make the blueprint harder as I’ve not written one before. Will give me an excuse to learn!