I’m publishing some MQTT messages to auto discover MQTT devices, but I’m struggling to create json payload with templates containing jinja code.
It seems like HA tries to parse my payload even though I would like to send it as a plain string
service: mqtt.publish
data:
qos: "0"
retain: false
topic: homeassistant/light/fimp_enocean_01A69E7E/light/config
payload: >
{
"schema": "template",
"brightness": true,
"brightness_scale": 100,
"command_topic": "pt:j1/mt:cmd/rt:dev/rn:enocean/ad:1/sv:out_lvl_switch/ad:01A69E7E",
"device": {
"identifiers": [
"enocean_01A69E7E"
],
"manufacturer": "EnOcean",
"model": "EnOcean FUD61",
"name": "Taklys TV-stue/gang"
},
"name": "Taklys TV-stue/gang",
"state_topic": "pt:j1/mt:evt/rt:dev/rn:enocean/ad:1/sv:out_lvl_switch/ad:01A69E7E",
"brightness_state_topic": "pt:j1/mt:evt/rt:dev/rn:enocean/ad:1/sv:out_lvl_switch/ad:01A69E7E",
"command_off_template": "{\n \"corid\": null,\n \"props\": {},\n \"serv\": \"out_lvl_switch\",\n \"src\": \"smarthome-app\",\n \"tags\": [],\n \"type\": \"cmd.binary.set\",\n \"uid\": \"123\",\n \"val\": false,\n \"val_t\": \"bool\",\n \"ver\": \"1\"\n}",
"command_on_template": "{\n \"props\":{},\n \"serv\":\"out_lvl_switch\",\n \"tags\":[]\n {%- if brightness -%}\n , \"type\":\"cmd.lvl.set\",\n \"val\":{{ (brightness / 2.55) | int }},\n \"val_t\":\"int\"\n {%- else -%}\n , \"type\":\"cmd.binary.set\",\n \"val\":true,\n \"val_t\":\"bool\"\n {%- endif -%}\n}",
"state_template": "{% if value_json.val %} on {% else %} off {% endif %}",
"brightness_template": "{% if value_json.val_t %}{{ (value_json.val * 2.55) | int }}{% endif %}",
"unique_id": "enocean_light_01A69E7E"
}
When I run this automation I get error message
Failed to call service mqtt.publish. Error rendering data template: UndefinedError: 'value_json' is undefined
Any ideas how can I escape it?