Read Data from state attribute

Hi,
I have a rest sensor reading data from a website. Now I have trouble to extract the data from the state attribute.

- platform: rest
  scan_interval: 3600
  name: "Hexal Pollenflug"
  resource: http://www.allergie.hexal.de/pollenflug/xml-interface-neu/pollen_de_7tage.php?plz=24111
  json_attributes_path: "$..datasets.pollendaten"
  json_attributes:
    - pollenbelastungen
  value_template: "{{ value_json.last_update }}"

The attribute is showing data like:

pollenbelastungen: 
- '@tag': '0'
  pollen:
    - '@name': Ambrosia
      '@belastung': '0'
    - '@name': Ampfer
      '@belastung': '0'
    - '@name': Beifuß
      '@belastung': '0'
    - '@name': Birke
      '@belastung': '0'
    - '@name': Buche
      '@belastung': '0'
    - '@name': Eiche
      '@belastung': '0'
    - '@name': Erle
      '@belastung': '0'
    - '@name': Esche
      '@belastung': '0'
    - '@name': Gräser
      '@belastung': '0'
    - '@name': Hasel
      '@belastung': '1'
    - '@name': Pappel
      '@belastung': '1'
    - '@name': Roggen
      '@belastung': '0'
    - '@name': Ulme
      '@belastung': '0'
    - '@name': Wegerich
      '@belastung': '0'
    - '@name': Weide
      '@belastung': '0'

My Sensor:

- platform: template
  sensors:
    hexal_pollen_today:
      icon_template:  "mdi:tree-outline"
      friendly_name: "Heute"
      value_template: >-
        {% set today_state = state_attr('sensor.hexal_pollenflug', 'pollenenbelastung')["@tag"]%}

I get no data.
any idea?

BR
Det

This should be {{ and }} on the other side.
Then you need to remove

Because that is only when you need to create a variable.

Lastly i believe you need to replace

With pollen

Giving you

{{ state_attr('sensor.hexal_pollenflug', 'pollenenbelastung')["pollen"]}}

You could test it in developer tools > templates first to make sure it works

Thank you for reply,
I tested with your changes, but with no success:

UndefinedError: 'list object' has no attribute 'pollen'

I already changed pollenenbelastung to pollenenbelastungen

Ok… you need a zero also.

{{ state_attr('sensor.hexal_pollenflug', 'pollenbelastungen')[0]["pollen"]}}

This is confirmed to be working

@Det69: firstly, you’ll get errors in your log for your value_template, as there’s no last_update in the returned data. Just use:

value_template: "{{ now() }}"

For the @tag attribute (as per your original template attempt), you need:

{{ state_attr('sensor.hexal_pollenflug', 'pollenbelastungen')[0]['@tag'] }}

But I don’t think that’s what you want. The output appears to be a set of pollen counts by plant species by day. The above template will always return 0. I guess you’re trying to get the pollen species list for today, which @Hellis81’s template does assuming the list is returned in day order — however, it exceeds the 255-character limit for a sensor state.

Here’s the structure of the pollenbelastungen data, cut down to just show two days and one plant species:

[
  {"@tag": "0", "pollen": [{"@name": "Ambrosia", "@belastung": "0"}, ...]},
  {"@tag": "1", "pollen": [{"@name": "Ambrosia", "@belastung": "0"}, ...]},
  ...
]

Playing about a bit:

{% set pl = (state_attr('sensor.hexal_pollenflug', 'pollenbelastungen')
             |selectattr('@tag','eq','0')|first)['pollen'] %}
{% set ns = namespace(pd={},total=0) %}
{% for p in pl %}
{% set ns.pd = dict(ns.pd, **{p['@name']: p['@belastung']|int}) %}
{% set ns.total = ns.total + p['@belastung']|int %}
{% endfor %}
{{ dict(ns.pd, **{"Total": ns.total}) }}

gives a dictionary by species and a total for day 0 that should just about fit in a state’s size limit provided they don’t add more species.

What, exactly, do you want your sensor to return from the data?

Thank you very much both,
I am allergic and have an APP that gives me a prediction. This I would now like to have also in my HA
The original is looking like this:

I know, that HA ist not like the app, but having the data, I can play with the output.

Once more thank you very much.

Det69

@Det69 I am currently looking for a better source of pollen forecast. At the moment I have the pollen forecast from the DWD in HA, but it’s not really good. In previous years, I was very satisfied with the Hexal app, which you also use.
Have you made any progress in the meantime as far as the presentation is concerned? I saw in the link to the Hexal website that I only have to change the zip code to mine.
Do you have any screenshots of your implementation so that I can get some ideas? Or do you already have something ready to share?

Thank you!
BR

@Det69 @JeWe81 any news so far for that topic? I would like to add this type of data to my HA Dashboard but i struggle to find the correct format for it.

G