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.
this is a very old thread… necroposting often doesn’t get the same visibility as if you post a new thread and also it alerts all the 3 year old posters on the original thread…
you should start a new thread with your question since it’s not the same as the original, but since it’s somewhat related to the very last post, you can link to it if you want… but there’s really not much info for the link.
in your new post you should go to dev-tools->states and share a screenshot of what your sensor.my_map shows in that states page… this will help us figure out why you’re getting that ‘None’ error… my guess is that there’s no such attribute as 1042 in your sensor.my_map…