bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
943 stars 251 forks source link

Entity not defined all of a sudden #1484

Closed sweetpants closed 1 year ago

sweetpants commented 1 year ago

After testing the latest version of PowerCalc, I still see the same issue with virtual power sensors. I have setup two virtual power devices which generate 2 entities (power/energy) each. In PowerCalc they show up as one device with two entities (power/energy). I use these devices as 'individual' devices in the energy tab and this works fine initially.

After a while for no apparent reason both entities of the device suddenly disappear, sometimes for one device sometimes both devices. The result is that in the energy configuration the devices are marked as 'Entity not defined' and the sensor stops working. The only way to get the sensor back is to go to the PowerCalc integration and reload the device. Then the 2 entities (power/energy) of the device re-appear and the sensor works again. However, if you had set the name of the power/energy sensor, the name falls back to the default name from Entity ID. The configured Name is gone.

Regards, Harry

sweetpants commented 1 year ago

Not sure if this https://github.com/bramstroker/homeassistant-powercalc/issues/1481 is related, but it also happens after an HA restart. However, the entities (power/energy) are still there after a restart. In my case they are gone also.

bramstroker commented 1 year ago

Please provide some more information about these entities. Which integration does provide the source entities which the powercalc virtual sensors are based on? Also could you include the diagnostics? You can download these by clicking on the 3 dots in the integration tab for powercalc and than "Download diagnostics".

Any relevant errors in the logs?

I think #1481 is definitely related. So any more information can help me reproduce the issue, which is hard on it's own.

Will take some time until I can have a more thorough look / debugging session, quite busy schedule coming week(s).

sweetpants commented 1 year ago

Hi,

Both sensors are virtual power sensors derived from a (ESPhome) switch entity creating only energy sensors with a standby power (0.7 = ESP 8266 switch) and normal power (xxxx watt). Unavailable states are ignored and energy integration methode is left. (see diagnostics)

Virtual power sensor #1, an infrared heating panel

{
  "home_assistant": {
    "installation_type": "Home Assistant Supervised",
    "version": "2023.2.3",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "x86_64",
    "timezone": "Europe/Amsterdam",
    "os_name": "Linux",
    "os_version": "5.10.0-21-amd64",
    "supervisor": "2023.01.1",
    "host_os": "Debian GNU/Linux 11 (bullseye)",
    "docker_version": "20.10.8",
    "chassis": "desktop",
    "run_as_root": true
  },
  "custom_components": {
    "powercalc": {
      "version": "v1.3.3",
      "requirements": [
        "numpy>=1.21.1"
      ]
    },
    "enphase_envoy": {
      "version": "0.1.2",
      "requirements": [
        "envoy-utils"
      ]
    },
    "nodered": {
      "version": "1.1.2",
      "requirements": []
    },
    "hacs": {
      "version": "1.30.1",
      "requirements": [
        "aiogithubapi>=22.10.1"
      ]
    },
    "scheduler": {
      "version": "v0.0.0",
      "requirements": []
    },
    "alarmo": {
      "version": "v1.9.7",
      "requirements": []
    }
  },
  "integration_manifest": {
    "after_dependencies": [
      "integration",
      "utility_meter"
    ],
    "codeowners": [
      "@bramstroker"
    ],
    "config_flow": true,
    "dependencies": [
      "light",
      "group",
      "template",
      "select",
      "utility_meter"
    ],
    "documentation": "https://github.com/bramstroker/homeassistant-powercalc",
    "domain": "powercalc",
    "iot_class": "local_polling",
    "issue_tracker": "https://github.com/bramstroker/homeassistant-powercalc/issues",
    "name": "Powercalc",
    "requirements": [
      "numpy>=1.21.1"
    ],
    "version": "v1.3.3",
    "is_built_in": false
  },
  "data": {
    "entry": {
      "entry_id": "bef557f41aa83b56528d1485303e6f9c",
      "version": 1,
      "domain": "powercalc",
      "title": "IR Panel Woonkamer",
      "data": {
        "entity_id": "switch.switch_2_relay",
        "mode": "fixed",
        "create_energy_sensor": true,
        "create_utility_meters": false,
        "name": "IR Panel Woonkamer",
        "standby_power": 0.7,
        "fixed": {
          "power": 1000.0
        },
        "energy_integration_method": "left",
        "ignore_unavailable_state": true,
        "unique_id": "pc_switch-2switchswitch-2_relay",
        "sensor_type": "virtual_power"
      },
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": "pc_switch-2switchswitch-2_relay",
      "disabled_by": null
    }
  }
}

