bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
1.01k stars 261 forks source link

Old energy sensors stay in entity registry, which prevent automatic group Riemann sum creation. #2476

Closed MrEbbinghaus closed 3 weeks ago

MrEbbinghaus commented 2 months ago

System Health details

System Information

version core-2024.9.0
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.4
os_name Linux
os_version 6.6.31-haos-raspi
arch aarch64
timezone Europe/Berlin
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok HACS Data | ok GitHub API Calls Remaining | 5000 Installed Version | 2.0.1 Stage | running Available Repositories | 1464 Downloaded Repositories | 26
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 13.1 -- | -- update_channel | stable supervisor_version | supervisor-2024.08.0 agent_version | 1.6.0 docker_version | 26.1.4 disk_total | 229.2 GB disk_used | 60.1 GB healthy | true supported | true host_connectivity | true supervisor_connectivity | true ntp_synchronized | true virtualization | board | rpi4-64 supervisor_api | ok version_api | ok installed_addons | Mosquitto broker (6.4.1), AirCast (4.2.1), Let's Encrypt (5.1.4), Studio Code Server (5.15.0), SQLite Web (4.2.0), NGINX Home Assistant SSL proxy (3.10.1), Advanced SSH & Web Terminal (18.0.0), room-assistant (2.20.0), Home Assistant Google Drive Backup (0.112.1), MQTT Explorer (browser-1.0.3), Matter Server (6.4.2), ESPHome (2024.8.3), Jellyfin NAS (10.9.10), Samba share (12.3.2), Tdarr (pro_1.3004), InfluxDB (5.0.0), AdGuard Home (5.1.1), Uptime Kuma (0.12.1), Paperless-ngx (2.1.1), Activity Assistant (v0.0.4.3-alpha), Glances (0.21.1), Nginx Proxy Manager (1.0.1), Piper (1.5.2), Whisper (2.1.2), Matterbridge (2.9.0-PSA-121), chrony (3.0.1)
Dashboards dashboards | 3 -- | -- resources | 18 views | 18 mode | storage
Recorder oldest_recorder_run | August 23, 2024 at 14:23 -- | -- current_recorder_run | September 4, 2024 at 19:20 estimated_db_size | 221.00 MiB database_engine | sqlite database_version | 3.45.3

Checklist

Describe the issue

The entity registry still contains the energy sensors of virtual sensors that once had an energy sensor, but that were later disabled via the Create energy sensor toggle in the UI and then deleted using the regular HA UI, after it was marked as "no longer provided".

This is an issue for group sensor, as the code that decides whether to create a Riemann sum sensor only does so, if there are no energy sensor entities in the registry:

https://github.com/bramstroker/homeassistant-powercalc/blob/7d503ead80b63dd8278abc12f0efaf3c39998943/custom_components/powercalc/sensors/group/custom.py#L357-L358

Reproduction steps

  1. Create a virtual sensor with Create energy sensor enabled
  2. Disable the Create energy sensor toggle
  3. Delete the now "not provided" energy entity
  4. (Optional) Restart HA for good measure.
  5. Create group sensor with the virtual sensor above as a member and Create energy sensor enabled.

An energy sensor is created for the group, but it's unavailable, because there are no child energy sensors.

Debug logs

Relevant log entries only:

2024-09-05 12:59:05.661 DEBUG (MainThread) [custom_components.powercalc.discovery] light.play_rechts: Auto discovered model (manufacturer=Signify Netherlands B.V., model=Hue play (LCT024), model_id=None)
2024-09-05 12:59:05.679 DEBUG (MainThread) [custom_components.powercalc.discovery] light.play_rechts: Auto discovered model (manufacturer=Signify Netherlands B.V., model=Hue play (LCT024), model_id=None)
2024-09-05 12:59:05.680 DEBUG (MainThread) [custom_components.powercalc.discovery] light.play_rechts: Already setup with discovery, skipping new discovery
...
2024-09-05 12:59:06.614 DEBUG (MainThread) [custom_components.powercalc.sensors.group.custom] Creating grouped power sensor: Schreibtisch power (entity_id=sensor.schreibtisch_power)
2024-09-05 12:59:06.614 DEBUG (MainThread) [custom_components.powercalc.sensors.group.custom] Creating grouped energy sensor: Schreibtisch energy (entity_id=sensor.schreibtisch_energy)
2024-09-05 12:59:06.616 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.schreibtisch_power to device b349049197c7c182466d84390e342c3b
2024-09-05 12:59:06.617 DEBUG (MainThread) [custom_components.powercalc.sensors.group.custom] Load previous energy sensor states from store
...
2024-09-05 12:59:06.727 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=light.play_rechts entity_category=None, sensor_name=Play Rechts power strategy=lut manufacturer=signify model=LCT024 unique_id=pc_f9292120-d577-4b5c-b92e-9a147934b0db)
2024-09-05 12:59:06.728 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.play_rechts_power to device 04180752d7b910f7a5481ff3ec50cec4
...
2024-09-05 12:59:06.768 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.schreibtisch_energy to device b349049197c7c182466d84390e342c3b
2024-09-05 12:59:06.770 WARNING (MainThread) [custom_components.powercalc.sensors.group.custom] sensor.schreibtisch_energy: Could not restore last state: [<class 'decimal.ConversionSyntax'>]

