MQTT trigger using template to "payload" option

Hi!

Please, pretty please, add something to the “Payload” option, in order to customize it somehow, it is too not usable in this moment.
For example, I want to make a trigger which will trigger only if the payload contains a specific string, I don’t want to trigger only by the entire payload received. It could work with a part of it, detecing some strings or even using a more complex template.

The workaround which all uses, by adding a condition to the automation, it is not the perfect fitt, because the “trances” will not be usable anymore. The trigger will fire everytime, no matter that condition, the condition will only block the automation to continue, but the trigger already has been fired…

For some situations, like mine, not even a MQTT sensor will help, thats why I think that customizing the payload from the trigger, will be the perfect solution.

Thank you!

You can use MQTT Trigger’s value_template option to do that.

1 Like

For this payload:
{"ZbReceived":{"Bathroom_Ambient":{"Device":"0x59C2","Name":"Bathroom_Ambient","EF00/0202":5,"Endpoint":1,"LinkQuality":45}}}
I done it and it is working, with this trigger:

platform: mqtt
topic: tele/ZigBee_Bridge/59C2/SENSOR
payload: "Bathroom_Ambient"
value_template: "{{ value_json.ZbReceived.Bathroom_Ambient.Name }}"

Edit: But now… I still don’t get it to work for a similar situation… Same payload
{"ZbReceived":{"Bathroom_Ambient":{"Device":"0x59C2","Name":"Bathroom_Ambient","EF00/0202":5,"Endpoint":1,"LinkQuality":45}}}

How can I make the autmation trigger when “EF00/0202” it is detected into the payload? I cannot add value_json as value, because I don’t want to use the value. I want to trigger when the “variable” EF00/0202 it is detected into the payload, not the value of this one :frowning:

I tried with this, but it is not working

platform: mqtt
topic: tele/ZigBee_Bridge/59C2/SENSOR
payload: EF00/0216
value_template: "{{ value_json.ZbReceived.Bathroom_Ambient }}"

Edit2: I think I got it, but I don’t understand how… This is what I have done as trigger

platform: mqtt
topic: tele/ZigBee_Bridge/59C2/SENSOR
value_template: "{{ 'EF00/0216' in value_json['ZbReceived']['Bathroom_Ambient'] }}"

So… Is this possible? To remove the payload and only let the value_template? Is ok like this?

Edit3: I don’t think it is working properly… :expressionless:
Edit4: Nope, not working… Any idea how to achieve this? :frowning:

The following value_template reports ‘yes’ if the received JSON payload contains the key named ‘EF00/0202’ otherwise it reports ‘no’.

platform: mqtt
topic: tele/ZigBee_Bridge/59C2/SENSOR
payload: "yes"
value_template: >
  {{ iif(value_json['ZbReceived']['Bathroom_Ambient']['EF00/0216'] is defined, 'yes', 'no') }}

Did my latest suggestion meet your requirements or does it require additional enhancement?

No, I am still trying with your example but no luck, it is not triggering :slight_smile:

I think this is working

platform: mqtt
topic: tele/ZigBee_Bridge/59C2/SENSOR
payload: "yes"
value_template: |
  {% if value_json['ZbReceived']['Bathroom_Ambient']['EF00/0216'] is defined %}yes{% endif %}

I think now I understand how this payload it is working…

Ideally, value_template should report a value. The way you designed your template, it only reports a value when the key exists, otherwise it reports nothing.

value_template: |
  {% if value_json['ZbReceived']['Bathroom_Ambient']['EF00/0216'] is defined %}
    yes
  {% endif %}

Other than that, there’s no functional difference; both templates report ‘yes’ only when the desired key exists in the payload.

value_template: >
  {{ iif(value_json['ZbReceived']['Bathroom_Ambient']['EF00/0216'] is defined, 'yes', 'no') }}
1 Like

You are right. And your way is cleaner too, but I needed something more complex and I didn’t know how to do it with iif. Look this is my final trigger:

  - platform: mqtt
    topic: tele/ZigBee_Bridge/C877/SENSOR
    payload: trigger_template
    value_template: |
      {% set value = value_json['ZbReceived']['Living_Ambient']['EF00/0212']
      | multiply(0.1) | float | round(1) - 0.50 %}
      {% if ( (value is defined) and (is_number(value)) and
      ( ((states('variable.living_ambient_temperature') | float -
      value ) | abs | round(1) >= 1) or (as_timestamp(now()) -
      as_timestamp(states.variable.living_ambient_temperature.last_changed) |
      int > 1800)) ) %}trigger_template{% endif %}
    id: Temperature
    alias: Temperature

This automation will trigger only if the value is defined, it is a number and the value will be bigger than the last one with at least value of 1 or the time will pass more than 30 minutes. I made this trigger because my “stupid” sensor it is sending mqtt data 2-3 times per second. Imagine how full is my bridge, with data :smiley: But it is ok, the bridge can handle it, I just wanted to not have the HA database too big and the automation to not run every second.

The first line of your template uses the value of the ‘EF00/0212’ key before checking if the key is defined.

    value_template: |
      {% set value = value_json['ZbReceived']['Living_Ambient']['EF00/0212']
      | multiply(0.1) | float | round(1) - 0.50 %}
      {% if ( (value is defined) and ...

You should confirm the key is defined before attempting to use its value.

I understand. I will modify it now, thank you! So… first (if defined), than set value… :smiley: