State.persist in pyscript not working

Hi Folks, I am trying to store weather data of today in a state and thought of using the state.persist function available in hacs-pyscript. But once homeassistant restarts, the state is lost. I have gone through the documentation and it only mentions is to put state.persist call outside so that it is called during shutdown and startup. But I can’t seem to make it work. Some screenshots of the code below

image

image

Anyone else facing the same issue? I did see “state.persist not working” issue on github but It was resolved in 2022.3 version.

Same here, except I don’t run persist at startup/shutdown, but at the start of my script that runs every hour…
Worked for a while and then it did not when there was 2 reboot in the same hour window…
Your approach seems better though.
Can’t help you, but very interested about this.

1 Like

Have you tried @time_trigger(“startup”) ?

@time_trigger if you don’t pass anything, it would by default run the function at startup.

Good to know thanks.
I’ve tested your persist functions and they seem to work well on my side.

Another option would be to create sensors with dummy values and only set them (not create them) in pyscript. It works I’ve tested it. You could also just create input_number variables in the UI and do the same.

Thanks for the tip @JohannCR. Just tried input_variable. Works fine.

I have the same issue. Unfortunately it is not really an option to use input_variables as I generate the names of the values I want to persist based on the script and these might change. I could create them manually but this would be a lot of manual effort. :frowning: do you maybe have any other idea/workaround?

Hi everyone, I’m still seeing this problem. I simply do:

state.persist('pyscript.foo', default_value=None)

@service
def update():
  foo = pyscript.foo

and the error:

NameError: name 'pyscript.foo' is not defined

This used to work, and I have other scripts where I used this exact pattern. Any idea what might have changed?

I reported the issue ~1.5 year ago :slight_smile:

I have the same problem