Improve example MQTT publish service using the YAML editor (#25242)

fixes undefined
This commit is contained in:
Jan Bouwhuis 2022-12-12 12:23:31 +01:00 committed by GitHub
parent 44bf43358c
commit 97e53836a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -782,11 +782,39 @@ payload_template: "{{ states('device_tracker.paulus') }}"
{% endraw %}
`payload` must be a string. If you want to send JSON then you need to format/escape it properly. Like:
`payload` must be a string.
If you want to send JSON using the YAML editor then you need to format/escape
it properly. Like:
```yaml
topic: home-assistant/light/1/state
payload: "{\"Status\":\"off\", \"Data\":\"something\"}"
payload: "{\"Status\":\"off\", \"Data\":\"something\"}"`
```
When using Home Assistant's YAML editor for formatting JSON
you should take special care if `payload` contains template content.
Home Assistant will force you in to the YAML editor and will treat your
definition as a template. Make sure you escape the template blocks as like
in the example below. Home Assistant will convert the result to a string
and will pass it to the MQTT publish service.
```yaml
service: mqtt.publish
data:
topic: homeassistant/sensor/Acurite-986-1R-51778/config
payload: >-
{"device_class": "temperature",
"name": "Acurite-986-1R-51778-T",
"unit_of_measurement": "\u00b0C",
"value_template": "{% raw %}{% raw %}{{ value|float }}{%{% endraw %} endraw %}",
"state_topic": "rtl_433/rtl433/devices/Acurite-986/1R/51778/temperature_C",
"unique_id": "Acurite-986-1R-51778-T",
"device": {
"identifiers": "Acurite-986-1R-51778",
"name": "Acurite-986-1R-51778",
"model": "Acurite-986",
"manufacturer": "rtl_433" }
}
```
Example of how to use `qos` and `retain`: