MQTT trigger wont trigger timer

Hi All

Recently with the changes made in Z2M I migrated my automation from node-red into HA automation. I am using sonff zigbee PIR sensors to trigger my lights for 10mins then turn off. I initially used the delay function but found that to be unreliable to am trying to move to timers.

I used the following code for a few WIFI PIR sensor that change state this worked fine however with MQTT trigger, it triggers the turn on automation but not the timer so never turns off. I understand that in my code there is no payload and I am guessing that is where the problem lies. I tried a number of variables with value_template but no joy. I have spent days on this with no luck so any help is greatly appreciated.

The MQTT payload is {"battery":100,"battery_low":false,"linkquality":43,"occupancy":true,"voltage":3000}
In MQTT explorer the message seems to trigger quickly then go back to false.

alias: Bedroom PIR Timer
description: ""
triggers:
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bedroom_pir
    id: Timer finished
  - trigger: mqtt
    topic: zigbee2mqtt/Zigbee Bedroom PIR
    id: Motion Stopped
  - trigger: mqtt
    topic: zigbee2mqtt/Zigbee Bedroom PIR
    id: Motion Detected
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion Stopped
        sequence:
          - action: timer.start
            target:
              entity_id: timer.bedroom_pir
            data:
              duration: "600"
      - conditions:
          - condition: trigger
            id:
              - Motion Detected
        sequence:
          - action: timer.cancel
            target:
              entity_id: timer.bedroom_pir
            data: {}
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              device_id: 9b51e60cd2e377dedcb57680b814a39f
      - conditions:
          - condition: trigger
            id:
              - Timer finished
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              device_id: 9b51e60cd2e377dedcb57680b814a39f
mode: single

Thanks again

Zigbee2MQTT should have modeled your Sonoff PIR as a binary_sensor entity in Home Assistant. Your automation ought to use a State Trigger to monitor the Binary Sensor’s state (on/off).

That’ll resolve the problem of not stopping the timer (because you currently have two identical MQTT Triggers so it only acts on the first one; the second one, that serves to cancel the timer, never succeeds to be processed).

Let me know if you need help to modify your automation to use a State Trigger.

Hi @123 thanks for the reply. I dont appear to have binary_sensors for the snoff pir’s. Is there a way to create them? I really like the idea of just seeing and on or off as i do with my wifi pir’s. If you could assist it would be greatly appreciated.

Which integration are you using for the WIFI PIR sensors?

It can’t be Z2M because that’s for Zigbee devices.

Sorry I mistyped there the WIFI PIR sensors work fine it the Zigbee ones that I had to change in the latest update of Z2M that has brought me to this situation, Apologies my mistake.

@123 I have found the binary Sensor you mentioned for occupancy on the PIR sensor so swapped to using that rather than the MQTT trigger and is now working like a charm. Thanks again for the advise is it was that put on the correct trail to find it and learn about it on the way.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.