Prevent Motion Sensor Automation from Reactivating After Manual Light Switch Toggle

Hello everyone, I’m new to automations.

My issue: In my living room, I have a Sonoff module connected to a switch that controls a bulb. The bulb is automated to turn on and off via a Sonoff motion sensor.

The automation works well: the light turns on when motion is detected and turns off otherwise.

However, I would like to prevent the automation from restarting if motion is already detected and someone presses the same physical button. But I want it to reactivate again if the room is empty.

Is it possible to configure the system to detect if the room is already occupied when the button is pressed and, in that case, disable the automation and reactivate it again if the room becomes empty?

Can this be done without adding another Zigbee button?

To summarize: I’m in the living room, I turn off the light because I don’t want it on, and I don’t want it to turn back on immediately afterward.

Thanks in advance :blush:

Attached is the automation I have and the YAML code.

alias: Allumer 2 lampions salons
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.capteur_presence_salon_occupancy
conditions: []
actions:
  - choose:
      - conditions:
          - type: is_occupied
            condition: device
            device_id: 82ab4758c127dc697bab80f24bcd0b65
            entity_id: f8f95be72ff020dadf289a40c288d77b
            domain: binary_sensor
        sequence:
          - type: turn_on
            device_id: 730d9cd6be4109887edd4aa70bf02087
            entity_id: 397b51739a5c10c6a722739bbebd0ebc
            domain: switch
      - conditions:
          - type: is_not_occupied
            condition: device
            device_id: 82ab4758c127dc697bab80f24bcd0b65
            entity_id: f8f95be72ff020dadf289a40c288d77b
            domain: binary_sensor
        sequence:
          - type: turn_off
            device_id: 730d9cd6be4109887edd4aa70bf02087
            entity_id: 397b51739a5c10c6a722739bbebd0ebc
            domain: switch
mode: single

Couple things:

  • You need hardware that sends a signal that you turned the switch off while it is already off. Some switches do this, others don’t. Turn the switch off and then listen to events while turning it off again, and see if you find anything. Alternatively, you could turn the switch on and back off whenever you are in the room and want the light to stay off. Whatever you decide to do, let’s call the the “override event”.
  • I would be very surprised if your motion sensor stays in the detected state continuously while the room is occupied. Likely it turns on and off unless you are constantly jumping up and down in that room. In other words, it is a motion sensor not a “room occupied” sensor. If it does turn off while the room is still occupied, you’ll need to decide on a length of time that it must be off for the room to be considered unoccupied. Then you can create a template binary sensor (in YAML) using the delay_off setting to create a “room occupied” sensor.

Once you have an “override event” and a “room occupied” sensor, then you can get your automation to do what you want. Probably the easiest is with an input toggle helper. Create one and call it something like “motion automation disabled”.

Now you can add the following logic to your automation (or in new automations):

  • If the “room occupied” sensor turns off, then turn off the input toggle helper.
  • If the “override event” is detected, turn on the input toggle helper
  • Edit your current automation and for any action that would turn off the lights, add a condition that the input toggle helper must be off.

Share what you tried. What condition can’t you get to work?

Also, I would recommend avoiding device triggers, device conditions, and device actions. See this post for detail.