Virtual power sensor #2, a floor heating pump

{
  "home_assistant": {
    "installation_type": "Home Assistant Supervised",
    "version": "2023.2.3",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "x86_64",
    "timezone": "Europe/Amsterdam",
    "os_name": "Linux",
    "os_version": "5.10.0-21-amd64",
    "supervisor": "2023.01.1",
    "host_os": "Debian GNU/Linux 11 (bullseye)",
    "docker_version": "20.10.8",
    "chassis": "desktop",
    "run_as_root": true
  },
  "custom_components": {
    "powercalc": {
      "version": "v1.3.3",
      "requirements": [
        "numpy>=1.21.1"
      ]
    },
    "enphase_envoy": {
      "version": "0.1.2",
      "requirements": [
        "envoy-utils"
      ]
    },
    "nodered": {
      "version": "1.1.2",
      "requirements": []
    },
    "hacs": {
      "version": "1.30.1",
      "requirements": [
        "aiogithubapi>=22.10.1"
      ]
    },
    "scheduler": {
      "version": "v0.0.0",
      "requirements": []
    },
    "alarmo": {
      "version": "v1.9.7",
      "requirements": []
    }
  },
  "integration_manifest": {
    "after_dependencies": [
      "integration",
      "utility_meter"
    ],
    "codeowners": [
      "@bramstroker"
    ],
    "config_flow": true,
    "dependencies": [
      "light",
      "group",
      "template",
      "select",
      "utility_meter"
    ],
    "documentation": "https://github.com/bramstroker/homeassistant-powercalc",
    "domain": "powercalc",
    "iot_class": "local_polling",
    "issue_tracker": "https://github.com/bramstroker/homeassistant-powercalc/issues",
    "name": "Powercalc",
    "requirements": [
      "numpy>=1.21.1"
    ],
    "version": "v1.3.3",
    "is_built_in": false
  },
  "data": {
    "entry": {
      "entry_id": "9fb43499f29974da2c0579d5190d0fd5",
      "version": 1,
      "domain": "powercalc",
      "title": "VV Pomp",
      "data": {
        "entity_id": "switch.vloer_pomp",
        "mode": "fixed",
        "create_energy_sensor": true,
        "create_utility_meters": false,
        "name": "VV Pomp",
        "fixed": {
          "power": 64.0
        },
        "energy_integration_method": "left",
        "ignore_unavailable_state": true,
        "unique_id": "pc_cv-installatieswitchvloer_pomp",
        "sensor_type": "virtual_power",
        "standby_power": 0.7
      },
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": "pc_cv-installatieswitchvloer_pomp",
      "disabled_by": null
    }
  }
}
bramstroker commented 1 year ago

You have exactly the same issue as reported in https://github.com/bramstroker/homeassistant-powercalc/issues/1481, which also has issues with powercalc sensors derived from ESPHome devices. I also have some esp home devices so I tried to reproduce on my development instance, and also on some other HA installation I had running, but I was unable to reproduce yet unfortunately. Could you enable debug logging and restart HA, and share the debug logs? Maybe they provide some further insight / clues for me. I have put a potential workaround in last comment of #1481. Could you give that a try to see if that resolves things for you. It's not a nice solution, I know and really a hack/workaround. But when I'm unable to reproduce this issue in any way I don't know how to resolve.

sweetpants commented 1 year ago

Thanks, i will implement the workaround, hope you can find something. I will try to enable debug logging later this week as i'm busy now.

sweetpants commented 1 year ago

Hmmm, added the workaround:

alias: Reload Powercalc entries on restart of HA description: |- Workaround to solv an issue of disappearing entities in PowerCalc #1481 https://github.com/bramstroker/homeassistant-powercalc/issues/1481 trigger:

and enabled debug on this integration. But after a HA reload I get a bunch off errors in the log and the restart does not (re)load the entities.

When manually run the automation i get some errors:

