Sensor Advice for a KWh Sensor

I’m working on an existing custom integration to implement an energy usage sensor for a plug. I know that I can use the sensor platform for KWh… However, I’m a little confused about how to go about doing it and was hoping for someone that could provide a push in the right direction.

The data is coming as an array of the total usage per hour for each day. I’m unsure what the best way to push this into a sensor would be to collect/update based on that is.

Basically, I’m getting the data as a json element per day with each element in the ‘data’ field being a sequential hour of the day starting at the timestamp listed (GMT midnight on a date).

{
    "data": "[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
    "date_ts": 1636588800000
}

Which I can of course convert into anything to make it easier to send into a sensor. However, this data is actively updated. So, as energy is used during that hour, this data will become:

{
    "data": "[0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
    "date_ts": 1636588800000
}

and so on. throughout that hour. I could delay the collection by an hour (basically getting the data for up to an hour, or two ago) to make that changing value easier to handle/less of an issue. However, I’m having a hard time conceptualizing how to get that into a sensor for KWh to collect historical data.

The examples in the dev pages here for total amount seem like they would make the most sense… But I can’t seem to find any examples of how that would actually work in code to insert data similar to the table for the sensor. Would the sensor update the value throughout the hour during the updated poll then write out the total during the next hour? Do I pass in the timestamp with value?

Thanks in advance for any insight.

I think just setting the state_class to total_increasing would work.
Just report the consumption of the current hour at each update. When hour “jump”, it would reset to zero (or at least a value < the previous one), and HA will understand you started a new cycle (hourly here).

Thank you, that’s exactly what I was wondering. Appreciate the advice.

Sorry I forgot to mark this as solved. I just did that.

However, one issue that I noticed that the data source that I’m using only updates about every 30 minutes or so. So, it’s possible (as I just watched) that I’ll get the value for the current hour and an updated value for the previous hour. Is it possible to update the sensors with a historical value? IE: Could I update the previous hour and the current hour at the same time if that data comes in together?

No. It is generally not possible in HA to set values in the past.

Thanks. That’s what I figured. I’ll just have to grab those values, update, then restet to zero and start pulling the new hour then. I’m assuming that HA will just reset the hour at that point we you said previously even if it’s at like 20 past the hour as long as it’s the first 0 value it got during the hour.