mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-25 18:26:56 +00:00
Add example for JSON-formatted payload
This commit is contained in:
parent
5579effea9
commit
3984d1674c
@ -10,7 +10,7 @@ footer: true
|
|||||||
logo: mqtt.png
|
logo: mqtt.png
|
||||||
---
|
---
|
||||||
|
|
||||||
The MQTT component will register the service `publish` which allows publishing messages to MQTT topics. There are two ways of specifying your payload. You can either use `payload` to hard-code a payload or use `payload_template` to specify a [template](/topics/templating/) that will be rendered to generate the payload.
|
The MQTT component will register the service `mqtt.publish` which allows publishing messages to MQTT topics. There are two ways of specifying your payload. You can either use `payload` to hard-code a payload or use `payload_template` to specify a [template](/topics/templating/) that will be rendered to generate the payload.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@ -19,10 +19,20 @@ The MQTT component will register the service `publish` which allows publishing m
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
{% raw %}
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"topic": "home-assistant/light/1/state",
|
"topic": "home-assistant/light/1/state",
|
||||||
"payload_template": "{% raw %}{{ states('device_tracker.paulus') }}{% endraw %}"
|
"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:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"topic": "home-assistant/light/1/state",
|
||||||
|
"payload":"{\"Status\":\"off\", \"Data\":\"something\"}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user