Use sensor with energy usage in energy dashboard

Hi all,

I have “build” my own integration with my energy meter and are now getting the hourly data into Home assistant as a sensor which looks like this:
image

It is basically a python script in AppDaemon that runs every hour, to get the data for the hour before it.

The code for it looks like this:

    def update(self, kwargs):
        self.log('Enegic Energy update started')
        token = self.get_token()  # Token will be retrieved from cache or newly fetched
        packets = self.get_usage(token)
        for period in packets:
            if period == 'PhaseHour':
                self.log('Use the hour data')
                deviceName = period
                friendlyName = f'Enegic - {deviceName}'
                self.log(friendlyName)
                sensor = f'sensor.enegic_{deviceName.replace(" ","_").replace(".","_").lower()}_energy'
                epochTime = (packets[period]['ts'] / 1000)
                energyTime = datetime.fromtimestamp(epochTime, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
                iavg = packets[period]['data']['iavg']
                voltage = 230
                wh_usage = [current * voltage for current in iavg]
                # Total Wh usage
                total_wh = sum(wh_usage)
                self.log(f'{friendlyName} , {energyTime}, {total_wh}')
                self.set_state(entity_id = sensor,
                    friendly_name = friendlyName,
                    device_class = 'energy',
                    state_class = 'measurement',
                    unit_of_measurement = 'Wh',
                    state = total_wh,
                    attributes = {
                        'last_updated_hour': energyTime
                        })

This all seems to work, but I cannot use the created sensor in my Energy Dashboard and I can’t find out how to do it.

I have already created a helper like this:

utility_meter:
   enegic_hourly_energy:
     source: sensor.enegic_phasehour_energy
     cycle: hourly

However, this one shows strange data, and I can’t even select it in the energy dashboard:

Can someone assist me in how to get this to work properly ?

Thank you in advance! :smiley:

Hi @Thomas1107,

Change

state_class = 'measurement'

to

state_class = 'total_increasing'

or

state_class = 'total'

depending on the implementation. (If it can in fact also decrease like a net meter, etc.)

Cheers!

Hi @davidrapan thanks for the quick reply I had

state_class = 'total_increasing'

at first, but since the data is not cumulative that didn’t work. The data I get from the API is only the usage for the hour before that.

So when I do the API call at 11:00, I get the total usage for 10:00 - 10:59. When I do the call at 12:00 I get the usage for 11:00 - 11:59. Is that where I’d use the ‘total’ then?

Here is a more clear printscreen from the history:

No it’s still 'total_increasing'. 'total' is for meters through which energy flows back and forth i.e consuming from the grid and returning back using single meter. 'total_increasing' is then for almost everything else and can reset periodically e.g. hourly like in your case. What I’m not quite sure is if it still works correctly if the sensor does not cycle through 0.

Edit: But you can easily work around that that everytime value from an api is lower than you current one you could first return 0 Wh and after that return the value from api. :wink:

I just noticed that you are missing unit of measurement in the sensor detail view. What class did you use as a base for your new custom one?

Edit1: W/ inheritance from SensorEntity

native_unit_of_measurement = 'Wh'