Creating permanent sensors to post to using HTTP API

I have an application running outside of homeassistant (actually just a collection of Python/pandas scripts) that takes data from the HA history database, calculates some statistics from it, and posts it back to HA using the HTTP API. I post to sensors that don’t exist, and as expected they spring into existence the first time and stick around until HA is restarted. That’s all OK, except that some of this data is only posted into HA daily, and having the sensors disappear after a reboot makes the dashboards look odd until the next post.

I’m probably missing something obvious, or just not understanding something basic, but how would I create these sensors so that they don’t disappear. I was assuming I could create them in configuration.yml (or in one of its included files) but I can’t figure out how.

I’m posting data of various types for various purposes, sometimes just state values and sometimes state + attributes.

As an example. I’m looking at the on/off state of my smart thermostat to calculate the daily runtime of my heating. (Aside, if there’s a way to do that using the built-in statistics capabilities, that would be easier, but I can’t figure that out, either:-) So I’m posting back a time interval.

What am I missing?

Thanks,
Steve.

Yes there is. Use a History Stats sensor:

There are examples of how to specify the time period.

1 Like

Not sure how I missed the history_stats platform. Thanks for the hint. That’s now all working perfectly, and has saved me a bunch of work!

That said, there is still some data I’m posting into HA that doesn’t originate in HA. For example, when I run daily backups of my various systems I post the backup size and runtime once the backup is completed. That lets me look at an HA dashboard and know that the overnight backups worked, or didn’t. That’s just a bash script on the server running the backups and finally using curl to send the data into HA via the HTTP API.

And those data only get posted once a day, so an HA restart leaves those sensors missing all day, so creating a permanent sensor somehow would be really helpful so I’m still looking for a way of doing that.

Thanks,
Steve.

Can Home Assistant pull the data rather than your app posting it?

That way it could be set to check after home assistant restarted.

The other option is to publish the data to an MQTT broker as a retained message. Home assistant will then receive the last message after a restart.

You will not get that.
More info: https://www.home-assistant.io/blog/2021/05/12/integrations-api/

As an alternative, you can use a template sensor with a webhook trigger.
https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors

The data in question isn’t stored anywhere, currently. In the example, it is just the runtime of a shell script on the server. I could put it somewhere else (I have a DB lying around already) and a simple server to serve it up via a rest api, I guess. I was hoping to not have to do that. The existing mechanism is almost there, if only there was a way to create sensors that didn’t disappear.

I have tried creating helpers and posting to those, and that works, but there aren’t helpers for all the data types I wnat to use (floating point numbers, for example), and they display in dashboards as changeable fields, which I don’t want.

Anyway, thanks for your suggestions. I guess I need to now decide if disappearing sensors is a big enough problem to justify going away and building a REST server… :slight_smile:

Steve.

I spent a little while thinking about implementing a little “proxy” server that would accept HA-compatible HTTP POST requests and save the data so that my HA instance could have HTTP REST sensors to pull the data in. That would then behave exactly as I want. The sensors in HA would be permanent, so that a restart would leave hols in my dashboards, and I’d just post from the outside to a different server.

Then it occurred to me that such a thing already exists. It is called Home Assistant! This might sound a bit like overkill, but I’ve now stood up another HA instance alongside my primary one. I use the HTTP API to post to that, and have HTTP REST sensors in my primary instance to pull the values in. Those sensors don’t disappear on reboot, obviously, and so this all works perfectly and thanks to the magic of docker took just a few minutes to set up. The proxy HA instance is completely empty - no integrations. I just had to create a user so I could get the authentication token.

It wasn’t a lot of effort to set up - certainly easier than writing something new - but it does seem a little over the top when all I want is to be able to create permanent sensors that I can post to using the HTTP API. But, it works and shouldn’t need any ongoing maintenance other than the occasional update, so I guess I’m happy!

Steve.

Hi. How did you make your HA to send data from the sensor to API?