I’m trying to extract values from a complex nested attribute structure and this thread is as close as I could get to what I need but not quite yet.
I’m using Solcast integration which exposes among others
sensor.solcast_forecast_tomorrow
which has as state tomorrow’s estimate in kWh and as attribute a lot of related information namely the hourly forecasts (the paste below is truncated to the first 9 hours)
resource_id: my_secret_resource_id
hourly:
- '00:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '01:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '02:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '03:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '04:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '05:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '06:00':
pv_estimate: 0
pv_estimate10: 0
pv_estimate90: 0
- '07:00':
pv_estimate: 125.4
pv_estimate10: 14.3
pv_estimate90: 302.3
- '08:00':
pv_estimate: 1476.3
pv_estimate10: 162.7
pv_estimate90: 4159.4
- '09:00':
pv_estimate: 4292
pv_estimate10: 525.1
pv_estimate90: 10227.4
I want to extract the pv_estimate value for a given hour using a template but after many attempts can’t figure out how to do it.
I’ve tried
{{ state_attr('sensor.solcast_forecast_tomorrow','hourly')[9] }}
which gives me for the 9th hour
{'09:00': {'pv_estimate': 4292.0, 'pv_estimate10': 525.1, 'pv_estimate90': 10227.4}}
but after that I’m lost and can’t get any further to just extract the pv_estimate value.
This is a dictionary within a dictionary and I would know how to do it if it was JSON but I can’t figure out how to do it when it is within an attribute.
Any help or pointers appreciated.
PS: I can use a REST sensor and query the solcast API directly and template the JSON output to get what I want. But that means I have both the Solcast integration which is convenient because, well, integrated, and my own REST sensor. That’s messy and doubles the number of Solcast API calls. So I’m looking for a more elegant solution using the sensor attributes which contain the data I want