Template sensor: Filling state attributes via loop

Hi,

I am trying to create a Template sensor and fill the attributes from inside a loop.

Fo example:
I have this template, which is working in developer tools.
The base sensor is a sensor based in the HAFAS integration (HACS) for german pubic transport.

{% for connections in state_attr("sensor.bruchhausen_b_ettlingen_to_karlsruhe_hbf","connections") %}
{% for legs in connections.legs %}
          
{{ legs.origin }}
{{ legs.departure }}

{% endfor %}
{% endfor %}

The result looks like this:

Bruchhausen(b Ettlingen)
2024-12-22 11:38:00+01:00

Bruchhausen(b Ettlingen)
2024-12-22 12:38:00+01:00

Bruchhausen(b Ettlingen)
2024-12-22 13:38:00+01:00

Now, I want to store these values (and some more) in a template sensor (in the attributes).
I also tried KI (which often helps me with jinja ^^) but in this case, nothing worked how I need it :confused:

Can someone give me a hint how to get this working? I am not sure how to set state attributes correctly in jinja2

Tobias

edit:
I tried it like this, but thats not working

  - name: "Fahrplan Bruchhausen"
    state: "bla"
    attributes:
        {% for connections in state_attr("sensor.bruchhausen_b_ettlingen_to_karlsruhe_hbf","connections") %}
        {% for legs in connections.legs %}

        origin: "{{ legs.origin }}"
        departure: "{{ as_datetime(legs.departure).strftime("%H:%M") }}"
        arrival: "{{ as_datetime(legs.arrival).strftime("%H:%M") }}"

        {% endfor %}
        {% endfor %}

In Home Assistant, Jinja2 is used to produce a value that is assigned to a YAML option.

Your example doesn’t work because you’re attempting to use Jinja2 to generate YAML.


It may be possible to extract the data you want without using a for-loop. Post an example of the value contained in the sensor’s connections attribute. I’ll see if filters can be used to get the origin, arrival and departure data.

Hi,

It’s a small project for me to get a better comprehension for the templating engine.

My idea is, to display a shedule for the german public transport in my dashboard.
Because, it’s seems not possible to loop through in directly in the dashbaord, I tried to generate a new sensor, which is besser usable in the dashboard.

  • only necessary attributes
  • No nested attributes
  • Only a single connection in one sensor

My example from above, is only a try to put something inside the attributes and I totaly failed ^^

the data from the sensor looks like this.

