Json / Templating Help

The darksky weather component provides forecast data.

[{'datetime': '2019-09-19T00:00:00', 'temperature': 83, 'templow': 63, 'precipitation': None, 'wind_speed': 4.01, 'wind_bearing': 163, 'condition': 'partlycloudy'}, {'datetime': '2019-09-20T00:00:00', 'temperature': 85, 'templow': 67, 'precipitation': 0.1, 'wind_speed': 4.31, 'wind_bearing': 161, 'condition': 'partlycloudy'}, {'datetime': '2019-09-21T00:00:00', 'temperature': 78, 'templow': 68, 'precipitation': 0.1, 'wind_speed': 12.1, 'wind_bearing': 193, 'condition': 'rainy'}, {'datetime': '2019-09-22T00:00:00', 'temperature': 73, 'templow': 54, 'precipitation': 1.4, 'wind_speed': 11.16, 'wind_bearing': 219, 'condition': 'rainy'}, {'datetime': '2019-09-23T00:00:00', 'temperature': 73, 'templow': 54, 'precipitation': None, 'wind_speed': 7.77, 'wind_bearing': 267, 'condition': 'sunny'}, {'datetime': '2019-09-24T00:00:00', 'temperature': 75, 'templow': 62, 'precipitation': 0.1, 'wind_speed': 7.2, 'wind_bearing': 200, 'condition': 'rainy'}, {'datetime': '2019-09-25T00:00:00', 'temperature': 71, 'templow': 52, 'precipitation': None, 'wind_speed': 12.66, 'wind_bearing': 244, 'condition': 'rainy'}, {'datetime': '2019-09-26T00:00:00', 'temperature': 68, 'templow': 56, 'precipitation': None, 'wind_speed': 9.72, 'wind_bearing': 288, 'condition': 'sunny'}]

I was hoping to use this data rather than adding the dark sky sensors and pull the data into a notification.

How can I pull temp low for example for today (2019-09-19) from the json string?

If i play with it in templates and the order is always the same:

{% set value_json = [{'datetime': '2019-09-19T00:00:00', 'temperature': 83, 'templow': 63, 'precipitation': None, 'wind_speed': 4.01, 'wind_bearing': 163, 'condition': 'partlycloudy'}, {'datetime': '2019-09-20T00:00:00', 'temperature': 85, 'templow': 67, 'precipitation': 0.1, 'wind_speed': 4.31, 'wind_bearing': 161, 'condition': 'partlycloudy'}, {'datetime': '2019-09-21T00:00:00', 'temperature': 78, 'templow': 68, 'precipitation': 0.1, 'wind_speed': 12.1, 'wind_bearing': 193, 'condition': 'rainy'}, {'datetime': '2019-09-22T00:00:00', 'temperature': 73, 'templow': 54, 'precipitation': 1.4, 'wind_speed': 11.16, 'wind_bearing': 219, 'condition': 'rainy'}, {'datetime': '2019-09-23T00:00:00', 'temperature': 73, 'templow': 54, 'precipitation': None, 'wind_speed': 7.77, 'wind_bearing': 267, 'condition': 'sunny'}, {'datetime': '2019-09-24T00:00:00', 'temperature': 75, 'templow': 62, 'precipitation': 0.1, 'wind_speed': 7.2, 'wind_bearing': 200, 'condition': 'rainy'}, {'datetime': '2019-09-25T00:00:00', 'temperature': 71, 'templow': 52, 'precipitation': None, 'wind_speed': 12.66, 'wind_bearing': 244, 'condition': 'rainy'}, {'datetime': '2019-09-26T00:00:00', 'temperature': 68, 'templow': 56, 'precipitation': None, 'wind_speed': 9.72, 'wind_bearing': 288, 'condition': 'sunny'}] %}
{{ value_json[0].templow }}

for today

{{ value_json[1].templow }}

for tomorrow

This is not really JSON, just a Python list of dictionaries.

{{ states.weather.dark_sky.attributes.forecast[0].templow }}

That’s assuming the first entry is always for today. If not you can still do it but it gets a bit more involved.

Awesome, thanks guys.

@VDRainer I wasn’t aware that you could input a dataset like that for use in the template editor. That will be super useful in the future. Thanks!

thanks @pnbruckner, I’ve only now found out we can write that as

{{ state_attr('weather.dark_sky','forecast')[0].templow }}

also. Always wondered and you now made me find it… cool!

In this particular case there’s no real advantage to write it using the state_attr function. You can, but there’s no advantage. If states.weather.dark_sky.attributes.forecast doesn’t exist or is None then either way will result in an error.

noted. I am just glad I finally figured it out…and as a personal thing, I like the consistency with my other templates for it to be written like this, with the exception of the last_changed templates that aren’t attributes and need the full states to be written verbose.

1 Like