(Solved) How to remember entity from external pushing REST API

Hi

I have an external sensor, that pushes data to HASS using the REST API.
When rebooting it gives me the error that the entity does not exist, until the sensor is pushing data… Than the entity is created, and history also exists.

How do I get Home Assistant to remember the entity from the external sensor?

kick… sorry…
does any developer use this function? Or knows some extra info?

Doesnt it work, if you setup the sensor “by hand” and just let the rest command fill it with values? You should be able to set it up with an initial value and thus have it restored after a restart.

If this doesn’t work, I’d use a template sensor that get its values from your rest sensor. Here you can as well set an initial state so a restart/reboot will be survived.

1 Like

I don’t know a really good way, but a few ideas for a workaround:

  1. Template Sensor as proposed by @paddy0174
  2. Can the external sensor send to MQTT? The MQTT broker can retain the value
  3. Use a helper entity, python script and an automation:
    Sensor update received => helper set to this value
    After reboot, the automation uses the value from the helper entity and sets the state of the sensor. As the sensor does not exist yet, a python script is needed. You find it here:
    https://github.com/rodpayne/home-assistant/blob/3e83b42c5a1e232dfe7955ec2fd71f3f2aedf036/python_scripts/set_state.py
1 Like

The answer to your question is no, HA will not remember your sensor if it’s entirely created and updated via that rest API. In fact it’s not actually a sensor. It may have the domain sensor but that’s meaningless in this case.

To that API the state machine is a giant hash map. You give it an entity id and a state, it stuffs the state in the machine with that id. It doesnt care what the id is, it’ll change the state of existing entities, make new ones, etc. But if it makes them they are integration-less and platform-less, they aren’t really entities.

Normally for sensors there’s an actual python object of type SensorEntity providing the basic capabilities of a sensor. An integration has its own subclass adding more functionality. And the classes it makes might inherit from RestoreEntity to provide restore after restart functionality. “Entities” created by that API have none of that, they are just state with a key, nothing else.

You should not use that API this way. One of the proposals above would work. Or make a custom_component with the functionality you need.

2 Likes

With my limited knowledge I ended up with creating a temporary sensor by templating a dummy sensor.
It is a dirty solution… I know.

The python script from a radio receiver pushes messages to Home Assistant via the API… @CentralCommand I read your advises, but don’t know how to re-arrange the sending python.

So for now thanks all for your help. I ended up with this:

   - platform: template
    sensors:
      p2000gript:
        value_template: >
          {% if states.sensor.dummy == None %}
          startup
          {% endif %}