zachowj / hass-node-red

Companion Component for node-red-contrib-home-assistant-websocket to help integrate Node-RED with Home Assistant Core
MIT License
460 stars 182 forks source link

New sensor properties: state_class & last_reset #78

Closed alexdelprete closed 2 years ago

alexdelprete commented 2 years ago

Hi,

in recent HA releases, in order to support long-term statistics, devs have updated sensors' properties, introducing last_resetand state_class: https://developers.home-assistant.io/docs/core/entity/sensor/

image image

Could you please update the component adding those two properties? They are needed in particular for the new Energy Integration. I use node-red to pull data from my energy meters, as a workaround I added state_classas an attribute key, but it would be nice to have it as a default property like device_class in the Home Assistant Config section.

image

Thanks a lot for your work.

Alessandro

hermann1514 commented 2 years ago

Hi. I have the same "Problem". Please add this attributes to a new Version.

And thanks to alexdelprete: With this i have now my Smartmeter in HA Energy :-)

Thx for this great work to zachowj.

Regards Hermann

alexdelprete commented 2 years ago

I discussed about it with @zachowj on discord: he told me he will work on it.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

alexdelprete commented 2 years ago

@zachowj Jason, from a quick look at the code, in order to add those two properties, only __init__.py has to be updated right? If you can confirm, I can try to submit a PR tomorrow.

zachowj commented 2 years ago

They should be added to the sensor class, in sensor.py, as I believe they only apply to sensors.

alexdelprete commented 2 years ago

For another component, I added state_class like this:

from homeassistant.components.sensor import SensorEntity

class ABBPowerOnePVISunSpecSensor(SensorEntity):
    """Representation of an ABB SunSpec Modbus sensor."""

    def __init__(
        self, platform_name, hub, device_info, name, key, unit, icon, device_class, state_class
    ):
        """Initialize the sensor."""
        self._platform_name = platform_name
        self._hub = hub
        self._device_info = device_info
        self._name = name
        self._key = key
        self._unit_of_measurement = unit
        self._icon = icon
        self._device_class = device_class
        self._state_class = state_class

    @property
    def device_class(self):
        """Return the sensor device_class."""
        return self._device_class

    @property
    def state_class(self):
        """Return the sensor state_class."""
        return self._state_class

But the code I see in sensor.py is much different. I guess I'll wait for you to implement it. :)

zachowj commented 2 years ago

Currently, how config properties are implemented it's not possible to update them from input into an entity node. Would this be bad for last_reset? You would have to manually edit the node and deploy for this value to be changed.

alexdelprete commented 2 years ago

Would this be bad for last_reset? You would have to manually edit the node and deploy for this value to be changed.

Well, right now that's how I'm making it work: editing the node and adding the state_class as an attribute, then deploy. I expect the only difference to be, if implemented, that we find state_class in the optional Home Assistant Config" section, instead of the Attributes section:

image

So it wouldn't change anything...if I understood correctly your post.

alexdelprete commented 2 years ago

Thanks for this Jason. Can I test it installing version "main" of the custom component?

zachowj commented 2 years ago

Yes, but you'll also need the latest version, v0.40.0, of the HA nodes.

alexdelprete commented 2 years ago

Updated everything. Seems to be working...I'll monitor the sensors...

Jpsy commented 2 years ago

In the release notes of 1.0.0 I read:

Using attributes for state_class and last_reset on the entity node will no longer show these properties in Home Assistant. There are now config fields for these properties.

But I cant find any new config fields after update from 0.5.4 to 1.0.1. Where should I move the attribute state_class to in the following sensor?

image

alexdelprete commented 2 years ago

But I cant find any new config fields after update from 0.5.4 to 1.0.1. Where should I move the attribute state_class to in the following sensor?

Like Jason wrote here: you need to update HA nodes to v0.40.0, from the NR "Manage Palette".

image

Jpsy commented 2 years ago

Oh, thanks for the swift answer! Will do.