Templates for json parsing

Hi,

I’m trying to start with the HA, but still I’m not able to make the stuff working the way I want (and what I understood from the examples).

Installation environment is Windows 7 64bit and standalone mosquitto MQTT broker (I’ll dig more details if needed), the installation is fresh (3 days old) so I assume it’s up to date…

I’m trying to setup a lights and switches via the MQTT. System itself is running fine, I can see messages running out of HASS to MQTT broker and I can even see my messages showig in the event log in so I assume the connection between hass and mosquitto is fine and even the topics are defined fine.

The problem starts with defining a template for the payload. If I keep the template empty/commented in the yaml configuration and the payload contain just the value, it works.

switch test1:
  platform: mqtt
  name: "test switch without value"
  state_topic: "home/test/switch2"
  command_topic: "home/test/switch2/set"
  payload_on: "ON"
  payload_off: "OFF"

while sending this to my mosquitto, the state is changed fine

mosquitto_pub.exe -t home/test/switch2 -m "ON"

What I’m unable to make working is the example of the RGB light
# Example configuration.yml entry
light:
platform: mqtt
name: “Office Light RGB”
state_topic: “office/rgb1/light/status”
command_topic: “office/rgb1/light/switch”
brightness_state_topic: “office/rgb1/brightness/status”
brightness_command_topic: “office/rgb1/brightness/set”
rgb_state_topic: “office/rgb1/rgb/status”
rgb_command_topic: “office/rgb1/rgb/set”
state_value_template: “{{ value_json.state }}”
brightness_value_template: “{{ value_json.brightness }}”
rgb_value_template: “{{ value_json.rgb | join(’,’) }}”
qos: 0
payload_on: “ON”
payload_off: “OFF”
optimistic: false

Could you please guide me, how the payload should look like to fit the templates?

I expected it to be for example

  • { state: ON }
  • { brightness: 128 }
  • { rgb: 10,20,30 }

but this just ends with parsing error in the event log of HASS.

If the templates from the MQTT light example is not correct for any reason, I would appreciate providing any working combination of json parsing template + the expected payload.

Thank you in advance!

Those payloads are not valid json. Wrap all strings in "

Thanks! Thats it!

C:\Program Files (x86)\mosquitto>mosquitto_pub.exe -t office/rgb1/light/status -m "{ ""state"": ""ON"" }"
C:\Program Files (x86)\mosquitto>mosquitto_pub.exe -t office/rgb1/brightness/status -m "{ ""brightness"":""10"" }"

is properly recieved by mosquitto (and then HA) as

office/rgb1/light/status { "state": "ON" }
office/rgb1/brightness/status { "brightness":"10" }

and HA properly parsed the json and changed the values of the object.

What about the RGB value? I have still some issues, but I guess it will be something really stupid again.

publish command
C:\Program Files (x86)\mosquitto>mosquitto_pub.exe -t office/rgb1/rgb/status -m "{ ""rgb"":""10,10,10"" }"
recieved json
office/rgb1/rgb/status { "rgb":"10,10" }
relevant config
rgb_state_topic: “office/rgb1/rgb/status”
rgb_value_template: “{{ value_json.rgb | join(’,’) }}”

once recieved by HA, following error is reported:
INFO:homeassistant.core:Bus:Handling <Event mqtt_message_received[L]: payload={ “rgb”:“10,10,10” }, topic=office/rgb1/rgb/status, qos=0>
ERROR:homeassistant.core:BusHandler:Exception doing job
Traceback (most recent call last):
File “C:\Users\slachrad\AppData\Local\Programs\Python\Python35-32\lib\site-packages\homeassistant\core.py”, line 808, in job_handler
func(arg)
File “C:\Users\slachrad\AppData\Local\Programs\Python\Python35-32\lib\site-packages\homeassistant\components\mqtt_init_.py”, line 160, in mqtt_topic_subscriber
event.data[ATTR_QOS])
File “C:\Users\slachrad\AppData\Local\Programs\Python\Python35-32\lib\site-packages\homeassistant\components\light\mqtt.py”, line 149, in rgb_received
templates’rgb’.split(’,’)]
File “C:\Users\slachrad\AppData\Local\Programs\Python\Python35-32\lib\site-packages\homeassistant\components\light\mqtt.py”, line 148, in
self._rgb = [int(val) for val in
ValueError: invalid literal for int() with base 10: ‘’

it’s not that crucial at the moment because the state RGB value is not reported in the interface at all (or is it?), but I think it would be beneficial to understand this for some future purposes.

Great!

Colors totally work and are reported in the UI.

You defined a template that is trying to join an array into a comma delimited string. However you’re not passing in an array in your JSON value but a string, this is obviously not going to work. Also, RGB is 3 colors…

And once again, I have to confirm, that once I provided properly formated json via MQTT, it just works

mosquitto_pub.exe -t office/rgb1/rgb/status -m "{ ""rgb"":[""255"",""128"",""128""] }"

the colour is properly shown in the UI (on the icon). It was my bad that I expected it reported directly on the colour picker.

Thank you for your help!