Hello, everyone,
I have a beginner’s question about cutting variables “to fit”.
I get the following values transmitted to a sensor via MQTT:
[{"ID":40561,"Name":"Value","ObjectIdent":"Value","VariableTyp":3,"VariableAction":52503,"VariableCustomAction":0,"VariableProfile":"","VariableCustomProfile":"","Value":"264.5885"}]
But I only need the last value after the last “Value” here, which is 264.5885.
What do I have to add to the configuration YAML now?
sensor:
- name: "Wasser"
state_topic: "mqttsync/symcon/Wasser"
unit_of_measurement: "m³"
value_template: "{{ value_json.Value }}"
Thanks and greetings Philip
Dujith
(Charles Evers)
2
In your configuration yaml shouldnt this be:
mqtt:
sensor:
- name: "Wasser"
state_topic: "mqttsync/symcon/Wasser"
unit_of_measurement: "m³"
value_template: "{{ value_json.Value }}"
Thanks for your answer, but if I use exact this configuration I get UNKNOWN as State.
Dujith
(Charles Evers)
4
What does
value_template: "{{ value }}"
return?
Edit:
Checking this out in the Developers Tools i can get the correct value from the string you posted. But only when removing the brackets [ ]
{% set value_json = {"ID":40561,"Name":"Value","ObjectIdent":"Value","VariableTyp":3,"VariableAction":52503,"VariableCustomAction":0,"VariableProfile":"","VariableCustomProfile":"","Value":"264.5885"} %}
{{ value_json.Value }}
Results in 264.5885
Hi,
now I tried
value_template: ‘{{ (value | from_json) }}’
but in the Log I get an Error:
Logger: homeassistant.helpers.template
Source: helpers/template.py:583
First occurred: 00:55:17 (1 occurrences)
Last logged: 00:55:17
Template variable warning: 'list object' has no attribute 'Value' when rendering '{{ value_json.Value }}'
HasQT
(Has)
7
That should be what you want including the square bracket [ ]
That’s from the earlier link.
{% set value_json = [{"ID":40561,"Name":"Value","ObjectIdent":"Value","VariableTyp":3,"VariableAction":52503,"VariableCustomAction":0,"VariableProfile":"","VariableCustomProfile":"","Value":"121.1211" }] %}
{{ value_json[0].Value | default(none) }}
Thank you very much, it worked exactly as you described. I actually just had to correct my spelling.