Retain values of entities on restart

Is there any reasonably usable way to retain the state of all entities during a restart.

I’m aware that Homeassistant has some mechanisms that allow integrations to do that.
But my questions works the other way around: Is there a way to make Homeassistant restore the value of any entity during the restart process?
(Emphasis on any and saving the state of every entity manually is not a reasonable solution because I have ~10.000 entities.)

Any no. I tf s not designed that way.

You canake it don’t for your own constructts with a trigger template sensor for instance.

I. Most cases you’re dealing with the state engine not actually having state until something is polled or checks in. So it said unknown until it does.

No. It does not make sense to do so for 99% of entities since they derive the value from actual sensors, templates, etc. It would also break many things in the process.

What would break and why?

No, they get the value when the state changes. As @NathanCu said, states will remain unknown until they change or they are polled. Some entities may respond to polling, but certainly not all. My system has over 3,000 entities and I don’t want the additional polling time to be added to my restarts.

If you have a specific “thing” that is breaking, assuming the mysterious “thing” is an automation, then you should rewrite the automation to handle “unavailable” entity states.

Saving the state at restart is likely worse. Say, for example, when I restart my Home Assistant server, the front door is locked. While restarting, my kid unlocks it to go get the mail and doesn’t lock it again on re-entry. After the restart is finished the state of the front door is ‘locked’. The same goes for lights and windows. How long had your Home Assistant server been offline? An hour or two to make hardware changes? I don’t know about your system but when my Home Assistant is not available for whatever reason, the manual switches still work. The current state upon restart is correctly: “Unknown”.

For instance Integral sensors would make wrong assumptions on the restored value being an actual value at that moment, followed by a real measurement from that time. Trend sensors and derivative going haywire because they will see a huge change because the actual new sensor reading comes in straght after. Automtions may fire on seeing a change that does not come from unavailable to something real, but from something that is stale, whihout you being aware it is the result of a reboot. You would introduce datapoints in time that are not based on actual readings at that time, followed by the real data.

That would only apply if the value would be set the way the sensor would do it, wouldn’t it?
If Homeassistant would just restore the value to the same as before, this shouldn’t happen.

Otherwise the restart itself will cause the very same problems anyway as a “no value” will cause a sudden drop.
I would argue that this wouldn’t make things worse.

Additionally, I wouldn’t want to force such a behaviour on anyone. I just want to have it for me (and anyone else that would like to).

The mysterious thing that is breaking are my eyes that want to look at the some values that are not there anymore (until an update is pushed).
How do I fix my eyes?

You are missing the point. If HA was down for some time HA will say sun was up when it is under. It will say it is a weekday in the weekend. It will say it is raining when it is dry.

No, you are missing the point.
I want to achieve that functionality.
I do not want to force this on anyone.

If you are fine the way it is: Be happy, it will stay that way for you.
I would like to have values.

Bad example. Sun and Time entities are polled often so you won’t see “unavailable” for long. Weather depends on your integration. I use OpenWeatherMap and the default is polling every ten minutes. I could make it poll more often, but then my usage would climb out of the free bucket.

As others have asked, just what “thing” is breaking?

It will be short, but the data point it will be there. The automations will see it change, but won’t know they are responding on stale data. Derivatives will mess up. All integrations and automations are built to deal with unavailable. They won’t work the same if the value isn’t there. That is a breaking change. You won’t fully know in what places things will misbehave until they do. But it does not matter, because the functionality isn’t there.

What I think can be useful though, which might just be what fixed the problem you think restoring state would solve:

It would be nice to have a way to get the last known state before unavailable. That would be tremendously useful in automations. I have not tried, but the new action to retrieve statistics might be able to do that.

1 Like

So, write an automation to save the state of every entity then set the entity on restart. Of course, if your wife turns off a light during the restart then your turn it back on when she isn’t expecting it…

It would also be fun if HA opens the front gate or front door because it thinks you just arrived… From away to home, right? No way to tell it was just a power outage that got fixed in the middle of the night.

What would be useful though is an easy way to get the last state before unavailability in automations etc. That could make reacting properly on outages way easier. It might already be possible with the new action to get statistics.

I assume that your comment wasn’t meant to be taken seriously. If not: Please read my initial statement where I clearly wrote “reasonable”. Saving the state of every entitiy is clearly not reasonable.

It’s interesting that everyone scrambles to find problems while I asked for a solution.
It’s my problem if I run into any problem while doing that. And honestly: It’s much easier to handle a few edge cases, where there might be a problem (which I still fail to see).

If you do not have a solution: fine, no problem.
But why do you keep inventing artificial problems when this wasn’t even the part of the initial question?

1 Like

Because you propose a blanket solution to apply to everything. You say you have over 10.000 entities. How many do you actually have a problem with? You do not even state your problem. Just your proposed solution.

Yet you want to change behavior for all entities. Lets say you have issues with 1% (which I highly doubt) then you have 9.900 entities you change without knowing what will happen. If it gets built into core, over a million instances of HA will be affected.

1 Like

I have built such a solution for my IMAP sensor.
I’m only interested in one specific email and want to keep the state.
So I wrote an automation to “preserve” the state in input text.
But I would not want this to all states.

I’m very curious what the actual problem is, just like everyone else here.
It’s hard to talk about a solution when we do not see the problem.

If there is a checkbox “retain values after reboot” then this will not affect anyone unless a person checks this box.

And I did not even ask for Homeassistant to be changed.
I simply asked whether this is possible. At all.

And

Does not match