origin: Bruchhausen Am Sang, Ettlingen
departure: "2024-12-22T21:28:00+01:00"
delay: "0:00:00"
destination: Karlsruhe Hbf
arrival: "2024-12-22T21:47:00+01:00"
delay_arrival: "0:00:00"
transfers: 1
duration: "0:19:00"
canceled: false
ontime: true
products: S 71
connections:
  - origin: Bruchhausen Am Sang, Ettlingen
    departure: "2024-12-22T21:28:00+01:00"
    delay: "0:00:00"
    destination: Karlsruhe Hbf
    arrival: "2024-12-22T21:47:00+01:00"
    delay_arrival: "0:00:00"
    transfers: 1
    duration: "0:19:00"
    canceled: false
    ontime: true
    products: S 71
    legs:
      - origin: Bruchhausen Am Sang, Ettlingen
        departure: "2024-12-22T21:28:00+01:00"
        platform: null
        delay: "0:00:00"
        destination: Bruchhausen(b Ettlingen)
        arrival: "2024-12-22T21:38:00+01:00"
        platform_arrival: null
        delay_arrival: "0:00:00"
        mode: walking
        name: null
        canceled: false
        distance: 420
        remarks: []
        stopovers: []
      - origin: Bruchhausen(b Ettlingen)
        departure: "2024-12-22T21:38:00+01:00"
        platform: "1"
        delay: "0:00:00"
        destination: Karlsruhe Hbf
        arrival: "2024-12-22T21:47:00+01:00"
        platform_arrival: "13"
        delay_arrival: "0:00:00"
        mode: train
        name: S 71
        canceled: false
        distance: null
        remarks:
          - Fahrradmitnahme begrenzt möglich
        stopovers:
          - Bruchhausen(b Ettlingen)
          - Ettlingen West
          - Karlsruhe Hbf
  - origin: Bruchhausen Am Sang, Ettlingen
    departure: "2024-12-22T22:06:00+01:00"
    delay: "0:00:00"
    destination: Karlsruhe Hbf
    arrival: "2024-12-22T22:49:00+01:00"
    delay_arrival: "0:00:00"
    transfers: 3
    duration: "0:43:00"
    canceled: false
    ontime: true
    products: RE 4745, RE 4740
    legs:
      - origin: Bruchhausen Am Sang, Ettlingen
        departure: "2024-12-22T22:06:00+01:00"
        platform: null
        delay: "0:00:00"
        destination: Bruchhausen(b Ettlingen)
        arrival: "2024-12-22T22:16:00+01:00"
        platform_arrival: null
        delay_arrival: "0:00:00"
        mode: walking
        name: null
        canceled: false
        distance: 420
        remarks: []
        stopovers: []
      - origin: Bruchhausen(b Ettlingen)
        departure: "2024-12-22T22:16:00+01:00"
        platform: "2"
        delay: "0:00:00"
        destination: Rastatt
        arrival: "2024-12-22T22:28:00+01:00"
        platform_arrival: "3"
        delay_arrival: "0:00:00"
        mode: train
        name: RE 4745
        canceled: false
        distance: null
        remarks:
          - Fahrradmitnahme begrenzt möglich
          - Fahrzeuggebundene Einstiegshilfe vorhanden
        stopovers:
          - Bruchhausen(b Ettlingen)
          - Malsch
          - Muggensturm
          - Rastatt
      - origin: Rastatt
        departure: "2024-12-22T22:28:00+01:00"
        platform: null
        delay: "0:00:00"
        destination: Rastatt
        arrival: "2024-12-22T22:28:00+01:00"
        platform_arrival: null
        delay_arrival: "0:00:00"
        mode: walking
        name: null
        canceled: false
        distance: null
        remarks: []
        stopovers: []
      - origin: Rastatt
        departure: "2024-12-22T22:34:00+01:00"
        platform: "4"
        delay: "0:02:00"
        destination: Karlsruhe Hbf
        arrival: "2024-12-22T22:49:00+01:00"
        platform_arrival: "14"
        delay_arrival: "0:00:00"
        mode: train
        name: RE 4740
        canceled: false
        distance: null
        remarks:
          - Fahrradmitnahme begrenzt möglich
          - Fahrzeuggebundene Einstiegshilfe vorhanden
        stopovers:
          - Rastatt
          - Karlsruhe Hbf
  - origin: Bruchhausen Am Sang, Ettlingen
    departure: "2024-12-22T22:46:00+01:00"
    delay: "0:00:00"
    destination: Karlsruhe Hbf
    arrival: "2024-12-22T23:02:00+01:00"
    delay_arrival: "0:00:00"
    transfers: 1
    duration: "0:16:00"
    canceled: false
    ontime: true
    products: RB 15921
    legs:
      - origin: Bruchhausen Am Sang, Ettlingen
        departure: "2024-12-22T22:46:00+01:00"
        platform: null
        delay: "0:00:00"
        destination: Bruchhausen(b Ettlingen)
        arrival: "2024-12-22T22:56:00+01:00"
        platform_arrival: null
        delay_arrival: "0:00:00"
        mode: walking
        name: null
        canceled: false
        distance: 420
        remarks: []
        stopovers: []
      - origin: Bruchhausen(b Ettlingen)
        departure: "2024-12-22T22:56:00+01:00"
        platform: "1"
        delay: "0:00:00"
        destination: Karlsruhe Hbf
        arrival: "2024-12-22T23:02:00+01:00"
        platform_arrival: "7"
        delay_arrival: "0:00:00"
        mode: train
        name: RB 15921
        canceled: false
        distance: null
        remarks:
          - Fahrradmitnahme begrenzt möglich
          - Fahrzeuggebundene Einstiegshilfe vorhanden
          - Laptop-Steckdosen
          - Klimaanlage
        stopovers:
          - Bruchhausen(b Ettlingen)
          - Ettlingen West
          - Karlsruhe Hbf
attribution: Provided by DB through HaFAS API
device_class: timestamp
icon: mdi:timetable
friendly_name: Bruchhausen Am Sang, Ettlingen to Karlsruhe Hbf

Consider using a flex-table-card to present data in a tabular form.
Then there will be probably no need to convert a source sensor into something different.

I think its not possible to filter elements in the flex-table-card.

  • Sometimes, the HAFAS API gives me values I want to remove
  • the train numbers doesn’t reflect the real numer written on the train, I will rename them

I think both is not possible directly in the flex table card because no template can be used in data option

Worth to try.

I already tried :wink:

And there is another problem.

I can have access to the first level, to the second level but not to the 3rd level of the provided data in the table. It’s not working.

origin: Bruchhausen Am Sang, Ettlingen <---- Access
....
connections:
  - origin: Bruchhausen Am Sang, Ettlingen
    departure: "2024-12-22T21:28:00+01:00" <------- Access
    ....
    legs:
      - origin: Bruchhausen Am Sang, Ettlingen
        departure: "2024-12-22T21:28:00+01:00" <------ No Access
        .....
      - origin: Bruchhausen(b Ettlingen)
        departure: "2024-12-22T21:38:00+01:00" <----- No Access
        ....

To clarify why I need that:
The Data include ALL steps from a train/bus stop to the Mainstation including all changes from one bus/train to another. These parts of the trip are the “legs” of the connection.
Because it’s possible to use the train oder the bus, there are sometimes walking parts in my shedule from the busstation to the trainstation.

  • I don’t want these walking parts in my table, so I have to filter them out of the dataset.
  • in the flex-table-card I was not able to get the data from the “legs”. I think because it’s a nested dictionary, to the 2nd layer it was possible.

Thats the reason because I want to create a new template sensor, only with the data I need and without nested dictionaries.

Probably because of a wrong code, that is why suggested to try & consult in the main flex-table-card thread (you will have to prepare a short MWE to allow other people to reproduce your case).

I already asked people who are very familar with the flex-table-card and I study tons of threads and the documentation.
It’s not possible to use templates directly in the flex-table-card config, thats the reason I try to create a template_sensor.

It is possible to extract data from attributes which have a complex structure like a list or a dictionary & perform any operations like math/string processing etc, but not gonna convince you.