custom-components / sensor.owlintuition

A set of sensors to integrate the OWL Intuition devices network
MIT License
11 stars 9 forks source link

Energy Dashboard - Broken in HA 2022.4 - state class not supported #21

Closed shortbloke closed 2 years ago

shortbloke commented 2 years ago

Since updating to 2022.4 (currently on 2022.4.2 - 11 April 2022) the Solar Generating and Exporting data no longer appears to work with the Energy Dashboard. The data is still available as a sensor, but not one that the Energy dashboard can use. The statistics panel reports that they state_class isn't supported: image

The entity sensor.owl_intuition_solar_exported_today has the following attributes:

unit_of_measurement: kWh
icon: mdi:flash
friendly_name: Solar Exported Today

There isn't a state class attribute state_class: total_increasing My Electricity consumption figures continue to work as they are using my template sensors which set the state_class.

Looking at the breaking changes in the release, I can't determine which of these is the likely cause of this regression.

Workaround, using template sensor for all sensors used in the Energy Dashboard:

####################################################
# Owl Intution - Energy Sensor                     #
####################################################
template:
  - trigger:
    - platform: state
      entity_id: sensor.owl_intuition_electricity_power
    sensor:
      - name: "Owl Grid Energy Now"
        unit_of_measurement: "W"
        state_class: measurement
        device_class: power
        state: >
          {% if is_state('sensor.owl_intuition_electricity_power', 'unknown') or is_state('sensor.owl_intuition_solar_generating', 'unknown') %}
            nan
          {% else %}
            {% set elec = states('sensor.owl_intuition_electricity_power') | float %}
            {% set solar = states('sensor.owl_intuition_solar_generating') | float %}
            {% if (float(elec) - float(solar)) < 0 %}
              0
            {% elif (float(elec) - float(solar)) > 0 %}
              {{ float(elec) - float(solar) }}
            {% endif %}
          {% endif %}

  - trigger: 
    - platform: state
      entity_id: sensor.owl_intuition_electricity_today
    sensor:
      - name: "Owl Grid Energy Today"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: >
          {% if is_state('sensor.owl_intuition_electricity_today', 'unknown') or is_state('sensor.owl_intuition_solar_generated_today', 'unknown') %}
            nan
          {% else %}
            {% set elec = states('sensor.owl_intuition_electricity_today') | float %}
            {% set solar = states('sensor.owl_intuition_solar_generated_today') | float %}
            {% set last_grid_today = states('sensor.owl_grid_energy_today') | float %}
            {% if (float(elec) - float(solar)) >= 0 %}
              {% if ((float(elec) - float(solar)) > float(last_grid_today)) or ((float(elec) - float(solar)) < 1 ) %}
                {{ float(elec) - float(solar) }}
              {% else %}
                {{ float(last_grid_today) }}
              {% endif %}
            {% endif %}
          {% endif %}

# Workaround - Breaking change in 2022.4
  - trigger: 
    - platform: state
      entity_id: sensor.owl_intuition_solar_exported_today
    sensor:
      - name: "Owl Solar Exported Today (template)"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: >
          {% if is_state('sensor.owl_intuition_solar_exported_today', 'unknown') %}
            nan
          {% else %}
            {{ states('sensor.owl_intuition_solar_exported_today') | float }}
          {% endif %}

  - trigger: 
    - platform: state
      entity_id: sensor.owl_intuition_solar_generated_today
    sensor:
      - name: "Owl Solar Generated Today (template)"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: >
          {% if is_state('sensor.owl_intuition_solar_generated_today', 'unknown') %}
            nan
          {% else %}
            {{ states('sensor.owl_intuition_solar_generated_today') | float }}
          {% endif %}

Resulting entity properties (example):

state_class: total_increasing
unit_of_measurement: kWh
device_class: energy
friendly_name: Owl Solar Generated Today (template)
psl3 commented 2 years ago

Agree Issue exists - hope for a solution!

glpatcern commented 2 years ago

Thanks for reporting guys, unfortunately I can't take a look these days nor test the latest HA. Just by code inspection it's not obvious, I'll need to add some debugging log and see live what happens - or likely involve you if I don't reproduce it with the regular sensor_electricity_energy_today.

glpatcern commented 2 years ago

Thanks, looks like a very obvious culprit. I wonder if the logs show anything pointing in that direction, @shortbloke ?

It's somehow weird that a constant stops working because the core had removed it, and yet the integration kind of works...

On Tue, 19 Apr 2022 at 16:15, petitpiton @.***> wrote:

The use of DEVICECLASS constants was deprecated in 2021.12 release. I know the code uses these. Maybe the deprecation has now moved to functionality removed?

https://www.home-assistant.io/blog/2021/12/11/release-202112/#updates-for-custom-integration-developers http://url

— Reply to this email directly, view it on GitHub https://github.com/custom-components/sensor.owlintuition/issues/21#issuecomment-1102710932, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABUNRBM35E4QGWNMO4QGQGLVF25ZJANCNFSM5TDEAHOA . You are receiving this because you were assigned.Message ID: @.***>

shortbloke commented 2 years ago

@glpatcern Not getting any errors in the logs. However comparing the sensors that do work vs those that don't:

Solar Exported Today (not working): 2022-04-19 17:55:11 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.owl_intuition_solar_exported_today, old_state=None, new_state=<state sensor.owl_intuition_solar_exported_today=1.95; unit_of_measurement=kWh, icon=mdi:flash, friendly_name=Solar Exported Today @ 2022-04-19T17:55:11.456695+01:00>>

Solar Exported Today with my template applied (working): 2022-04-19 17:54:04 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.owl_solar_exported_today_template, old_state=None, new_state=<state sensor.owl_solar_exported_today_template=unknown; state_class=total_increasing, unit_of_measurement=kWh, device_class=energy, friendly_name=Owl Solar Exported Today (template) @ 2022-04-19T17:54:04.840644+01:00>>

The non-working sensor lacks device and state class information.

shortbloke commented 2 years ago

Adding some extra logging to `OwlIntuitionSensor(Entity)::init()

2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Battery - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': None, 'state_class': None, 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_battery
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Battery Level - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'battery', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_battery_lvl
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Radio - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'signal_strength', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_radio
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Power - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_power
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Today - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_energy_today
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Cost Today - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': None, 'state_class': None, 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor electricity_cost_today
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Solar Generating - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor solargen
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Solar Generated Today - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor solargen_today
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Solar Exporting - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor solarexp
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Solar Exported Today - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Adding sensor solarexp_today
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Power P1 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Today P1 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Power P2 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Today P2 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Power P3 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'power', 'state_class': 'measurement', 'attribution': 'Powered by OWL Intuition'}
2022-04-19 18:38:33 DEBUG (MainThread) [custom_components.owlintuition.sensor] Name: OWL Intuition Electricity Today P3 - self._attrs: {'last_update': None, 'last_reset': None, 'device_class': 'energy', 'state_class': 'total_increasing', 'attribution': 'Powered by OWL Intuition'}

Appears to show the sensors are initialised with the correct device_class and state_class

shortbloke commented 2 years ago

The update call seems to be passing the correct info too. I've just deleted and recreated the database (waiting now) to see if that happens to fix it or not.

mib1185 commented 2 years ago
shortbloke commented 2 years ago

Thanks @mib1185 I'll take a look. @glpatcern I'll work on refactoring and I'll submit a PR if I can get it working.

glpatcern commented 2 years ago

Thanks @mib1185 I'll take a look. @glpatcern I'll work on refactoring and I'll submit a PR if I can get it working.

That's OK with me! Given those breaking changes in HA, it should be relatively simple to find what are nowadays the constants to be used to describe state_class and device_class valid values.

petitpiton commented 2 years ago

Glad my hunch was right about the system constants. When will the fix be merged?

glpatcern commented 2 years ago

@petitpiton indeed, apologies to @shortbloke as I missed the notification of the PR, will comment on that and hopefully merge it right away.