Turn of light when 2 motion sensors haven'd detected motion

Hi there,

I have 2 motion sensors in my living room and I can’t get it to turn the lights off when the 2 of them havend detected motion. With 1 sensor it works perfectly but with 2 it doesn’t. The lights turn off if either one of them has no motion or they don’t turn off at all. Is there a automation to fix this problem?

Use a timer, and whenever motion is detected for either sensor, reset it back to x minutes.

Callifo’s suggestion would work but the timers run on the motion detected.
If you post your automation maybe we can help

Yes, providing some details of the entities involved, and the automation or automations you have so far, would be very helpful.

But, assuming the two motion sensors are binary sensors (I’ll refer to them as binary_sensor.motion_1 and binary_sensor.motion_2), if you want an automation that turns off a light when they both have changed to `'off``, then the easiest way (especially if you use the automation editor) is to trigger when either goes off, and then have a condition that they are both off:

- trigger:
  - platform: state
    entity_id:
    - binary_sensor.motion_1
    - binary_sensor.motion_2
    to: 'off'
  condition:
  - condition: state
    entity_id:
    - binary_sensor.motion_1
    - binary_sensor.motion_2
    state: 'off'
  action:
  - service: light.turn_off
    entity_id: light.my_light

Or, using a template trigger:

- trigger:
  - platform: template
    value_template: >
      {{ is_state('binary_sensor.motion_1', 'off') and
         is_state('binary_sensor.motion_2', 'off') }}
  action:
  - service: light.turn_off
    entity_id: light.my_light

Thanks for the help, but the to automations you suggested didn’t work. I have 2 lights in my livingroom and 2 sensors:
entity_id: switch.0x680ae2fffe40e9a0_switch
entity_id: switch.0x680ae2fffe414f3b_switch

entity_id: binary_sensor.0x00158d00046020bf_occupancy
entity_id: binary_sensor.0x00158d000451d4ab_occupancy

I have 2 automations to trigger the lights on when motion is detected.

- id: '1590011931755'
  alias: Livingroom1
  description: ''
  trigger:
  - device_id: 83a60ec9d82a4198b8b2841584e989c9
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d00046020bf_occupancy
    platform: device
    type: motion
  - device_id: 3a3890008e0d4a379c712e17671ea705
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d000451d4ab_occupancy
    platform: device
    type: motion
  condition:
  - after: sunset
    condition: sun
  action:
  - device_id: 8a11139e7fd14b689317176c0cc9d774
    domain: switch
    entity_id: switch.0x680ae2fffe40e9a0_switch
    type: turn_on
  - device_id: 936d9eecff03448388512a29c99522a2
    domain: switch
    entity_id: switch.0x680ae2fffe414f3b_switch
    type: turn_on
- id: '1594331405459'
  alias: Livingroom 2
  description: ''
  trigger:
  - device_id: 83a60ec9d82a4198b8b2841584e989c9
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d00046020bf_occupancy
    platform: device
    type: motion
  - device_id: 3a3890008e0d4a379c712e17671ea705
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d000451d4ab_occupancy
    platform: device
    type: motion
  condition:
  - before: 01:30
    condition: time
  action:
  - device_id: 8a11139e7fd14b689317176c0cc9d774
    domain: switch
    entity_id: switch.0x680ae2fffe40e9a0_switch
    type: turn_on
  - device_id: 936d9eecff03448388512a29c99522a2
    domain: switch
    entity_id: switch.0x680ae2fffe414f3b_switch
    type: turn_on

I have the two motion sensors combined in a group. Then the group acts as one motion sensor. So that way if either is triggered the light goes on. If they both stop sending motion the timer is triggered to go off.

And how did you get them in a group?

What Phil advised should have worked for you.
Admittedley it does not have a delay on but we could introduce that once it works.
You have not showed the code you tried, (i.e. as you implemented it)

Looking at your code it is obviously generated byt the GUI editor and I get (understand) : -

  action:
  - device_id: 8a11139e7fd14b689317176c0cc9d774
    domain: switch
    entity_id: switch.0x680ae2fffe40e9a0_switch
    type: turn_on

But can I ask why your switch is called : - switch.0x680ae2fffe40e9a0_switch ?
You do know that you can rename both the ‘Name Override’ (friendly name) and the ‘entity_id’ ? say from switch.0x680ae2fffe40e9a0_switch to say switch.livingroom_lamp01 and have the friendly name as say “Ruben’s Lamp” This makes reading the code easier and even if you choose from a list it’s considerably less obscure.

But the other bit : -

  trigger:
  - device_id: 83a60ec9d82a4198b8b2841584e989c9
    domain: binary_sensor
    entity_id: binary_sensor.0x00158d00046020bf_occupancy
    platform: device
    type: motion

I’m a bit lost with. why does it have both a device_id AND an entity_id ??
type: motion - really not sure about this, a binary sensor is either ‘on’ or ‘off’ (I know that the front end transcribes this to other states according to device class, but it’s still ‘on’ or ‘off’)
I don’t see how this triggers

That’s a new “device” type trigger. That’s just how they work, and they’re not intended to be hand edited, as I understand it.

I understand why device triggers & actions were created, but it sure makes it harder to help people that way.

Please post the automation you created when you tried to implement my suggestion. These device type triggers and actions are new and many of use are unfamiliar with them because we don’t use the automation editor – we hand edit YAML files.

Well, I ‘would’ “Like” your post, but maybe someone would think that I “like” the 'implications' (Which I definitely do not ! ) :rofl: (sorry, it’s not personal ! :smiley: )

1 Like

I just copy and pasted your suggestion and changed the id’s.

And how did you get them in a automation?

trigger:
      - platform: state
        entity_id: group.all-motion-sensors
        to: 'on'

And off:

trigger:
      - platform: state
        entity_id: group.all-motion-sensors
        to: 'off'
        for:
          minutes: 5

Thanks Frits, that was what i needed. It works now! here is my automation:
Turn on:

- id: '1594590949115'
  alias: Livingroom
  description: ''
  trigger:
  - entity_id: group.livingroom
    platform: state
    to: 'on'
  condition:
  - after: '21:30'
    before: 02:00
    condition: time
  action:
  - device_id: 8a11139e7fd14b689317176c0cc9d774
    domain: switch
    entity_id: switch.0x680ae2fffe40e9a0_switch
    type: turn_on
  - device_id: 936d9eecff03448388512a29c99522a2
    domain: switch
    entity_id: switch.0x680ae2fffe414f3b_switch
    type: turn_on

Turn off

- id: '1594591100122'
  alias: Livingroom off
  description: ''
  trigger:
  - entity_id: group.livingroom
    for: 00:02:00
    platform: state
    to: 'off'
  condition: []
  action:
  - device_id: 8a11139e7fd14b689317176c0cc9d774
    domain: switch
    entity_id: switch.0x680ae2fffe40e9a0_switch
    type: turn_off
  - device_id: 936d9eecff03448388512a29c99522a2
    domain: switch
    entity_id: switch.0x680ae2fffe414f3b_switch
    type: turn_off
1 Like

I learned this too about 3 month ago from another great member of this community. So the credits should go to @Tinkerer for this one.
Check out his blog for more handy tips and tricks: https://blog.ceard.tech/?m=1

1 Like

Well then it should have worked. If you could have posted exactly what you did I might have spotted the problem. But @frits1980’s solution is also a good one. Glad you got it working either way!

For anyone else coming across this very helpful thread, I learnt a couple of things whilst trying to get it to work:

  • My installation of HA had a separate groups.yaml file, but apparently that has just recently (at the time of writing) been deprecated.

  • In any case, that was for ‘generic groups’, and platform-specific groups are preferable

  • This is what the entry for my motion sensor group looks like in my configuration.yaml file:

binary_sensor:
  - platform: group
    name: Downstairs Toilet and Utility Motion Sensor Group
    unique_id: downstairs_toilet_utility_motion_group
    device_class: motion
    entities:
      - binary_sensor.downstairs_toilet_motion
      - binary_sensor.utility_room_motion