hello.
is there anyone that can help me to get the data right.
i have mqtt sensors that give me [{“temp”:124}] can someone help HOW to get just 12,4 out of this string ?
Post the MQTT Sensor’s configuration.
here is the sensor config
- name: “Panntemp”
state_topic: “panntemp”
payload_available: “temp”
unique_id: “plc.dl240.in.panna_inne”
unit_of_measurement: ‘°C’
qos: 0
Go to Developer Tools > States, find sensor.panntemp
and tell me its current state
value.
Panntemp [{“temp”:426},{“temp”:424},{“temp”:426}] unit_of_measurement: °C friendly_name: Panntemp
...
unique_id: "plc.dl240.in.panna_inne"
value_template: "{{ value | from_json[0].temp }}"
unit_of_measurement: "°C"
qos: 0
or
...
unique_id: "plc.dl240.in.panna_inne"
value_template: "!regex_replace '\"temp\":(\d+)', '\\1' {{ value }}"
unit_of_measurement: "°C"
qos: 0
the first example result in : Invalid config for [mqtt]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘[’) for dictionary value @ data[‘mqtt’][‘sensor’][1][‘value_template’]. Got ‘{{ value | from_json[0].temp }}’. (See /config/configuration.yaml, line 12).
the second example result in : Error loading /config/configuration.yaml: while scanning a double-quoted scalar
in “/config/mqtt.yaml”, line 64, column 24
found unknown escape character ‘d’
in “/config/mqtt.yaml”, line 64, column 52
Add this line to the sensor’s configuration.
value_template: '{{ (value | from_json)[0].temp }}'
YES it works!!! many many thanks
You’re welcome!
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.
For more information about the Solution tag, refer to guideline 21 in the FAQ.
hi again 123. do you think you can help me with this also?
i also have some binary_sensors the same way.
data looks like this [{“data”:“true”}] i like to get the true or false value out.
binary_sensor:
- name: "burner"
state_topic: "burner"
unique_id: "plc.dl240.in.x32_brannare_till"
value_template: "{{ value_json[0].burner[1].temperatur }}" #dont work
qos: 0
and current state of sensor.burner “unknown”
If the received value is this:
[{"data":"true"}]
then I suggest trying the following template:
value_template: '{{ (value | from_json)[0].data | bool }}'
that dont work.
received value is this:
[{“data”:true}]
and current state of sensor.burner “unknown”
- name: "burner"
state_topic: "burner"
unique_id: "plc.dl240.in.x32_brannare_till"
value_template: "{{ iif((value | from_json)[0].data | bool, 'ON', 'OFF') }}"
qos: 0
YES again you did it it works many many thanks man!!!