Your example of using attributes
helped me a lot. I messed with your scenario and learned a ton more. The Jinja documentation linked in HA Developer Tools page tells why you’re getting back single character with each index reference: List filter–Convert the value into a list. If it was a string the returned list will be a list of characters.
For some reason your attributes’ value_template
is being overridden and returning a string rather than a list created by the split method. Possible bug?
I figured out a workaround. Remove your attributes’ value_template
and let it be returned as a csv string. Then you can use a template with the split function to convert it to a list. By the way, Jinja to_json
didn’t work.
Here’s the template I used to verify that this worked.
{{ '========= RAW ==============' }}
{{
state_attr('sensor.inpo_praha_temperature', 'forecast_time')
}}
{{ '========= SPLIT TO LIST ==============' }}
{% set items =
state_attr('sensor.inpo_praha_temperature', 'forecast_time').split(",")
%}
{{ items[0] }}
{{ items[1] }}
{{ items[2] }}
{{ items[3] }}
{{ items | length }}
{{ '==========================' }}
Output:
========= RAW ==============
06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00,00:00,01:00,02:00,03:00,04:00,05:00,06:00
========= SPLIT TO LIST ==============
06:00
07:00
08:00
09:00
25
==========================
Something else interesting I learned is that HA Templates can use Python string methods in their dotted notation with your template variables in addition to the piped Jinja filters. That will come in handy to me.
Example Templates:
{{ 'raw: ' + states('sun.sun') }}
{{ 'Jinja filter: ' + states('sun.sun') | upper}}
{{ 'Python method: ' + states('sun.sun').upper() }}
Output:
raw: below_horizon
Jinja filter: BELOW_HORIZON
Python method: BELOW_HORIZON