twrecked / hass-virtual

Virtual Components for Home Assistant
GNU General Public License v3.0
168 stars 28 forks source link

Value to survive restart #14

Closed GLehnhoff closed 1 month ago

GLehnhoff commented 3 years ago

Hi. It seems that the value of a virtual variable is not surviving a HA restart. Is this true and if so, is there a workaround available? Or is my setting of "initial_value" my problem? Cheers, gl

twrecked commented 3 years ago

I suspect it won't survive, I never programmed it to do that. I'll take a look.

tnagels commented 3 years ago

In my scenario I also need the value to survive a restart. I want to use the virtual sensor to split power measurements into different tariffs & can't use utility_meter as I don't want a cyclic reset. But as the virtual sensor resets on a reboot, I can't use this either.

Steven-D-Morgan commented 3 years ago

@tnagels I have a “Start Up Automation”. This is essentially composed of scripts that perform basic functions upon start up.

For example, I use virtual switches as “conditions” in automations. Such as “ENABLE Outdoor Timers”. If I do not want my lights to turn off in the normal 1 hour, etc then I flip that virtual switch (which is a condition of my automation).

Now, to answer your question. I have a script that turns on all my virtual switches on startup EXCEPT my “Vacation Mode” virtual switch. I hope this made sense and helps!

lalexdotcom commented 3 years ago

I could use this one too! Your integration is very useful to me (I use it to replace light groups) and having persistent value would be a must....

I would have help, but I never used python...

twrecked commented 3 years ago

I will take a look at this. Home Assistant has a storage package I can use.

Kwinnieprince commented 3 years ago

I could also use this, I use this to keep some states, enable night mode in the house, etc... It would be useful to keep the state when home assistant reboots.

lalexdotcom commented 3 years ago

I'm not a python coder (I would have help), but I'm à coder, and I tried to read the developer doc... I didn't see any storage API.

The history API could have help (get the last known state which is not unavailable), but the history can be disabled...

The most reliable way would be, in my opinion, to store a virtual.yaml file (or JSON, or any format) in the config folder, storing the virtual devices serialized, and updated each time a device's state is updated, file which would be loaded at startup to restore devices' states (and checking if any device has been removed or added)...

My 2 cents... 😉

fprokop commented 2 years ago

Many of the use cases for the virtual components can be achieved by the HA built in template components. The only thing missing there (at least for me) is for the state to stay persistent after restart, when the state can't be rendered. That's why I am hoping that the virtual components get this feature, as there are many use cases for that. Any update on that?

rsilves commented 1 year ago

I just migrated all my light templates to virtual components thinking they would be easier to use and I stumbled upon the issue that they don't keep their state on reboots! Is there any workaround to solve this? Thanks :)

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 365 days with no activity. This issue will be automatically closed within 28 days if there is no further activity.

github-actions[bot] commented 1 month ago

This issue was closed because it has been inactive for 28 days since being marked as stale.