Keep states after restarting

Hi
I want to record how much food my cats are eating in average at a specific time. So I wrote a python script which get the right entity e.g. cat.kalle_after_noon_food, get the state and the counter attribute, doing some math and then updates the state of the entity and the counter. If the entity cat.kalle_after_noon_food not exists it will get created (should be only happen at the first time). To control the right output of the script, I’m using the sqlite addon to check the entities in the database. It’s all working fine, but when I restart HA the entity card

type: entities
entities:
  - entity: cat.kalle_after_noon_food
    name: Kalle nachmittags

is saying Entity not available: cat.kalle_after_noon_food although the entity is inside the sql database after the restart. When I run the python script again a new entity without a reference to the old entity gets created (old_state_id is NULL).
Why does HA behave like this and how can I load the sql data into the HA after restart or something like this to get what I want.

What I’ve tried:

  • adding the entities to the recorder:
recorder:
    include:
        entity_globs:
            - cat.*
  • adding the entities to the history:
history:
    include:
        entity_globs:
            - cat.*
  • adding the entities to the customize.yaml file and include in the configuration.yaml file
cat.kalle_after_noon_food:
    unit_of_measurement: 'g'
  • adding automation.initial_state: ‘on’ (even if I don’t really know what this is doing)
cat.kalle_after_noon_food:
    unit_of_measurement: 'g'
      automation:
        initial_state: 'on'

Maybe it could be related to the fact that you use cat instead of another domain name?

In some integrations, it auto-restores entities.

hmm ok. I don’t know if it is related to this. So I have to use binary_sensor instead of cat? Or is it possible to tell HA to restore my custom entities?

I think that would make more trouble, given that binary_sensors should only be on or off.

You could try doing something with an automation and a file integration, save the state to a file when HA shuts down, and restore when it boots back up. Or just use an input_number instead.

It is not possible to open/ save files in python scripts :confused: but I will use input_numbers. This feels wrong to me and a bit hacky. Strange that you have to do it like this.