Dictionary value saved and updated in a helper?

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…!! :slight_smile: Thank you

edit; or is there another way to approach this i’m not thinking of?

I also prepare dictionaries & store them in an attribute of a template sensor (states cannot be more than 255 chars, so using an attribute).

Question: do you have a fixed set of keys?
I mean: today it could be

{ 'aaa': 123, 'bbb': 'as12' }

and tomorrow same set of keys

{ 'aaa': 555, 'bbb': 777 }

or could it be a different set of keys?

{ 'aaa': 555, 'ddd': 11, 'xxx': 'ABC' }

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 …

I you think that the dictionary may be created/changed by a script - consider using ONE input_text helper.

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… :slight_smile: 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.

beautiful, that’s more than enough to go on, thank you! I’ll post back up here if I get something actually working. Appreciate your help.