How to add energy monitoring to a custom component?

Hi all,

I’m trying to add the new Energy Monitoring feature to the lightwaverf energy monitor however I’m struggling to understand exactly how this all works.

I’ve updated my code so that I’m importing the DEVICE_CLASS_POWER and STATE_CLASS_MEASUREMENT however I’m still getting “No Matching Statistics Found” when I try and add the energy monitor as a source under Configure Grid Consumption.

The code I have to initialise the sensor is as follows:

from homeassistant.components.sensor import PLATFORM_SCHEMA, STATE_CLASS_MEASUREMENT
from homeassistant.const import CONF_SCAN_INTERVAL, DEVICE_CLASS_POWER

# ... other code ...

def setup_platform(hass, config, add_devices, discovery_info=None):
    """Configure the sensor platform for home assistant."""
    scan_interval = hass.data[DATA_KEY]
    _LOGGER.info("scan interval= %s", scan_interval)
    lightwave_devices = [
        LightwaveEnergy("CURRENT_USAGE", scan_interval, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT),
        LightwaveEnergy("TODAY_USAGE", scan_interval, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT),
    ]
    add_devices(lightwave_devices)


class LightwaveEnergy(Entity):
    """Primary class for Lightwave Energy."""

    def __init__(self, sensor_type, scan_interval, device_class, state_class):
        """Initialize the sensor."""
        _LOGGER.info("Lightwave Energy Init , scan interval=%s", scan_interval)
        self.sensor_type = sensor_type
        self.serial = ""
        self.mac = ""
        self.current_usage = ""
        self.today_usage = ""
        self.lightwave_energy_data = None
        self.scan_interval = scan_interval
        self._updatets = time.time()
        self._attr_device_class = device_class
        self._attr_state_class = state_class

and the resource details from the developer tool box for each of the two measurements returned is as follows:

# sensor.electricity_current_useage_w
unit_of_measurement: W
friendly_name: Electricity Current Usage (W)
icon: mdi:flash
device_class: power

# sensor.electricity_energy_today_usage_kwh
unit_of_measurement: kWh
friendly_name: Electricity Energy Today Usage (kWh)
icon: mdi:flash
device_class: power

The docs mention a STATE_CLASS_TOTAL_INCREASING option for the sensor state, however that doesn’t appear to be available in the version I’m running:

System Health

version core-2021.8.8
installation_type Home Assistant Core
dev false
hassio false
docker false
user homeassistant
virtualenv true
python_version 3.8.7
os_name Linux
os_version 5.10.52-v7+
arch armv7l
timezone Europe/London
Home Assistant Community Store
GitHub API ok
Github API Calls Remaining 3906
Installed Version 1.14.1
Stage running
Available Repositories 881
Installed Repositories 38
Home Assistant Cloud
logged_in true
subscription_expiration 17 September 2021, 1:00
relayer_connected true
remote_enabled false
remote_connected false
alexa_enabled true
google_enabled false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok

If anyone can point me in the direction of an explainer on how to add this functionality to an existing code-base, that would be amazing!

This will only be available in 2021.9.x

Ok, thanks, that makes sense then!

Do you know if there are any guides to setting up a custom sensor for the current version?

Not from a developer perspective, I only know that the entity needs to have state_class set to measurement and last_reset must be set.

Ok, thanks.

It’s setting those attributes that I’m having issues with - the docs seem to assume that you already know how to do this!

Not a guide as such, but I had to struggle through the same thing when trying to set up energy sensors for my custom component ( homeassistant-lightwave2/sensor.py at master · bigbadblunt/homeassistant-lightwave2 (github.com) ) - my code may or may not help.

However, one thing that caught me out for a long time was that I don’t think inheriting from Entity works - I think you have to inherit from SensorEntity; i.e. I think you need to change

class LightwaveEnergy(Entity):

This may be a complete red herring, I went through lots of iterations getting this to work, so it might have been nothing to do with the above!

1 Like