State value for "Custom Template Sensor" gets automatically reset to empty

Hello, I followed this thread:

and created a new “Custom Template Sensor” successfully.

I’m able to update the value of the state for this sensor fine:

curl -X POST -H "Authorization: Bearer LONG_LIVED_ACCESS_TOKEN" \
       -H "Content-Type: application/json" \
       -d '{"state": "29", "attributes": {"unit_of_measurement": "°C", "friendly_name": "Outdoor Temperature"}}' \
       https://{my-homeassistant-url}/api/states/sensor.outdoor_temperature

However, the state gets reset back to empty a few seconds later:

2019-08-10 19:01:39 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.2901368752] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sun.sun, old_state=<state sun.sun=above_horizon; next_dawn=2019-08-11T02:07:35+00:00, next_dusk=2019-08-10T18:49:52+00:00, next_midnight=2019-08-10T22:27:29+00:00, next_noon=2019-08-11T10:27:32+00:00, next_rising=2019-08-11T02:50:03+00:00, next_setting=2019-08-10T18:07:07+00:00, elevation=16.78, azimuth=273.07, rising=False, friendly_name=Sun @ 2019-08-10T18:49:36.775854+03:00>, new_state=<state sun.sun=above_horizon; next_dawn=2019-08-11T02:07:35+00:00, next_dusk=2019-08-10T18:49:52+00:00, next_midnight=2019-08-10T22:27:29+00:00, next_noon=2019-08-11T10:27:32+00:00, next_rising=2019-08-11T02:50:03+00:00, next_setting=2019-08-10T18:07:07+00:00, elevation=16.2, azimuth=273.88, rising=False, friendly_name=Sun @ 2019-08-10T18:49:36.775854+03:00>>}
2019-08-10 19:01:39 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.outdoor_temperature, old_state=<state sensor.outdoor_temperature=; unit_of_measurement=°C, friendly_name=Outdoor Temperature, icon=mdi:thermometer @ 2019-08-10T19:01:26.011035+03:00>, new_state=<state sensor.outdoor_temperature=29; unit_of_measurement=°C, friendly_name=Outdoor Temperature @ 2019-08-10T19:01:39.812520+03:00>>
2019-08-10 19:01:39 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.2901368752] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.outdoor_temperature, old_state=<state sensor.outdoor_temperature=; unit_of_measurement=°C, friendly_name=Outdoor Temperature, icon=mdi:thermometer @ 2019-08-10T19:01:26.011035+03:00>, new_state=<state sensor.outdoor_temperature=29; unit_of_measurement=°C, friendly_name=Outdoor Temperature @ 2019-08-10T19:01:39.812520+03:00>>}

2019-08-10 19:01:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.outdoor_temperature, old_state=<state sensor.outdoor_temperature=29; unit_of_measurement=°C, friendly_name=Outdoor Temperature @ 2019-08-10T19:01:39.812520+03:00>, new_state=<state sensor.outdoor_temperature=; unit_of_measurement=°C, friendly_name=Outdoor Temperature, icon=mdi:thermometer @ 2019-08-10T19:01:56.014998+03:00>>
2019-08-10 19:01:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.2901368752] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.outdoor_temperature, old_state=<state sensor.outdoor_temperature=29; unit_of_measurement=°C, friendly_name=Outdoor Temperature @ 2019-08-10T19:01:39.812520+03:00>, new_state=<state sensor.outdoor_temperature=; unit_of_measurement=°C, friendly_name=Outdoor Temperature, icon=mdi:thermometer @ 2019-08-10T19:01:56.014998+03:00>>}

I chart for this sensor in the “History” section is just a lot of dots / discrete values rather than a continuous line.

It doesn’t look right to me and any insight into what might be happening here is very welcome.

Assuming the values for the sensor come only from your curl POSTs, then you should not use a template sensor for this. Rather you should use an HTTP Sensor (which requires no configuration entry and is simply created when the POST is received.)

@pnbruckner, thanks for the message. Indeed HTTP sensor seems to be fit for my use-case, I’ll give it a go!.

OK, so @pnbruckner thanks again, I now understand what was happening. (I only started to use HA a week ago).
I created a “Custom Template Sensor”, but then I made an HTTP post as if my sensor was “HTTP sensor”. I actually thought that I’m updating my “Custom Template Sensor”, but perhaps each time a new HTTP Sensor was created and deleted or something like that. Or at least a clash of some sort between the “Custom Template Sensor” and "“HTTP Sensor”.

Anyway, I deleted the “Custom Template Sensors” from Integration and kept posting new values for the HTTP sensor - it all looks normal now.

I also need to check if the historical sensor values would disappear each time I restart HA, as I read “The sensor will then exist as long as Home Assistant is running. After a restart of Home Assistant the sensor will be gone until it is triggered again.”

Just to add - historical values do not disappear when I restart HA, so this solution (HTTP Sensor) works for me fine. Thanks.