Trigger an automation using type MQTT

Hi! I’m unable to make this work… I have a zigbee doorlock in zigbee2mqtt. I want to trigger an automation when a specific user id unlock the door. It was working flawless with ZHA using zha_event but now that I’ve moved to Z2M, I’m unable to make this work with the trigger type MQTT. If anyone could give me hint/help that would be really appreciated!

Here’s the output of mosquitto_sub when I unlock the door with the user, as you can see the important part for me here is “action_user”:6 and the topic is "zigbee2mqtt/mqtt doorlock "

zigbee2mqtt/mqtt doorlock {"action":"unlock","action_source":0,"action_source_name":"keypad","action_user":6,"auto_relock_time":0,"battery":96,"linkquality":72,"lock_state":"unlocked","state":"UNLOCK","voltage":5600}
zigbee2mqtt/mqtt doorlock {"action":"","auto_relock_time":0,"battery":96,"linkquality":72,"lock_state":"unlocked","state":"UNLOCK","voltage":5600}
zigbee2mqtt/mqtt doorlock/action unlock

I’ve played with different combination of this automation without any luck, the trigger just doesn’t work…

alias: Unlock notification - UserX
description: ''
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/mqtt_doorlock
condition:
  - condition: template
    value_template: '{{ action_user == 6 }}'
action:
  - service: notify.mobile_app_cell
    data:
      message: Code 6 unlocked the door
      title: Code 6 unlocked the door
      data:
        ttl: 0
        priority: high
mode: single

I’ve read the documentation (MQTT Device Trigger - Home Assistant) but can’t see what I’m missing…

I think the problem is that your topic contains a space. The topic from the mqtt trigger needs to match exactly the topic sent by your mqtt broker, but you added an underscore.

Maybe that, but surely that you have to properly extract the value from the json

condition:
  - condition: template
    value_template: '{{ trigger.payload_json.action_user == 6 }}'
1 Like

That was my main concern, I tried replacing the space with a _, escaping before the space with a backslash, put int double quote, as is with the space. None of them worked … I’ll try the suggestion bellow. Thanks for your help!

Thanks for your suggestion! At least I can see a trace now but something’s wrong and I can’t see what/why. How can I debug those kind of things ?

In my trace I see this:

In first step Détails:

This node was not executed and so no further trace information is available.

Trace Timeline:

Stopped because of unknown reason “null” at 17 January 2022, 11:09:52 (runtime: 0.00 seconds)

I see two MQTT messages when I unlock,

zigbee2mqtt/mqtt doorlock {"action":"unlock","action_source":0,"action_source_name":"keypad","action_user":6,"auto_relock_time":0,"battery":96,"linkquality":63,"lock_state":"unlocked","state":"UNLOCK","voltage":5700}

And just after the doorlock resend another one without the action_user and unlocked data:

zigbee2mqtt/mqtt doorlock {"action":"","auto_relock_time":0,"battery":96,"linkquality":63,"lock_state":"unlocked","state":"UNLOCK","voltage":5700}

Is it possible the second message override the first and can’t trigger on that?

I think the best way is to rename your device in Zigbee2mqtt to mqtt_doorlock. That way, you will get messages on

zigbee2mqtt/mqtt_doorlock

The space wasn’t the problem, I confirmed it by changing the device name. This automation doesn’t work and I can’t understand why or how can I debug this…

alias: Unlock notification - User6
description: ''
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/DoorlockAvant
condition:
  - condition: template
    value_template: '{{ trigger.payload_json.action_user == ''6'' }}'
action:
  - service: notify.mobile_app_cell
    data:
      message: User 6 unlocked the door
      title: User 6 unlocked the door
      data:
        ttl: 0
        priority: high
mode: single
variables:
  slot: '{{ trigger.event.data.args.code_slot }}'

I still get the " This node was not executed and so no further trace information is available." or “Stopped because of unknown reason “null” at 18 January 2022, 07:53:10 (runtime: 0.00 seconds)”

I tried to create a sensor too, get the same result…

sensor:
  - platform: mqtt
    name: "User 6 door"
    state_topic: "zigbee2mqtt/DoorlockAvant"
    value_template: "{{ value_json.action_user == '6' }}"

The first time I typed the code, the sensor became False and never got true.

Here’s the state of the entity in dev tools:

Here’s a screenshot of mqtt explorer showing the topic I see related to the doorlock:

Here’s another thing I tried, I created a sensor, that way when this specific user enter it’s code the sensor turn true, otherwise it’s false.

  - platform: mqtt
    name: "User porte avant"
    state_topic: "zigbee2mqtt/DoorlockAvant"
    json_attributes_topic: "zigbee2mqtt/DoorlockAvant"
    value_template: "{{ value_json.action_user == 6 }}"

but I get the same result, when I enter the code, the state of the sensor seems to “reset” to false, but never become true…

Just to narrow it down:
If you leave the condition out, does it trigger ?

That’s a great idea. After removing the condition the automation seems to “trigger”, but it doesn’t really. By that I mean, there’s new traces but they all show the exact same messages I had before and action doens’t run:


And


I realized there was this piece of code in my automation:

variables:
  slot: '{{ trigger.event.data.args.code_slot }}'

that was literally stuck in my automation, I used this variables in my old automation with ZHA, I can’t remove it when I delete, save the yaml it come back… So I created a brand new automation with the exact same code minus the variables and this one work like a charm…

Thanks for your help, I’m not sure if I should open a bug or something because there’s definitely something not working as expected but I’m not sure I would be able to produce any good info/logs on that…