Another use case for me is tracking devices which have no integration. For instance I use tasker to report the location of my car and headphones every time it connects or disconnects from one of those devices via bluetooth. There’s no integration for these so I have to stash the state of these devices in a variable since there are multiple fields to track for devices (location name, latitude, longitude, gps accuracy, other useful data, etc.). Or i can use device_tracker.see but device trackers created that way don’t survive a restart either so its not any better.
Another one I’ve encountered is when trying to write automation to notify me any time there’s a new add-on. Supervisor has an API that returns the list of all currently available add-ons I can poll against but that meant I also had to keep track of my own list of all the add-ons I’d already seen. This way I can diff the two and determine what is new. I resorted to stashing that list in a text file due to the issue described here - HA provides no place to store more then 255 chars of data that survives a restart.
Also tbh, I don’t think @123 has to explain all that much here. Just take a scan through the search results on this forum for rogro82. Rogro82’s hass-variables plugin is the go-to plugin people use to fill this gap and there are tons of posts about people using it or recommending its use. I guess you could make an argument that many of these are workarounds where people are closing gaps in HA’s featureset (like making timers and device trackers survive restarts for instance). That may be true but
a) Not all are
b) HA team has limited time so it would be great to give us this capability in the meantime. If/when they close that gap then we can delete our variable and automation and use OOTB functionality.
I have numerous examples in my config, follow the link and CTRL-F “variable”. Generally I use them to store the state of an entity after it has changed. Things that are too irregular or long to store in input booleans or input selects.
There’s also this variable custom component that I maintain (and would be happy to put to rest ). It has the same inefficiency in that it uses the entity system to store state.
For anyone asking, here’s another use case for storing state: I have an rgb light set to a current rgb value and brightness. I want to flash the light a different color to indicate a notification and then set the light back to its prior setting after the notification has passed. How else to do this but saving the state of the light into some sort of variable?
For people who don’t know, it’s a useful feature but be aware that ‘snapshot’ scenes don’t survive a restart. So if you plan to restore a scene, created with snapshot_entities, keep in mind it no longer exists after a restart.
I want to use the value of an entity (maybe a long one like the body of an email from the IMAP content sensor) after the state of that entity has changed. I believe scenes won’t cover this use case, correct?
Correct. The email body must reside in some entity, either in its state or attributes. If it exceeds 255 characters it must reside in attributes (state is limited to 255).
A scene represents the desired states for a collection of entities. A scene has no “space” to store other information beyond the entity names and the desired states (just the states and not attributes).
A scene is sort of like a recipe. It contains ingredients (light.kitchen) and quantities (brightness: 100).
Thinking about this: what if a new domain existed (variables) and we gave two services:
variables.set(key, value): creates a new variables entity (e.g., variables.my_var) whose state is the value.
variables.remove(key): deletes a variables entity with the provided key
These services would interact with a JSON storage to hold onto the values. The set service “creates” the variable and reading the entity’s state “gets” the variable.
This would be an MVP; more complex functionality (templates, etc.) could come after.
EDIT: I’ve drafted a PR for this here: https://github.com/home-assistant/core/pull/39070 – no clue if this is the ultimate direction or a detour, but it was a good exercise to play with the concepts.
I read the PR and I understand, in this very early stage, it is limited to creating a string value. My only comment is that, as it evolves, that string must be capable of holding more than the current cap of 255 characters. If it is unable to breach that ceiling, shelve this thing now because we can already use input_text to store a string of up to 255 characters.
If it will indeed be possible to hold a string value of several kilobytes, wonderful, please continue!
A tremendous bonus would be the ability to store other types like integer and, of course, list and dict. Then I imagine it will be possible to retrieve and consume the value with its type retained. For example:
The PR’s description shouldn’t be taken for documentation – a service is basically a function, so I used shorthand to communicate the idea. Nevertheless, yes, your existing service call structure in YAML is the way to go.