Hello,
sadly i need some more help with MQTT sensors
This is an example of my mqtt data (as you can see, i have a json inside a json field)
{"timestamp": 1686941544.2616673,
"data":
"{\"1\":
{\"roomId\": 1, \"roomName\": \"Sala\", \"actualTemp\": 27.6, \"setTemp\": 20.0, \"valveAperture\": 0, \"status\": true},
\"2\":
{\"roomId\": 2, \"roomName\": \"Cucina\", \"actualTemp\": 28.0, \"setTemp\": 19.0, \"valveAperture\": 0, \"status\": false},
\"3\":
{\"roomId\": 3, \"roomName\": \"Bagnetto\", \"actualTemp\": 27.1, \"setTemp\": 19.0, \"valveAperture\": 0, \"status\": false},
\"4\":
{\"roomId\": 4, \"roomName\": \"Bagno\", \"actualTemp\": 27.2, \"setTemp\": 19.0, \"valveAperture\": 0, \"status\": false},
\"5\":
{\"roomId\": 5, \"roomName\": \"Camera\", \"actualTemp\": 27.4, \"setTemp\": 19.5, \"valveAperture\": 0, \"status\": false},
\"6\":
{\"roomId\": 6, \"roomName\": \"Studio\", \"actualTemp\": 27.6, \"setTemp\": 19.0, \"valveAperture\": 0, \"status\": false}}
"}
Now i’d like to create 6 sensors, one for each room, where the value is the actual temp and as attributes i’d have valve aperture, target temp and the status.
I tried with something like that:
#temperatures
- name: "Temperature Room 1"
state_topic: massi/sensors/roomTemp
value_template: "{{ value_json.data[1].actualTemp }}"
suggested_display_precision: 1
unit_of_measurement: "°C"
json_attributes_topic: massi/sensors/roomTemp
json_attributes_template: >
{ "targetTemp": {{ value_json.data[1].setTemp }},
"valveAperture": {{ value_json.data[1].valveAperture }},
"statusError": {{ value_json.data[1].status }}
}
obviously nothing works The point - i think - is that the nested json is not parsed. Indeed i get this error:
Template variable warning: 'str object' has no attribute 'actualTemp' when rendering '{{ value_json.data[1].actualTemp }}'
but i’ve not understood how to get this parsed.
I’d also like to add:
- how can i set the unit of measurement of attributes?
- are also attributes logged and graph-ed?
- do you see anything very very wrong in what i’m trying to do?
Thanks