2023-02-12 17:17:57.873 ERROR (MainThread) [homeassistant.components.automation.reload_powercal_entries_on_restart_of_ha] Reload Powercalc entries on restart of HA: Error executing script. Unexpected error for call_service at pos 1: There were no matching config entries to reload Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step await getattr(self, handler)() File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step await service_task File "/usr/src/homeassistant/homeassistant/core.py", line 1787, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1824, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 756, in admin_handler await result File "/usr/src/homeassistant/homeassistant/components/homeassistant/init.py", line 262, in async_handle_reload_config_entry raise ValueError("There were no matching config entries to reload") ValueError: There were no matching config entries to reload 2023-02-12 17:17:57.898 ERROR (MainThread) [homeassistant.components.automation.reload_powercal_entries_on_restart_of_ha] While executing automation automation.reload_powercal_entries_on_restart_of_ha Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/automation/init.py", line 546, in async_trigger await self.action_script.async_run( File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1524, in async_run await asyncio.shield(run.async_run()) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 409, in async_run await self._async_step(log_exceptions=False) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 453, in _async_step self._handle_exception( File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 476, in _handle_exception raise exception File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step await getattr(self, handler)() File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step await service_task File "/usr/src/homeassistant/homeassistant/core.py", line 1787, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1824, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 756, in admin_handler await result File "/usr/src/homeassistant/homeassistant/components/homeassistant/init.py", line 262, in async_handle_reload_config_entry raise ValueError("There were no matching config entries to reload") ValueError: There were no matching config entries to reload

If i Then reload the entities/devices in the integration all is well

2023-02-12 17:20:00.058 DEBUG (MainThread) [custom_components.powercalc.discovery] switch.switch_2_relay: Auto discovered model (manufacturer=Espressif, model=esp8285) 2023-02-12 17:20:00.059 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.switch_2_relay entity_category=None, sensor_name=IR Panel Woonkamer power strategy=FixedStrategy manufacturer= model= standby_power=0.70 unique_id=pc_switch-2switchswitch-2_relay) 2023-02-12 17:20:00.059 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: IR Panel Woonkamer energy 2023-02-12 17:20:00.061 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.ir_panel_woonkamer_power to device 960bcde19c4957bdd2cefd5f9b5bc498 2023-02-12 17:20:00.080 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.switch_2_relay: State changed to "off". Power:0.70 2023-02-12 17:20:00.086 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.ir_panel_woonkamer_energy to device 960bcde19c4957bdd2cefd5f9b5bc498 2023-02-12 17:20:13.269 DEBUG (MainThread) [custom_components.powercalc.discovery] switch.vloer_pomp: Auto discovered model (manufacturer=Espressif, model=nodemcuv2) 2023-02-12 17:20:13.269 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.vloer_pomp entity_category=None, sensor_name=VV Pomp power strategy=FixedStrategy manufacturer= model= standby_power=0.70 unique_id=pc_cv-installatieswitchvloer_pomp) 2023-02-12 17:20:13.270 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: VV Pomp energy 2023-02-12 17:20:13.272 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.vv_pomp_power to device cb273c0e1d8d36244c434a37215ab501 2023-02-12 17:20:13.289 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.vloer_pomp: State changed to "off". Power:0.70 2023-02-12 17:20:13.304 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.vv_pomp_energy to device cb273c0e1d8d36244c434a37215ab501

Regards, HArry

bramstroker commented 1 year ago

That's probably because the entities are not available / linked to the config entry after startup. That would probably cause this error message "There were no matching config entries to reload". I see you can also pass entry id to the service, so that's worth a try.

description: ""
mode: single
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: homeassistant.reload_config_entry
    data:
      entry_id: bef557f41aa83b56528d1485303e6f9c

I have put the one of your first diagnostics in here, that's where you can find this entry id. So you'll need to call the service multiple times for each of the problematic config entries.

bramstroker commented 1 year ago

@sweetpants were you able to try this workaround?

sweetpants commented 1 year ago

Yes, but dows not work. When 'run' the automation manually, it still does not work. Also tried adding a delay after start, but that also does not help.

alias: Restart PowerCalc integration (workaround) description: Workaround for ESPhome virtual sensors that disapear after a HA restart trigger:

bramstroker commented 1 year ago

Closing as it's a duplicate of #1481. Conversation for this issue is now happening on two issues, let's keep all conversation on one.