Diagnostics dump or YAML config

The group sensor:

"data": {
    "entry": {
      "created_at": "1970-01-01T00:00:00+00:00",
      "data": {
        "name": "Schreibtisch",
        "create_utility_meters": false,
        "hide_members": true,
        "unique_id": "adsfgsjakldfh3lh487sadhfh3n4fz8n3479f8h",
        "group_member_sensors": [
          "998e4263163f5fe25051f2f211ecc89d",
          "85b1562063c41b979db96cdbcb8f1bf7",
          "b92c03ac895d232bc4fca50e3b238894"
        ],
        "sensor_type": "group",
        "device": "b349049197c7c182466d84390e342c3b",
        "create_energy_sensor": true,
        "_energy_entity": "sensor.schreibtisch_energy",
        "group_power_entities": [],
        "include_non_powercalc_sensors": true
      },
      "disabled_by": null,
      "domain": "powercalc",
      "entry_id": "f425e5fd8ab6f0b632b083d25e1f9ac5",
      "minor_version": 1,
      "modified_at": "2024-09-05T10:13:25.784532+00:00",
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "title": "Schreibtisch",
      "unique_id": "adsfgsjakldfh3lh487sadhfh3n4fz8n3479f8h",
      "version": 3
    },
    "power_entities": [
      "sensor.play_rechts_power",
      "sensor.play_links_power",
      "sensor.play_oben_power"
    ],
    "energy_entities": [
      "sensor.play_rechts_energy",
      "sensor.play_oben_energy",
      "sensor.play_links_energy"
    ]
  }

One of the member sensors:

"data": {
    "entry": {
      "created_at": "1970-01-01T00:00:00+00:00",
      "data": {
        "entity_id": "light.play_rechts",
        "manufacturer": "signify",
        "model": "LCT024",
        "unique_id": "pc_f9292120-d577-4b5c-b92e-9a147934b0db",
        "sensor_type": "virtual_power",
        "name": "Play Rechts",
        "_power_entity": "sensor.play_rechts_power",
        "_energy_entity": "sensor.play_rechts_energy",
        "create_energy_sensor": false,
        "create_utility_meters": false
      },
      "disabled_by": null,
      "domain": "powercalc",
      "entry_id": "998e4263163f5fe25051f2f211ecc89d",
      "minor_version": 1,
      "modified_at": "2024-08-08T10:34:51.855243+00:00",
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "integration_discovery",
      "title": "Play Rechts",
      "unique_id": "pc_f9292120-d577-4b5c-b92e-9a147934b0db",
      "version": 3
    }
  }
bramstroker commented 1 month ago

@MrEbbinghaus why do you want to disable the energy sensors for the individual members? The group sensors expect them to be there because they don't calculate (riemann) by default but just get the data for summing from the individual energy sensors. Also see https://docs.powercalc.nl/sensor-types/group/custom/#force-creating-riemann-sum-sensor.

You can force calculating by setting this force_calculate_group_energy, but I see it's not available in the GUI yet. I will add that there as well.

bramstroker commented 1 month ago

Done with #2484, will be in next release.

MrEbbinghaus commented 1 month ago

@bramstroker I have a light fixture with multiple lights. I don't care about the energy sensors of the individual lights, only of the group.

As I understand, the linked code, it should be able to deal with missing energy sensors without having to manually set force_calculate_group_energy (which is good).

The issue from a UX perspective is: Adding a child sensor without the energy sensor. and Adding a child sensor with the energy sensor and then deleting that sensor. have two different behaviours regarding the group sensor, the second one failing silently.

The real bug

I think the underlying bug is that the groups config entry still contains the field _energy_entity although the referenced entity has been deleted, and resolve_entity_ids_recursively uses that without checking that the entity exists. So the code I linked above gets a set of entity_ids that don't exist and decides to use them instead of creating a Riemann sum sensor for the group.

bramstroker commented 1 month ago

@MrEbbinghaus Thanks for the extensive information. Will see if I can also fix this today, but I consider it quite an edge case. Anyway with the force_calculate_group_energy you can force the creation of the riemann sensor and fix your setup.

bramstroker commented 1 month ago

_energy_entity should also be correctly unset now with #2487. This was indeed cause of your issue.