Hi All,
I’ve created a sensor that puts a JSON string inside the state of the sensor. I’m trying to create a template to display these values in a card. The content of the state attribute is like:
[{
"TargetDepartureTime": "10:56",
"Delay": "0"
}, {
"TargetDepartureTime": "11:06",
"Delay": "0"
}, {
"TargetDepartureTime": "11:46",
"Delay": "0"
}, {
"TargetDepartureTime": "11:56",
"Delay": "0"
}, {
"TargetDepartureTime": "12:06",
"Delay": "0"
}]
I’ve tested the JSON with JSONLint and it seems valid JSON.
When I open the template designer and enter the content below, the content is parsed correctly:
{% set ov_json = [{
"TargetDepartureTime": "10:56",
"Delay": "0"
}, {
"TargetDepartureTime": "11:06",
"Delay": "0"
}, {
"TargetDepartureTime": "11:46",
"Delay": "0"
}, {
"TargetDepartureTime": "11:56",
"Delay": "0"
}, {
"TargetDepartureTime": "12:06",
"Delay": "0"
}] %}
{% for value in ov_json %}
{% if value.Delay | int > 0 %}
{% set message = '- Delay: ' + value.Delay %}
{% endif %}
Departure: {{ value.TargetDepartureTime }}{{message}}
{% endfor %}
But when I change the ov_json
variable to states.sensor.tram_6__stop_9505.state
(this is the custom sensor) I get the output below:
Template:
{% set ov_json = states.sensor.tram_6__stop_9505.state | tojson %}
{% for item in ov_json %}
For item: {{ item }}
{% endfor %}
Result:
For item: "
For item: [
For item: {
For item: \
For item: "
For item: T
For item: a
For item: r
For item: g
For item: e
For item: t
For item: D
For item: e
For item: p
For item: a
For item: r, etc....
I’ve also got a hint to use | tojson
but this makes no difference…
The question is, did I do something wrong on coding the sensor, or am I missing something important here…
I do have to admit I’m a noob on templating, just started experimenting.