diff --git a/source/_integrations/mqtt.markdown b/source/_integrations/mqtt.markdown index 6e8ce25043a..2df5701e1b4 100644 --- a/source/_integrations/mqtt.markdown +++ b/source/_integrations/mqtt.markdown @@ -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`: