[Solved] Template sensor to read dynamically nested attribute information

I want to read i.e. precipitation_probability from todays date (or always first set after forecast) fromt the following sensor:

temperature: 30
humidity: 36
pressure: 1019.1
wind_bearing: 224
wind_speed: 9.3
visibility: 42.9
attribution: Data provided by Deutscher Wetterdienst (DWD)
forecast: 
- datetime: '2020-09-15'
  condition: sunny
  temperature: 30.3
  templow: 17
  precipitation: 0
  precipitation_probability: 2

I have some troubles finding the right code for a template sensor to read the probabilty and precipitation from the foreast and can find any documentation for HAss to read from a nested sensor with a dynamic date as the identifier to match. Can someone point me in the right direction? Thanks!

Is the lates forecast always the first entry in the forecast: section?

Then it should work like this:

state_attr('sensor.xyz', 'forecast')[0]['precipitation_probability']

If it is not always the first entry, I don’t know of any easy way (I know how to do it in python with AppDaemon) to do this.

This should always match todays date (if it exists)

{% set date = now().stftime('%Y-%m-%d') %}
{% set item = state_attr('sensor.xyz', 'forecast') | selectattr('datetime','eq', date) | first %}
{{ item.precipitation_probability if item else 'unknown' }}
1 Like

Cool, whenever I see your solutions, I say to myself “maybe you should start doing more things with HA than AppDaemon”.

I’ve been thinking about moving back but I’d need to make some new integrations. Appdaemon is great but I don’t know if it’s needed anymore. It really depends on the automation I guess.

What do you mean with this? Sorry, I don’t understand.

Currently I’m still doing all automations in AppDaemon, however with all the new features (like global variables) that will get introduced in 0.115, I need to rethink this as some of these currently missing features were the reason to use AppDaemon in the first place. However, one of the main uses for AppDaemon for me is to learn Python, in the hope to contribute some code to HA one day.

I have a few appdeamon apps that could be replaced with a built in integration. I’ve just been too lazy to make them. I’ve got one on my list that’s a pretty high up WTH, so I might be doing this pretty soon.

You could probably add stuff now. It’s not that hard and you don’t need to know the core that well. The hardest part is knowing how to use the API. If you can learn appdeamon, you can build an integration.

Thanks for the feedback - as it is always the first item on the list this seems easier to me.

Now I have a formatting problem. This code shows the correct value in the Template Editor in Dev Tools:

{{ state_attr('weather.xyz, 'forecast')[0]['temperature'] }}

If I try to use it as a template sensor it does not:

- platform: template
  sensors: 
    DWD_today_max_temp:
      friendly_name: 'DWD Today Max Temp'
      device_class: temperature
      unit_of_measurement: '°C'
      icon_template: 'mdi:thermometer-high'
      value_template: '{{ state_attr('weather.xyz', 'forecast')[0]['temperature'] }}'

Error Message of HAss File Editor:

can not read an implicit mapping pair; a colon is missed at line 115, column 99:
… forecast’)[0][‘temperature’] }}’
^

Any ideas why the value_template needs different code than the template editor?

You need double quotes outside the curly brackets, like this:

"{{ state_attr('weather.xyz', 'forecast')[0]['temperature'] }}"
1 Like

Otherwise this happens:

Ahh, thanks to both of you for explaining, works!