bramstroker / homeassistant-powercalc

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

entities do not start after HA restart #1410

Closed salut1a closed 1 year ago

salut1a commented 1 year ago

After rebooting HA, all entities report that they are unavailable, it helps to select the reload option on each HA wersja 2022.11.4 PowerCalc - v1.1.8

bramstroker commented 1 year ago

Do you have this after every reboot or incidentially?

Anything in the logs? also try to enable debug logging.

salut1a commented 1 year ago

this happens after every reset. Now I noticed in the logs that maybe because I gave autodiscover earlier.

powercalc: energy_sensor_precision: 2 force_update_frequency: 00:00:15 create_energy_sensors: true

enable_autodiscovery: true --------------------------------------

energy_sensor_naming: "{} Energysensor" power_sensor_naming: "{} Powersensor" create_utility_meters: true

2023-01-02 22:23:07.988 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration powercalc which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2023-01-02 22:23:24.018 DEBUG (MainThread) [custom_components.powercalc] Start auto discovering entities 2023-01-02 22:23:24.019 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.salon: Auto discovered model (manufacturer=Google Inc., model=Google Home) 2023-01-02 22:23:24.023 DEBUG (MainThread) [custom_components.powercalc] media_player.salon: Model not found in library, skipping discovery 2023-01-02 22:23:24.023 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.wszystkie: Auto discovered model (manufacturer=Google Inc., model=Google Cast Group) 2023-01-02 22:23:24.023 DEBUG (MainThread) [custom_components.powercalc] media_player.wszystkie: Model not found in library, skipping discovery 2023-01-02 22:23:24.024 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.schedule_f14e96: Auto discovered model (manufacturer=@nielsfaber, model=Scheduler) 2023-01-02 22:23:24.024 DEBUG (MainThread) [custom_components.powercalc] switch.schedule_f14e96: Model not found in library, skipping discovery 2023-01-02 22:23:24.024 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.schedule_f8cec3: Auto discovered model (manufacturer=@nielsfaber, model=Scheduler) 2023-01-02 22:23:24.025 DEBUG (MainThread) [custom_components.powercalc] switch.schedule_f8cec3: Model not found in library, skipping discovery

salut1a commented 1 year ago

disabling autodiscovery didn't help

salut1a commented 1 year ago

