Automation - binary_sensor state prevent from triggering

Hello.
I’m looking for help with a proper answer. I have automation to turn swith on (MQTT Swtich) when message under topic “zigbee2mqtt/xiaomi_motion_sensor” will contain occupancy true and only if sensor state is off.

- alias: "Kitchen light ON (motion sensor)"
  trigger:
    platform: mqtt
    topic: "zigbee2mqtt/xiaomi_motion_sensor"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: "{{ true == trigger.payload_json.occupancy }}"
      - condition: state
        entity_id: binary_sensor.xiaomi_motion_binary_sensor
        state: "off"
 #also tried condition template and  value_template: "{{ is_state('binary_sensor.xiaomi_motion_binary_sensor', 'off') }}"
  action:
    service: mqtt.publish
    data_template:
      topic: "cmnd/sonoff/power"
      payload: "on"

And this automation doesn’t work.
The reason is condition. Proper message is send to “zigbee2mqtt/xiaomi_motion_sensor” but automation is not triggered because this part of condition doesn’t met:

      - condition: state
        entity_id: binary_sensor.xiaomi_motion_binary_sensor
        state: "off"

If I remove this small part, then automation works well.

This is my binary_sensor (motion sensor from xiaomi aquara products which working via zigbee2mqtt. Payload on/off is true/false but status of this binary_sensor is on/off):

- platform: "mqtt"
  name: "xiaomi_motion_binary_sensor"
  state_topic: "zigbee2mqtt/xiaomi_motion_sensor"
  availability_topic: "zigbee2mqtt/bridge/state"
  payload_on: true
  payload_off: false
  value_template: "{{ value_json.occupancy }}"
  device_class: "motion"

HA -> Developer Tools -> Templates shows me correct status for binary_sensor.xiaomi_motion_binary_sensor so I don’t understand why automation doesn’t work…
I suppose that my “and” condition is looking for state change. So It will work when proper mqtt message arrive AND state for my binary_sensor will be changed from something to “off”.
I’m not 100% sure about it, this is only my speculation.
If this is correct then how I can check status in conditions? I want to trigger this automation when message arrive and if binary_sensor have “off” status. Any idea?

Your conditions are contradicting each other.

binary_sensor.xiaomi_motion_binary_sensor is a reflection of MQTT Topic zigbee2mqtt/xiaomi_motion_sensor with an occupancy json value of true. It will only be false/off if zigbee2mqtt/xiaomi_motion_sensor#occupancy is false. So, you are effectively checking the same thing twice with your conditions, and one is testing for true and the other for false. It can’t be both at the same time.

Just use:

trigger:
  - platform: state
    entity_id: binary_sensor.xiaomi_motion_binary_sensor
   to: "on"

with no conditions and it will trigger whenever there is motion.

1 Like

Yeah of course you have right.
I’ve no idea why I haven’t seen it before.

1 Like