Hi, I’ve done some research and found that what I would all an “associative array” in javascript is called a dictionary (value?) in Home Assistant. The idea is the string like this: {key1:‘value1’, key2:‘value2’, … } .
My use case is this: for about 60 stocks I want to calculate and save the 50day/200day ratio into a “dictionary” helper that I can retrieve the next day and compare to today’s value. If the value has crossed the golden or death crosses I will take some automation action and will color that particular chart on my dashboard a bright color.
IS there a way to update a dictionary in a helper and retrieve values from it? The example based on today’s data would look something like this:
{NVDA:1.325,AAPL:1.006,AC.TO:0.911, … }
pseudo, or actual, code would be awesome…!! Thank you
edit; or is there another way to approach this i’m not thinking of?
it’s a relatively unchanging set, and when changes are made it’s purposeful and I don’t mind a bit of extra work to set it up. I mean, I could brute force it and create 60 helpers, but I prefer to find a more elegant solution if there’s one out there!
Ideally the dictionary would be flexible enough to handle changes somewhat dynamically… I can see creating a script to iterate through a set of symbols and write values into a variable to be retrieved later
edit: values will always be numeric to 3 decimal places and never null if it makes a difference. I noticed your example above contained strings. … but again ideally the approach would be extensible for future, such as writing all entity attributes into a single dictionary …
That sounds right… I plan to regenerate the full input_text morning and it will be static for the day with no need to update any individual key:value independently. Do you happen to have any examples how I might iterate through a set of keys and write a single long string into a helper?
Also, do you happen to have any examples how to retrieve an individual value from a helper by using a key? I’m happy to research it of course but if you already have working examples… thanks in advance!
No , no ready examples with input_text.
You need to create a jinja dictionary and then use to_json function.
To extract data from a string - first use from_json function, then address a key.