2023-01-03 23:03:14.958 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration powercalc which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2023-01-03 23:03:31.951 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] sensor.klima_current_power: Auto discovered model (manufacturer=Broadlink, model=SP3S-EU) 2023-01-03 23:03:31.954 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=sensor.klima_current_power entity_category=None, sensor_name=Klima energia Powersensor strategy=FixedStrategy manufacturer= model= standby_power=0.20 unique_id=34ea347a15ab-power) 2023-01-03 23:03:31.955 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Klima energia Energysensor 2023-01-03 23:03:31.955 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor daily (entity_id=sensor.klima_energia_energy_daily) 2023-01-03 23:03:31.956 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor weekly (entity_id=sensor.klima_energia_energy_weekly) 2023-01-03 23:03:31.956 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor monthly (entity_id=sensor.klima_energia_energy_monthly) 2023-01-03 23:03:31.957 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] sensor.smart_socket_3_power: Auto discovered model (manufacturer=Tuya, model=Smart Socket (37mnhia3pojleqfh)) 2023-01-03 23:03:31.961 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=sensor.smart_socket_3_power entity_category=None, sensor_name=Suszarka Energy Calc Powersensor strategy=FixedStrategy manufacturer= model= standby_power=0.20 unique_id=tuya.3038076070039f64c8cacur_power) 2023-01-03 23:03:31.962 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Suszarka Energy Calc Energysensor 2023-01-03 23:03:31.962 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Suszarka Energy Calc Energysensor daily (entity_id=sensor.suszarka_energy_calc_energysensor_daily) 2023-01-03 23:03:31.963 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Suszarka Energy Calc Energysensor weekly (entity_id=sensor.suszarka_energy_calc_energysensor_weekly) 2023-01-03 23:03:31.964 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Suszarka Energy Calc Energysensor monthly (entity_id=sensor.suszarka_energy_calc_energysensor_monthly) 2023-01-03 23:03:31.965 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] sensor.aubess_smart_socket_20a_em_power: Auto discovered model (manufacturer=Tuya, model=Aubess Smart Socket 20A#slash#EM (2iepauebcvo74ujc))

salut1a commented 1 year ago

last log:

2023-01-03 23:25:25.962 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.smart_socket_3_power: State changed to "2891.0". Power:320.00 2023-01-03 23:25:30.973 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.smart_socket_3_power: State changed to "2886.0". Power:320.00 2023-01-03 23:25:35.999 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.smart_socket_3_power: State changed to "2881.0". Power:320.00 2023-01-03 23:25:39.139 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.lazienka_bojler_power: State changed to "1529". Power:1507.00 2023-01-03 23:25:46.071 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.smart_socket_3_power: State changed to "2884.0". Power:320.00 2023-01-03 23:25:51.060 DEBUG (MainThread) [custom_components.powercalc.sensors.power] sensor.smart_socket_3_power: State changed to "2879.0". Power:320.00

salut1a commented 1 year ago

create_energy_sensors: false create_utility_meters: false disable this option helped

bramstroker commented 1 year ago

Interesting finding.

What do you mean with unavailable exactly? Do you have a screenshot of the situation. You are only using the GUI configuration right?

I think you might have the same issue as reported here: https://github.com/bramstroker/homeassistant-powercalc/issues/1153. But I was never able to reproduce or fix this.

When you have these config settings than the entities are created succesfully after every restart?

create_energy_sensors: false
create_utility_meters: false

What if you only disable utility meters or energy sensors to find the actual cause?

salut1a commented 1 year ago

but didn't help, the entity is still not anailable

salut1a commented 1 year ago

2023-01-05 14:36:14.974 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration powercalc which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2023-01-05 14:36:31.126 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] sensor.klima_current_power: Auto discovered model (manufacturer=Broadlink, model=SP3S-EU) 2023-01-05 14:36:31.130 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=sensor.klima_current_power entity_category=None, sensor_name=Klima energia Powersensor strategy=FixedStrategy manufacturer= model= standby_power=0.20 unique_id=34ea347a15ab-power) 2023-01-05 14:36:31.130 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Klima energia Energysensor 2023-01-05 14:36:31.131 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor daily (entity_id=sensor.klima_energia_energy_daily) 2023-01-05 14:36:31.131 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor weekly (entity_id=sensor.klima_energia_energy_weekly) 2023-01-05 14:36:31.132 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Klima energia Energysensor monthly (entity_id=sensor.klima_energia_energy_monthly) 2023-01-05 14:36:31.133 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] sensor.smart_socket_3_power: Auto discovered model (manufacturer=Tuya, model=Smart Socket (37mnhia3pojleqfh))

salut1a commented 1 year ago

blad

bramstroker commented 1 year ago

This means not the entity is unavailable, but the entity is not created by powercalc at all in HA, which seems rather similar bug as I linked above. On the integration overview page when you click a config entry (where you reload), are you seeing any entities there. Or is that list empty and after reload entities appear?

Do you have this issue with all powercalc entities or only some of them?

You mentioned that setting create_energy_sensors to false worked, but later when I asked again it didn't work?

bramstroker commented 1 year ago

This is all expected and just says powercalc tried to create the 3 utility meters during startup. There is no indication these entities are not created and no further error which makes it very strange. Seems some strange bug in internals of HA, as it only hits a handful of users. hundreds to thousands of users are using the powercalc GUI config on a daily basis without any problems.

For every powercalc entry you should see the following in the logs:

The only common thing #1153 is that you are also using Tuya.

Could you try temporarily disabling that integration and restarting to see if that resolves something?

How many powercalc entries do you have? Maybe you can also try disabling them one by one to see if this resolves anything, so we can find the culprit.

It's very cumbersome, I know. but without any reproduction steps I have no idea how to resolve the issue.

I will ask around on discord to the HA devs if anyone has seen a similar issue before.

salut1a commented 1 year ago

I also found such a thing

Logger: custom_components.powercalc.sensors.power Source: custom_components/powercalc/sensors/power.py:94 Integration: Powercalc (documentation, issues) First occurred: 14:17:51 (4 occurrences) Last logged: 14:17:51

switch.grzejnik_socket_1: Skipping sensor setup Model not found in library (manufacturer: Tuya, model: Wifi Plug (j0zozzoarutv0nu1)) switch.grzejnik_socket_1: Skipping sensor setup: Model not found in library (manufacturer: Tuya, model: Wifi Plug (j0zozzoarutv0nu1)) switch.lazienka_bojler: Skipping sensor setup Model not found in library (manufacturer: TuYa, model: Smart plug (with power monitoring) (TS011F_plug_1)) switch.lazienka_bojler: Skipping sensor setup: Model not found in library (manufacturer: TuYa, model: Smart plug (with power monitoring) (TS011F_plug_1))

bramstroker commented 1 year ago

That is just some debug information of the auto discovery routine which is run during HA startup. Powercalc will look at all your entities in you whole installation and checks if it has manufacturer and model informaiton available. When it has it checks if a matching power profile is available in powercalc library. When that is not the case you'll see Skipping sensor setup: Model not found in library in the logs. I'm 99% certain this has no relation to your issue. But just to be sure you could try disabling autodiscovery enable_autodiscovery: false, but you already tried looking at one of your first comments.

salut1a commented 1 year ago

I noticed one more thing, I re-enabled the creation of energy and meters and newer Tuya devices that work in HA through Z2M in Zigbee works powercalc, while those created through Tuya integrations entities do not create.

salut1a commented 1 year ago

enable_autodiscovery now I have false all the time

bramstroker commented 1 year ago

@salut1a All these logs don't help further as they don't provide any information as to why the entities are not created, so please don't post them anymore to not pollute the issue. As long as you see Creating utility_meter sensor: than powercalc has correctly delegated entity creation to HA. Something goes wrong in HA internals, but I have no clue.

To progress any further you really have to answer the questions.

salut1a commented 1 year ago

Unless the PowerCalc just starts earlier than the Tuya and therefore the entities do not form

salut1a commented 1 year ago

so sometimes I get mixed up with settings, integrations, etc. But I also think the problem is with Tuya

by the way, you can create powercalc entities for a device that consumes variable power over time, e.g. air conditioning, for now I use the Riemann integral for it. Sorry for too many logos but I wanted to provide as much information as possible.

bramstroker commented 1 year ago

We all started once. HA has all kind of terminology which you'll need to get familiar with. It's a whole voyage ;-)

Ok good to know, it seems definitely a problem with the Tuya based sensors. I think I might need to buy some hardware to see if I can reproduce. What would I need exactly, cheapest option?

One question. Could you click on the three dots on the right bottom for one of the problematic Tuya sensors and Download diagnostics. Than share it here.

Screenshot 2023-01-07 at 16 04 31

create_energy_sensors option will just create a Riemann integral sensor under the hood. This would be similar to creating a Riemann sensor using the HA helpers. create_utility_meters option will create utility meters, same as with the HA helpers.

The options are just there to easily create these directly for you virtual power sensors (without needing to setup the helpers manually), but when you already have a real power sensor (for the air conditioner) there is no real advantage to do it via powercalc. You can however using the power_sensor_id and energy_sensor_id option to refer to an existing power sensor, see the readme.

salut1a commented 1 year ago

https://pl.aliexpress.com/item/4000478798085.html?spm=a2g0o.order_list.order_list_main.153.56091c242LTl4D&gatewayAdapt=glo2pol

I am looking for the best solution to evaluate the daily, monthly and total energy consumption for a meter that gives only power to HA through Tuya integration (although the smart application has access to daily and total energy). At constant power it works great for variables I use helper.

config_entry-powercalc-6504adf033da32916027e9aafa626f2b.json.txt

bramstroker commented 1 year ago

I will see, it will take a few weeks for order at aliexpress to arrive to the Netherlands.

Could you try one final thing and that is defining a unique_id yourself when setting up a new powercalc sensor with the GUI. First remove one of the Tuya entries from powercalc. And next add it again but fill in a unique_id. Try restarting another time and see if it survives restarts now without the need to reload.

You could also have a look into creating the power sensors for Tuya using the YAML configuration, as it was also found in the other referenced issue that it was working (also after restarts) with YAML.

sensor:
  - platform: powercalc
    entity_id: sensor.aubess_smart_socket_20a_em_power
    name: Lustro Energia
    create_utility_meters: true
    standby_power: 0.2
    ignore_unavailable_state: true
    fixed:
      power: 320
salut1a commented 1 year ago

Unfortunately, giving unique_id did not help

super :) Typing the definition into sensor.yaml worked for me enough success :)
Extra super thank you for your help, very much, I will only be left with the correct choice of calculation method for devices that only transmit power entities to have a day, a month, full

As I choose the method of integration on left, because it should be from the description of the settings, the sensor calculates too high energy consumption by converting power to daily consumption in kWh

bramstroker commented 1 year ago

I think I have found the issue by digging through the Tuya integration source code. There is some logic in place which migrates entities have unique id tuya.xxxxx to a new format. This happens during startup. Looking at the diagnostics of your powercalc config entry the unique id is tuya.bf6a6bfb475e1f6942h32ycur_power. Tuya is modifying and breaking the powercalc entities because it sees this unique id. I will make some changes in powercalc code to make the unique id prefix with pc_ or something like that to circumvent this issue.

bramstroker commented 1 year ago

I have a fix now in the master branch. Are you able to install that with HACS? You'll need to click redownload en also check the beta checkbox. After that you'll need to add the power sensor for the Tuya switch again with the GUI, and you can remove from the YAML. Now after each restart it will hopefully persist. Are you able to test that?

salut1a commented 1 year ago

ok will it work with my HA 2022.11.4 ? i find 1.2.4 in redownload

bramstroker commented 1 year ago

Yes it will work with your HA version, but you don't need powercalc 1.2.4, but install the master branch. Click redownload on integration page in HACS.

Screenshot 2023-01-07 at 20 39 51 Screenshot 2023-01-07 at 20 40 01
salut1a commented 1 year ago

when I created a new powercalc and gave reload, no entities are created

salut1a commented 1 year ago

config_entry-powercalc-051b3f7f97390444cadb5357d21024fb.json.txt

bramstroker commented 1 year ago

I doubt you are on latest master as the unique id in your config entry is still the same:

"unique_id": "tuya.bf6a6bfb475e1f6942h32ycur_power",

It should be the following now:

"unique_id": "pc_tuya.bf6a6bfb475e1f6942h32ycur_power",

Did you restart HA first after you redownloaded powercalc master branch?

salut1a commented 1 year ago

I gave reload I have now versions 1.2.4

bramstroker commented 1 year ago

You must have master not 1.2.4, see my screenshots above...

salut1a commented 1 year ago

ok i changed to master. i just directly treated your sreen where it was highlighted 1.2.4 :)

salut1a commented 1 year ago

entity not created requires reload config_entry-powercalc-d47108ccfc03f84be80f42f8198a4438.json.txt

bramstroker commented 1 year ago

You are sure you did delete the config entry before and created a new one? With "Add integration" button.

salut1a commented 1 year ago

I gave a different name I thought a new powercalc would be created, one moment :)

salut1a commented 1 year ago

deleted, restarted, created a new one in gui, not auto create config_entry-powercalc-bcbe4aaac4acfb9980d278dc6cc655ee.json.txt

bramstroker commented 1 year ago

Very strange, as your unique id is still the same. Is suspect you are still on an old version of powercalc somehow. Anyway I will release to v1.2.5 soon. Then you can try again. When it is still not working I will go forward an order some Tuya device at aliexpress to do some further testing myself.

salut1a commented 1 year ago

for me the version of the entries in sensors.yaml is a very good solution.

salut1a commented 1 year ago

to be sure, I gave reload master + restart + create new sensor again, it does not create, but for me the entry in yaml is enough

config_entry-powercalc-8c50cf2e2043c1c0273b46c35bab867a.json.txt

bramstroker commented 1 year ago

Closing this issue, as YAML is ok for you. Maybe my fix did the magic for others. I have ordered a Tuya switch anyhow, so when that arrives in coming weeks I'll also be able to test myself.

Martidjen commented 1 year ago

I see that you already closed this issue, but just wanted to let you know that I am seeing the same issue with all of my ESPHome devices. Other type of devices work fine. Might be useful information :)