MQTT and JSON template

1,769 views
Skip to first unread message

Tomas Jan

unread,
Jan 4, 2016, 4:30:00 PM1/4/16
to Home Assistant Dev
Hello All


I have question about template with JSON values

via MQTT I receive data like this:
{'type': 'temperatures', 'data': [{'name': 'bedroom', 'temp': 18.25}, {'name': 'bathroom', 'temp': 22.19}, {'name': 'kitchen', 'temp': 85.0}}


How to extract this values in MQTT template sensor. Can I use key name in template ?  These data are a random values order

I have checked like this without success:
value_template: {{ value_json.data.name['bedroom'].temp }}
value_template
: {{ value_json.data.name[\'
bedroom\'].temp }}



Second question is about configuration.yaml. Can I use multiple sensors from different topics like this ?
sensor:
  platform: mqtt
  state_topic: "mqtt_sensor"
    name: "bedroom sensor"
      unit_of_measurement: "°C"
      value_template: {{ value_json.data.name['bedroom'].temp }}
    name: "kichen sensor"
      unit_of_measurement: "°C"
      value_template: {{ value_json.data.name['kitchen'].temp }}
  state_topic: "mqtt_dispatcher"
    name: "sensor 1"
      unit_of_measurement: "%"
      value_template: {{ value_json.data.name['sensor 1'].temp }}
    name: "sensor 2"
      unit_of_measurement: "%"
      value_template: {{ value_json.data.name['sensor 2'].temp }}



Paulus Schoutsen

unread,
Jan 5, 2016, 4:11:29 PM1/5/16
to home-assi...@googlegroups.com
The problem here is that the key 'data' holds an array. Array indices can only be accessed through an index number. Try this instead:

value_json.data.0.temp


--
You received this message because you are subscribed to the Google Groups "Home Assistant Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to home-assistant-...@googlegroups.com.
To post to this group, send email to home-assi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/home-assistant-dev/b2835827-32ce-4587-9262-ecd8c3765e41%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
It's nice to be important but it's more important to be nice.
Reply all
Reply to author
Forward
0 new messages