States not restored reliably - please help

For a couple of weeks now, I have been trying to find out why the state of my input booleans and variables are only restored sometimes.

I have had several theories:

Performance issues (DB): Tried local SQLite and external MariaDB. No difference.
Performance issues: Tried a clean setup with only one switch, two booleans and three variables. No difference.
Persistence package: Tried with and without. No difference.
Re-set the states every after every reboot: No difference.

For me this is a deal breaker. I need to rely on HA to resume states for obvious reasons. Not very smart home if it acts randomly and you never know which state the light schedules and alarm is in after a restart…

Right now I have reverted back to 0.61.1 since z-wave started acting funny in later versions, but I have tried all of the above in 0.63.3 as well.

Example from the database:

**Before restart - correct states:**
11778	variable	variable.alarm_state	**Natt**	{"friendly_name": "Larm", "icon": "mdi:security-home", "persistent": true}	NULL	2018-02-23 16:29	2018-02-23 16:29	2018-02-23 16:30
11952	variable	variable.light_state	**Dagsljus**	{"friendly_name": "Belysning", "icon": "mdi:lightbulb-outline", "persistent": true}	NULL	2018-02-23 16:30	2018-02-23 16:30	2018-02-23 16:30
11969	variable	variable.heater_state	**22.0 °C**	{"friendly_name": "V\u00e4rme", "icon": "mdi:temperature-celsius", "persistent": true}	NULL	2018-02-23 16:30	2018-02-23 16:30	2018-02-23 16:30
12001	input_boolean	input_boolean.outdoor_lights_schedule	**on**	{"friendly_name": "Schema f\u00f6r utebelysning", "icon": "mdi:av-timer", "persistent": true}	NULL	2018-02-23 16:30	2018-02-23 16:30	2018-02-23 16:30
12004	input_boolean	input_boolean.common_lights_schedule	**on**	{"friendly_name": "Schema f\u00f6r allm\u00e4nljus", "icon": "mdi:av-timer", "persistent": true}	NULL	2018-02-23 16:30	2018-02-23 16:30	2018-02-23 16:30

**After restart - incorrect values (should be the same as before restart):**
12592	input_boolean	input_boolean.common_lights_schedule	**off**	{"friendly_name": "Schema f\u00f6r allm\u00e4nljus", "icon": "mdi:av-timer", "persistent": true}	NULL	2018-02-23 16:34	2018-02-23 16:34	2018-02-23 16:37
12594	input_boolean	input_boolean.outdoor_lights_schedule	**off**	{"friendly_name": "Schema f\u00f6r utebelysning", "icon": "mdi:av-timer", "persistent": true}	NULL	2018-02-23 16:34	2018-02-23 16:34	2018-02-23 16:37
12600	variable	variable.heater_state	**unknown**	{"friendly_name": "V\u00e4rme", "icon": "mdi:temperature-celsius", "persistent": true}	NULL	2018-02-23 16:34	2018-02-23 16:34	2018-02-23 16:37
12601	variable	variable.light_state	**unknown**	{"friendly_name": "Belysning", "icon": "mdi:lightbulb-outline", "persistent": true}	NULL	2018-02-23 16:34	2018-02-23 16:34	2018-02-23 16:37
12602	variable	variable.alarm_state	**unknown**	{"friendly_name": "Larm", "icon": "mdi:security-home", "persistent": true}	NULL	2018-02-23 16:34	2018-02-23 16:34	2018-02-23 16:37

So, is it just me or do you all experience this after restarts?

Please, any tips are extremely welcome!

My garage door sensor looses its state after HA restart. It goes to unknown. All my other device states are correct after a restart.

I also had issues with the restore function. My recommendation is to use the presistance package from Dale

copy it and you’re done

Thanks, but I already use dale3h/homeassistant-config. But unfortunately the problem still remains.

I’ve only observed this once with my variables, and that was after I changed logger values in my configuration.yaml.

I’d amended the history setting too at a prior reboot, anything similar happening?

I get it all the time too. What I did was write a script to reset to defaults when it acts up so I don’t have to manually turn on/off my input_booleans. There doesn’t seem to be a reason why it sometimes doesn’t correctly restore the state.

Yeah, that’s how I do it now, but it really doesn’t work for alarm state.

However very comforting that I’m not the only one experience this.

Thanks, haven’t tried that. Will do!

Tried with and without History now - no difference unfortunately.

I’m out of ideas, I’m sure someone will figure it out though!

Never seems to take too long around here :slight_smile: Best of luck!

Thanks!

Since I took a dive in to the database yesterday I am trying an opposite theory:
So far I have assumed that HA can’t read the states from the database and trying out performance issues.

Now when I have seen that the errors are actually in the database I need to figure out what causes HA to write ‘unknown’ state to the database.

As always - all suggestions are welcome. I’m not ready to give up just yet.

I’ll use this thread as my own documentation for now. It’s getting impossible to remember all troubleshooting variants.

Tried now:

  • Bare minimum config (no z-wave, no tradfri, no telldus, no sun…)
  • No Persistence package
  • One custom_component: Variable (three variables configured)
  • Four input_booleans configured
  • External MariaDB

States restored correctly every time.
Ten times in a row.
That’s never happened before.

I can’t believe it. Just as I wrote that - the eleventh restart failed. No states restored.

…I need some fresh air.

@gazoscalvertos has a function in his take on Alarm that writes the alarm state in a json-file. That seemed to work really good so I suggested he’d use that but make it more customizable. And like magic, he did. The result is

and it works just great.

Case now closed by workaround:

Hi,

A newbie here. Sorry if asking dumb questions.

I tried HASS-persistence. But it seems to only restore states, but not attributes. I am using 0.80.3. Looked into the python code for HASS-persistence, it seems to be updating the states only when receiving state change events. Did I misunderstand anything?

Thanks!!