State trigger with 2 sensors

I’d like to make an automation using 2 sensors with their motion dedection on/off state.

Problem is i want to trigger an action only when both of the sensor states are off.

Is this code right?;

- id: '1579199711593'
  alias: Turn Off Hallway Lights
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.motion_sensor_02
    - binary_sensor.motion_sensor_03
    to: 'off'
    for: 00:02:00
  action:
  - service: light.turn_off
    entity_id: light.hallway

Simplest way is to create a group containing the two motion sensors. In your automation’s State Trigger, replace the two binary_sensors with the group.

  - platform: state
    entity_id: group.my_two_motion_sensors
    to: 'off'
    for: 00:02:00

So will trigger wait for both sensors to be off before action if i group them, right?

Correct. The group reports on if any of its members are on. It will report off when all of its members are off.

To add to this, when multiple entities are added to a trigger, HA evaluates them as ‘OR’. Therefore, you current automation will be triggered if any of the sensors go off.

Ahh then grouping is exactly what i’m looking for then thanks :slight_smile:

By the way how do i group them? Like this;?

group.yaml

hallway_sensor_group:
  name: "hallway_sensor_group"
  entities:
    - binary_sensor.motion_sensor_02
    - binary_sensor.motion_sensor_03

Assuming you have defined it in the groups.yaml file, it appears to be correct (Group documentation).

You will need to execute Configuration > Server Controls > Reload Groups in order to load the newly created group into Home Assistant. You may want to execute Check Configuration prior to reloading groups (to ensure there are no syntax errors).

Yep i defined them in group.yaml and it seems to be working, thanks @123 and @hass :pray:

1 Like