Condition / Trigger based on payload not working

After browsing the help section and documentation for hours I was not able to resolve my problem.

I am trying to do an automation with mqtt. When the value exceeds a certain value the action should start.
Therefore I created a trigger " When an MQTT message has been received" and a template condition. At first I tried to do it like this:

talias: set_wb_16a
description: ""
trigger:
  - platform: mqtt
    topic: pv/HM1500/ch0/P_AC
   
condition:
  - condition: template
    value_template: "\"{{trigger.payload_json > 300}}\""
action:
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: wallbox/999999/command
      payload: maxCurrent 16
mode: single

The trigger is working everytime when new data is presented but I am unable to test the condition because I get an error message:

Error occurred while testing condition
In 'template' condition: UndefinedError: 'trigger' is undefined

When I listen to that specific topic it gets data like this:

Message 14 received on pv/HM1500/ch0/P_AC at 12:41 PM:
523.6
QoS: 0 - Retain: false

So I thought maybe I have to pick “pv/HM1500/ch0/#” as a topic. Output from listening is the following:

Message 4 received on pv/HM1500/ch0/P_AC at 12:21 PM:
369
QoS: 0 - Retain: false

I tried to edit the yaml to the following but it still says the trigger is undefined:

alias: set_wb_16a
description: ""
trigger:
  - platform: mqtt
    topic: pv/HM1500/ch0/#
condition:
  - condition: template
    value_template: "\"{{trigger.payload_json.P_AC > 300}}\""
action:
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: wallbox/999999/command
      payload: maxCurrent 16
mode: single

Am I completely wrong?

Kind regards
Matthias

Maybe try:

"{{ trigger.payload_json.P_AC > 300 }}"

I don’t know where this double quotes and backslashes came but it doesn’t matter. It’s still not working with the condition looking like this:

condition: template
value_template: "{{trigger.payload_json.P_AC > 300}}"

When you use this:

trigger.payload_json

it implies the received payload is in JSON format. In other words, the payload isn’t merely a single value but consists of one or more key-value pairs. With that in mind, the following comparison is incorrect because trigger.payload_json isn’t a single value.

trigger.payload_json > 300

However, the error message indicates the received value is not in JSON format.

Message 4 received on pv/HM1500/ch0/P_AC at 12:21 PM:
369

Use this:

trigger.payload | float(0) > 300

Example:

alias: set_wb_16a
description: ""
trigger:
  - platform: mqtt
    topic: pv/HM1500/ch0/P_AC
condition:
  - condition: template
    value_template: "{{ trigger.payload | float(0) > 300 }}"
action:
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: wallbox/999999/command
      payload: maxCurrent 16
mode: single
1 Like

Thanks a lot it is working. Despite I get the same error when pressing “test” (the condition). The whole automation is working and also the action is taking place.

1 Like

That form of testing will fail when the condition refers to the trigger variable.

The trigger variable is defined only when the automation is triggered by one of its triggers. Otherwise, the trigger variable is undefined.

Reference: Testing your automation


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.