i’m practing with HA and jinja… i can built a list this way in a script
- variables:
mylist: "[
JINJA CODE HERE
]"
and use it with jinja filter or whatever. as it won’t be updated often i’d like to store anywhere…
is it possible?! i think not, right?! python script (i have to study python yet)?!
There are no global variables in Home Assistant that allow you to store and update data as a list. The closest thing is an input_select (which stores its data in an attribute as a list) but any changes you make to it are lost after a restart.
You may need to investigate custom integrations that provide the functionality of global variables. Here are two:
{% set dictstr = states('input_text.my_dict').replace("{","").replace("}","").replace("'","").split(", ") %}
{% set n = namespace(dict=[]) %}
{% for i in dictstr %}
{% set pair = i.split(":") %}
{% set n.dict = n.dict + [(pair[0], pair[1])] %}
{% endfor %}
{% set dictrestored = dict.from_keys(n.dict) %}
Not a very nice solution but at least it is working without custom integrations.
A separate entity for each variable. The entity’s state is limited to storing a string (like all entities) but its attributes can store other data types.
A single entity for multiple variables. Each variable is stored as a separate attribute (therefore its type can be dictionary, list, etc). Variables can be defined dynamically.
Thank you so much, that looks really interesting. Still a workaround though.
I need this in just one automation, so I will stick to the input_text parsing for now. An input_dictionary would be nice eventually.
A State Object has a state value, constrained to storing a string limited to 255 characters, and an attributes value which is a dictionary, able to store key-value pairs of any type.
All entities store their information, in the state machine, as a State Object. It’s just a matter of choosing something that allows you to easily modify/update its attributes (because that’s the only place able to store a dictionary). The two (sensor) integrations that allow for that are MQTT and Template which has the advantage of not requiring additional infrastructure (like an MQTT broker).
It would still have to store its information as a State Object so the only place it could put a dictionary is in attributes, same as the Trigger-based Template Sensor mentioned previously.