Turn on/off light using one automation and based on the status change of a motion sensor

Greetings,

I see a lot of automation examples to turn lights on and off based on a motion sensor using 2 automations (1 for ON and one for OFF)
Is there a way to address the on / off automation by assessing in the trigger just a change of status (regardless if it goes from occupancy to clear or vice versa) and in the action turning on or off the lights based on that status change?

Thanks

Giancarlo

Welcome Giancarlo!

I assume the simplest approach is as follows (I’m using it in my wardrobe):


  alias: Garderobe 
  description: /packages/automationen/diele/garderobe.yaml
  mode: single
  max_exceeded: silent

  trigger:
  - platform: state
    entity_id: binary_sensor.bewegung_garderobe
    to:
    - 'on'
    - 'off'
    from:
    - 'on'
    - 'off'

  action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.garderobe

mode: single works fine with my Hue motion sensor. With Aqara I made the experience that mode: restart is working better.

For a motion sensor example, try this…
The example below I use in my kitchen. You’ll note that the sensor only triggers to turn on the light, not off.

The off comes later on when there is a wait for trigger. If no motion is detected for 5 minutes, then the lights go off, otherwise it just waits there indefinitely until it is sent a motion clear event.

description: ""
mode: single
trigger:
  - type: motion
    platform: device
    device_id: f812b27b7f45a6f29272be68528dea77
    entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
    domain: binary_sensor
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: 100
      rgb_color:
        - 41
        - 219
        - 255
      transition: 1
    target:
      entity_id: light.kitchen_light_group
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: f812b27b7f62a6f29272be68528dea68
        entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.kitchen_light_group

The other option is to use 2 triggers, one for on, one for off, and use trigger id’s to then decide what to do later on using either an if or choose condition.

I use this in another automation but this should give you the idea:

description: ""
mode: single
trigger:
  - platform: state
    id: ensuite_b
    entity_id:
      - input_boolean.ensuite_bathroom_trv_switch
  - platform: state
    id: downstairs_b
    entity_id:
      - input_boolean.downstairs_bathroom_trv_switch
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: ensuite_b
        sequence:
          - type: turn_off
            device_id: c2d723e042a5db4dde6287f7c1597f2b
            entity_id: switch.aqara_switch_top_floor_bathroom_switch
            domain: switch
      - conditions:
          - condition: trigger
            id: downstairs_b
        sequence:
          - type: turn_off
            device_id: 8d74b7c8e812e444564b8c4f46021cde
            entity_id: switch.aqara_switch_module_ensuite_switch
            domain: switch

Thanks for the replies,
now I have a base to start!
cheers
Gc

Hello all

After testing several option I decided to opt for an intermediate solution given an extra variable.
I realized that when the shower curtain is close while showering the light turns off because the sensor on the ceiling does not register any movement.

So the option of showering with the curtain open or change it to one transparent is not that appealing.

I decided to add a motion sensor on the roof of the shower working in tandem with the bathroom one.

I let any sensor to trigger any event and then I used the and and or operator in the action using the ´choose’ action based on the sensors status.

In essence if the two sensor are both off —> turn the light off
If any of the sensors is on —-> keep or turn the light on.

To the next challenge
Cheers

Giancarlo

In this way the automation is triggered only by the sensor change status and nobody has gets the soap in the eyes.

Cheers

Giancarlo

Have you group both sensors into a binary sensor group and treated them as a single entity, or are you checking each one individually ? (both options work).

1 Like