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 %}
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
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.
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 ^^
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 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.