Automation trigger mqtt platform with value template for payload?

Hi, it’s possible to use a “value_template” in automation with mqtt trigger platform?
I have a topic with json values and I cannot use payload directly.

This doesn’t work:

trigger:

  • platform: mqtt
    topic: tele/sonoffbridge/RESULT
    payload: ‘{ “RfReceived.RfKey”: “3” }’

Thank you

1 Like

I think you have to use a template condition for this.

Leave the payload of the mqtt trigger to trigger on every state change.
Use the condition to test for the key in trigger.to_state.state.

Now I have completly understand you suggestion, thank you! :slight_smile:

- id: pulsante_marty
  alias: Pulsante Marty ramen
  trigger:
  - platform: mqtt
    topic: "tele/sonoffbridge/RESULT"
  condition:
    condition: template
    value_template: "{{ trigger.payload_json['RfReceived']['RfKey'] == 3 }}"
  action:
  - service: switch.toggle
    data_template: { "entity_id": "switch.marty_ramen" }
3 Likes

I used
value_template: ‘{{ trigger.payload_json.RfReceived.Data == “STRING” }}’

1 Like

Hi Everyone,

I need your help in parsing an MQTT message. I have this sensor that occationally sends a null value and I need to turn on a switch if that happens.

{“Time”:“2020-05-29T17:01:32”,“AM2301”:{“Temperature”:null,“Humidity”:null,“DewPoint”:null},“TempUnit”:“C”}

I have tried this but it doesn’t work. Can you let me know what is wrong?

  • id: ‘1590768729127’
    alias: sample
    description: ‘’
    trigger:
    • platform: mqtt
      topic: tele/tasmota-02/SENSOR
      condition:
    • condition: template
      value_template: ‘{{ trigger.payload_json.AM2301[0].Temperature == null }}’
      action:
    • device_id: cc6eb9c3955e41d1a7d2723c680b6562
      domain: switch
      entity_id: switch.85605775840d8e9da84c
      type: turn_on

Thanks!

Please format your code correct

Click on the </>, and then put your code between the two times ```

In developer tools -> template

{% set my_test_json = {“Time”:“2020-05-29T17:01:32”,“AM2301”:{“Temperature”:null,“Humidity”:null,“DewPoint”:null},“TempUnit”:“C”} %}

The temperature is {{ my_test_json.AM2301.Temperature == null}}

Thanks @francisp. Works perfectly in the template editor. I’ll try this out.

@francisp, I tried implementing this as a trigger to turn off a switch but for some reason, it doesnt work. Is there something missing in the action?

Trigger Code:

- id: '1590768729127'
  alias: sample
  description: ''
  trigger:
  - platform: mqtt
    topic: tele/tasmota-02/SENSOR
  condition:
  - condition: template
    value_template: '{{ trigger.payload_json.AM2301.Temperature == null }}'
  action:
  - device_id: cc6eb9c3955e41d1a7d2723c680b6562
    domain: switch
    entity_id: switch.85605775840d8e9da84c
    type: turn_off

MQTT Transition to null: