Help Understanding MQTT Trigger payload and value_template

Hi all, I need some help understanding how to make an MQTT Automation trigger work.

I read the MQTT Automation trigger doc (link), and to my understanding, I am doing everything right.

I am trying to trigger an automation when an invalid code is an input in Alamo. Alarmo returns HA this information via MQTT:

Topic: alarm/event
Payload:

{
    "event": "INVALID_CODE_PROVIDED"
}

I got this payload from listening to the topic in HA, so I am getting this information.

In my automation, I have the following trigger:

platform: mqtt
topic: alarmo/state
id: alarmo_state_invalid_code
payload: INVALID_CODE_PROVIDED
value_template: "{{ value_json.event }}"

From what I could get from the documentation using the value_template: "{{ value_json.event }}" should extract the event key value from the JSON payload and compare it against the payload value INVALID_CODE_PROVIDED. However, that is not happening.


Full automation for reference:
It is rather long as I am integrating Alarmo and a ZHA PIN PAD. All the other triggers are working fine:

alias: "Alarmo - ZHA Alarm Panel to MQTT "
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: xx:xx:xx:xx:xx:xx:xx:xx
      args:
        arm_mode_description: Disarm
    id: disarm
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: xx:xx:xx:xx:xx:xx:xx:xx
      args:
        arm_mode_description: Arm_Day_Home_Only
    id: arm_home
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: xx:xx:xx:xx:xx:xx:xx:xx
      args:
        arm_mode_description: Arm_All_Zones
    id: arm_away
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: xx:xx:xx:xx:xx:xx:xx:xx
      args:
        arm_mode_description: Arm_Night_Sleep_Only
    id: arm_night
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_disarmed
    payload: disarmed
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_armed_home
    payload: armed_home
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_armed_away
    payload: armed_away
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_armed_night
    payload: armed_night
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_armed_arming
    payload: arming
  - platform: mqtt
    topic: alarmo/state
    id: alarmo_state_invalid_code
    payload: INVALID_CODE_PROVIDED
    value_template: "{{ value_json.event }}"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: disarm
        sequence:
          - service: mqtt.publish
            data:
              topic: alarmo/command
              payload_template: >-
                { "command": "DISARM", "code": "{{ trigger.event.data.args.code
                }}" }
      - conditions:
          - condition: trigger
            id: arm_home
        sequence:
          - service: mqtt.publish
            data:
              topic: alarmo/command
              payload_template: >-
                { "command": "ARM_HOME", "code": "{{
                trigger.event.data.args.code }}" }
      - conditions:
          - condition: trigger
            id: arm_away
        sequence:
          - service: mqtt.publish
            data:
              topic: alarmo/command
              payload_template: >-
                { "command": "ARM_AWAY", "code": "{{
                trigger.event.data.args.code }}" }
      - conditions:
          - condition: trigger
            id: arm_night
        sequence:
          - service: mqtt.publish
            data:
              topic: alarmo/command
              payload_template: >-
                { "command": "ARM_NIGHT", "code": "{{
                trigger.event.data.args.code }}" }
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: alarmo_state_disarmed
          - condition: not
            conditions:
              - condition: device
                device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                domain: alarm_control_panel
                entity_id: alarm_control_panel.front_door_alarm_pinpad_alarmcontrolpanel
                type: is_disarmed
        sequence:
          - device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: alarm_control_panel
            entity_id: alarm_control_panel.front_door_alarm_pinpad_alarmcontrolpanel
            type: disarm
            code: "00000"
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: alarmo
          - service: notify.notify
            data:
              title: Alarmo
              message: Home alarm Disarmed.
              data:
                tag: alarmo
      - conditions:
          - condition: trigger
            id: alarmo_state_armed_home
        sequence:
          - device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: alarm_control_panel
            entity_id: alarm_control_panel.front_door_alarm_pinpad_alarmcontrolpanel
            type: arm_home
            code: "00000"
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: alarmo
          - service: notify.notify
            data:
              title: Alarmo
              message: Home alarm Armed Home.
              data:
                tag: alarmo
      - conditions:
          - condition: trigger
            id: alarmo_state_armed_away
        sequence:
          - device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: alarm_control_panel
            entity_id: alarm_control_panel.front_door_alarm_pinpad_alarmcontrolpanel
            type: arm_away
            code: "00000"
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: alarmo
          - service: notify.notify
            data:
              title: Alarmo
              message: Home alarm Armed Away.
              data:
                tag: alarmo
      - conditions:
          - condition: trigger
            id: alarmo_state_armed_night
        sequence:
          - device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: alarm_control_panel
            entity_id: alarm_control_panel.front_door_alarm_pinpad_alarmcontrolpanel
            type: arm_night
            code: "00000"
          - service: notify.notify
            data:
              message: clear_notification
              data:
                tag: alarmo
          - service: notify.notify
            data:
              title: Alarmo
              message: Home alarm Armed Night.
              data:
                tag: alarmo
      - conditions:
          - condition: trigger
            id: alarmo_state_armed_arming
        sequence:
          - service: notify.notify
            data:
              title: Alarmo
              message: Home alarm is arming.
              data:
                tag: alarmo
      - conditions:
          - condition: trigger
            id: alarmo_state_invalid_code
        sequence:
          - service: notify.notify
            data:
              title: Alarmo
              message: An invalid code was used to arm or disarm the Home Alarm.
              data:
                tag: alarmo
    default: []
mode: single

EDIT:
Solution Explanation: I was using the wrong MQTT topic. The error event is returned at alarmo/event, not alarmo/status

I agree. I can’t see anything incorrect in what you have done.

Nothing looks wrong with the trigger however two things jump out at me.

  1. You have many many triggers but your automation has mode: single. That means if any two of those triggers happen extremely close together then only one will work. Whichever one came second will be dropped and ignored. Any chance that is what’s happening to you? Maybe one of those zha events is also firing at the same time and causing the alarmo one to be ignored?

  2. All your MQTT triggers are looking at the same topic (alarmo/state). But only the one assumes the payload is JSON, all the others assume it is simply text like armed_away, armed_home, etc. That’s not invalid it just seemed odd. In my experience things either talk in JSON or don’t rather then mix and match. I just wanted to confirm it’s correct that the alarmo only sends a JSON payload that one time to that topic.

2 Likes

You could use the following:

  - platform: event
    event_type: alarmo_failed_to_arm
    event_data:
      reason: invalid_code
1 Like

Thanks for the reply @CentralCommand!

You just made me realize that I did the dumbest mistake ever…I forgot to change the MQTT topic.
I get the JSON at alarmo/event not alarmo/state.

alarmo/state just returns plain text.

1 Like