Repository: bramstroker/homeassistant-powercalc

Issue:
Reload every restart
Hi
Fantastic job as always .
One issue on my side every time i restart home assistant i need to go through each entity and reload it in order to show the sensor is there a soultion for this
Regards


Discussion:
When you setup a new sensor using the GUI there is no need to restart.
Setting up a sensor using YAML requires a restart, unfortunately this is something the HA architecture enforces and there are no workaround for that.
Thanks i understand but i meant after i set the sensor with GUI and they are working perfectly. 
But the issue is when i restart HA they don't show up i need to go through each entity and reload it to make sensor show again 
Thanks 
Worthy to mention is that the first entity show sensor adter restart but i have 7 other which failed to
> Thanks i understand but i meant after i set the sensor with GUI and they are working perfectly. But the issue is when i restart HA they don't show up i need to go through each entity and reload it to make sensor show again Thanks

That's strange, did not see that behaviour before.
Any clues in the logs? When there are no errors in the logs I suggest to enable debug logging for powercalc (see last section of readme).
Btw do you have the following in `configuration.yaml` as described in installation steps.

```
powercalc:
```

That is required to run powercalc setup routine during startup.
Not sure if this can help 
Logger: homeassistant.components.sensor
Source: util/dt.py:185
Integration: Sensor (documentation, issues)
First occurred: 1:29:07 PM (2 occurrences)
Last logged: 1:29:07 PM

Error adding entities for domain sensor with platform utility_meter
Error while setting up utility_meter platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 428, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 691, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 776, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/utility_meter/sensor.py", line 512, in async_added_to_hass
    dt_util.parse_datetime(state.attributes.get(ATTR_LAST_RESET))
  File "/usr/src/homeassistant/homeassistant/util/dt.py", line 185, in parse_datetime
    return ciso8601.parse_datetime(dt_str)
TypeError: argument 1 must be str, not None
![SmartSelect_20221011_133508_Home Assistant.jpg](https://user-images.githubusercontent.com/96690422/195068366-0e09d721-f575-4998-9e95-55a66e0869a8.jpg)![SmartSelect_20221011_133457_Home Assistant.jpg](https://user-images.githubusercontent.com/96690422/195068382-d75351b7-1985-4265-9deb-c2216e07bf1d.jpg)
Not seen this error before, but that's definitely not something which should happen.
Not sure what causes that yet. Seem to be some corrupt state of your utility meter.

Which version of powercalc and HA do you use?
You shouldn't really edit that one, but I suggest you to take a look at `config/.storage/core.restore_state` and remove all the powercalc utility meter entries from it, as there seem to be some invalid state attributes there.
After each restart HA will have a look here and restore entities to reflect the last state.
Hopefully that will make the error disappear.
I did delete them but after restarting they appear again in the file 😅 i saved the file before restarting 
Im running 0.26.1 with HA 9.2
Could you try stopping HA, then delete and start again?
HA will write the state from memory to restore_state when it is shutdown (also happens when you restart), so that would be the reason they appear again.
Sorry i did not understand how i can stop HA and edit the file !!! Will not be able to access the file 
hmm yes, I understand your issue. I have ssh access to my HA instance so I can edit that way.
Anyways I suggest to just wait 24 hours to see if the error disappears. I have good hopes when the utility meter has calculated a new state the error will just disappear. 
Still not sure if this is related to your other powercalc sensors not being setup correctly, but let's first wait a bit until investigating that further.
Thank you for putting time to this will share what i found after 48 hrs
> hmm yes, I understand your issue. I have ssh access to my HA instance so I can edit that way.
> Anyways I suggest to just wait 24 hours to see if the error disappears. I have good hopes when the utility meter has calculated a new state the error will just disappear. 
> Still not sure if this is related to your other powercalc sensors not being setup correctly, but let's first wait a bit until investigating that further.

Hi i found out that the issue is powercalc start before the integration of the source entity in this case tuya .
So on the next step where HA needs to start and reload tuya integration powercalc lose the values and never return them till i manually reload the entity. 
Is their a way to fire powercalc after tuya integration 
Regards
Powercalc should not be dependant on loading order of other integrations. You could try adding tuya to dependencies in manifest.json in custom_components/powercalc directory. But this is not something I can (and will) add for all kind of integrations. 
Powercalc should populate the value for power sensor after HA is fully started or when the underlying source entity changes state. The second condition should happen when Tuya does it's thing later than powercalc.
Hard to say what exactly goes wrong on your installation. 
Could you please enable debug logging for powercalc? That should hopefully give some more pointers what is exactly happening. Otherwise it is just a guessing game.
> Powercalc should not be dependant on loading order of other integrations. You could try adding tuya to dependencies in manifest.json in custom_components/powercalc directory. But this is not something I can (and will) add for all kind of integrations. 
> Powercalc should populate the value for power sensor after HA is fully started or when the underlying source entity changes state. The second condition should happen when Tuya does it's thing later than powercalc.
> Hard to say what exactly goes wrong on your installation. 
> Could you please enable debug logging for powercalc? That should hopefully give some more pointers what is exactly happening. Otherwise it is just a guessing game.

Note:
I already enabled it nothing relative in the log
There should be numerous log entries when setting the log level to debug. You should at least see a few "Creating power sensor ..." entries. For each powercalc sensor you should see more than 2 lines, sometimes even 10 lines.

What does your configuration.yaml look like now?
> > Powercalc should not be dependant on loading order of other integrations. You could try adding tuya to dependencies in manifest.json in custom_components/powercalc directory. But this is not something I can (and will) add for all kind of integrations. 
> > Powercalc should populate the value for power sensor after HA is fully started or when the underlying source entity changes state. The second condition should happen when Tuya does it's thing later than powercalc.
> > Hard to say what exactly goes wrong on your installation. 
> > Could you please enable debug logging for powercalc? That should hopefully give some more pointers what is exactly happening. Otherwise it is just a guessing game.
> 
> Note:
> I already enabled it nothing relative in the log

Wow HA did not start i needed to setup it again and go to a backup 😅😅

For the yaml file it is like this
![SmartSelect_20221012_231048_Home Assistant.jpg](https://user-images.githubusercontent.com/96690422/195438770-7c4a4339-9a2f-4a5c-a6f9-292f97e55903.jpg)
> > > Powercalc should not be dependant on loading order of other integrations. You could try adding tuya to dependencies in manifest.json in custom_components/powercalc directory. But this is not something I can (and will) add for all kind of integrations. 
> > > Powercalc should populate the value for power sensor after HA is fully started or when the underlying source entity changes state. The second condition should happen when Tuya does it's thing later than powercalc.
> > > Hard to say what exactly goes wrong on your installation. 
> > > Could you please enable debug logging for powercalc? That should hopefully give some more pointers what is exactly happening. Otherwise it is just a guessing game.
> > 
> > Note:
> > I already enabled it nothing relative in the log
> 
> Wow HA did not start i needed to setup it again and go to a backup 😅😅
> 
> For the yaml file it is like this
> ![SmartSelect_20221012_231048_Home Assistant.jpg](https://user-images.githubusercontent.com/96690422/195438770-7c4a4339-9a2f-4a5c-a6f9-292f97e55903.jpg)

I found those registered for all sensors i copied samples
(MainThread) [custom_components.powercalc] switch.wifi_switch: Model not found in library, skipping auto configuration
2022-10-12 23:09:52.146 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.roof_round_light_switch_1: Auto discovered model (manufacturer=Tuya, model=k34prlxwjymp50cx)
2022-10-12 23:09:52.147 DEBUG (MainThread) [custom_components.powercalc] switch.roof_round_light_switch_1: Model not found in library, skipping auto configuration
2022-10-12 23:09:52.147 DEBUG (MainThread) [custom_components.powercalc] Done auto discovering entities




.....




(MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh bed light energy
2022-10-12 23:09:52.697 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] light.left_ground_cabinet: Auto discovered model (manufacturer=Tuya, model=tmy7lcbauqtcy2cc)
2022-10-12 23:09:52.698 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=light.left_ground_cabinet entity_category=None, sensor_name=Wh kitchen cabines power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bfce0866448823bca1qyq1switch_led)
2022-10-12 23:09:52.698 DEBUG


Thanks
So for example `Wh kitchen cabines power` is missing and not working after startup?
Because the logs say it is created correctly, I don't see any issues here.
and you are right even when i manually reload the entity it repeats the same steps and works fine the only difference that during the startup after restart those steps is not in order !!
so this is the log after manual reload
2022-10-14 00:06:23.676 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.aubess_smart_switch_em_switch_1 entity_category=None, sensor_name=Wh water pump power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bf84a549f8de279e7dlvufswitch_1)
2022-10-14 00:06:23.676 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh water pump energy
2022-10-14 00:06:23.680 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.wh_water_pump_power to device 1b9bb4c6595cacbf12972b728a2bc0e0
2022-10-14 00:06:23.688 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.aubess_smart_switch_em_switch_1: State changed to "off". Power:0.00
2022-10-14 00:06:23.705 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.wh_water_pump_energy to device 1b9bb4c6595cacbf12972b728a2bc0e0
Please share the full logs after startup completed, all these different snippets won't help a bit an seems fine. Because I also don't see the errors you were initially talking about.
You can copy paste that to a txt file and attach to the reply.
[powercalc.txt](https://github.com/bramstroker/homeassistant-powercalc/files/9791641/powercalc.txt)
powercalc full log

The relevant log entries indicating the creation of the power and energy sensors are here:

```
2022-10-15 11:52:01.878 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.heater_socket_1: Auto discovered model (manufacturer=Tuya, model=j0zozzoarutv0nu1)
2022-10-15 11:52:01.878 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.heater_socket_1 entity_category=None, sensor_name=Wh heater power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bfaab7ed5688fc5520uo7jswitch_1)
2022-10-15 11:52:01.879 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh heater energy
2022-10-15 11:52:01.879 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.abood_tv: Auto discovered model (manufacturer=LG, model=55LH600V-TB)
2022-10-15 11:52:01.880 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.abood_tv entity_category=None, sensor_name=Wh abood tv power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=ba7c3cd6-9b15-a6eb-9748-78d22fc07970)
2022-10-15 11:52:01.881 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh abood tv energy
2022-10-15 11:52:01.881 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.exhust_fan_switch_1: Auto discovered model (manufacturer=Tuya, model=ogupymaavjyzigvr)
2022-10-15 11:52:01.882 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.exhust_fan_switch_1 entity_category=None, sensor_name=Wh exhust fan power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bf8893cd4ab7ad91311xtsswitch_1)
2022-10-15 11:52:01.882 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh exhust fan energy
2022-10-15 11:52:01.882 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] light.bed: Auto discovered model (manufacturer=Tuya, model=tmy7lcbauqtcy2cc)
2022-10-15 11:52:01.883 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=light.bed entity_category=None, sensor_name=Wh bed light power strategy=LinearStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bf8262a095cda3846emmxnswitch_led)
2022-10-15 11:52:01.883 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh bed light energy
2022-10-15 11:52:01.883 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] light.left_ground_cabinet: Auto discovered model (manufacturer=Tuya, model=tmy7lcbauqtcy2cc)
2022-10-15 11:52:01.884 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=light.left_ground_cabinet entity_category=None, sensor_name=Wh kitchen cabines power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bfce0866448823bca1qyq1switch_led)
2022-10-15 11:52:01.884 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh kitchen cabines energy
2022-10-15 11:52:01.884 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.aubess_smart_switch_em_switch_1: Auto discovered model (manufacturer=Tuya, model=labfkgzm2ikdcpdp)
2022-10-15 11:52:01.885 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.aubess_smart_switch_em_switch_1 entity_category=None, sensor_name=Wh water pump power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bf84a549f8de279e7dlvufswitch_1)
2022-10-15 11:52:01.885 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh water pump energy
2022-10-15 11:52:01.886 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.wall_lights_switch_1: Auto discovered model (manufacturer=Tuya, model=ogupymaavjyzigvr)
2022-10-15 11:52:01.886 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.wall_lights_switch_1 entity_category=None, sensor_name=Wh wall lights power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bf7e3c5b8d03bc8358fsmkswitch_1)
2022-10-15 11:52:01.886 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh wall lights energy
2022-10-15 11:52:01.887 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] switch.roof_round_light_switch_1: Auto discovered model (manufacturer=Tuya, model=k34prlxwjymp50cx)
2022-10-15 11:52:01.887 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=switch.roof_round_light_switch_1 entity_category=None, sensor_name=Wh roof round light power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=tuya.bfbcfa5c9692fa4ef9rictswitch_1)
2022-10-15 11:52:01.887 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh roof round light energy
2022-10-15 11:52:01.887 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] climate.bedroom_ac_gree: Auto discovered model (manufacturer=Gree, model=None)
2022-10-15 11:52:01.888 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=climate.bedroom_ac_gree entity_category=None, sensor_name=Wh bedroom ac power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=502cc68516c2)
2022-10-15 11:52:01.888 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh bedroom ac energy
2022-10-15 11:52:01.888 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] climate.office_ac: Auto discovered model (manufacturer=Gree, model=None)
2022-10-15 11:52:01.889 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=climate.office_ac entity_category=None, sensor_name=Wh office ac power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=502cc6853a6d)
2022-10-15 11:52:01.889 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh office ac energy
2022-10-15 11:52:01.889 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] climate.living_room_ac: Auto discovered model (manufacturer=Gree, model=None)
2022-10-15 11:52:01.890 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=climate.living_room_ac entity_category=None, sensor_name=Wh living room ac power strategy=FixedStrategy manufacturer= model= standby_power=0.00 unique_id=502cc684ba5b)
2022-10-15 11:52:01.890 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Wh living room ac energy
```

I don't see anything wrong with that. All this is exactly as expected. After that you must have several power and energy sensors available in HA.

You can verify that in the entities list, developer tools or you can click on the entry in the GUI like this screenshot.

<img width="295" alt="Screenshot 2022-10-15 at 11 07 36" src="https://user-images.githubusercontent.com/2345875/195978624-d44c8a7f-f715-446a-b333-851aadd6a055.png">

After HA fully started I see the following in the logs, which is powercalc handling the initial state update for the power sensor.
For `climate.bedroom_ac_gree` and `switch.heater_socket_1` there is a problem with the state of these entities. That's why powercalc cannot calculate the power for that and it will make the power and energy sensor unavailable.

```
2022-10-15 11:52:07.788 DEBUG (MainThread) [custom_components.powercalc.sensors.power] light.bed: State changed to "off". Power:0.00
2022-10-15 11:52:07.789 DEBUG (MainThread) [custom_components.powercalc.sensors.power] light.left_ground_cabinet: State changed to "off". Power:0.00
2022-10-15 11:52:07.797 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.roof_round_light_switch_1: State changed to "off". Power:0.00
2022-10-15 11:52:07.798 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.aubess_smart_switch_em_switch_1: State changed to "off". Power:0.00
2022-10-15 11:52:07.798 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.heater_socket_1: Source entity has an invalid state, setting power sensor to unavailable
2022-10-15 11:52:07.798 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.wall_lights_switch_1: State changed to "off". Power:0.00
2022-10-15 11:52:07.798 DEBUG (MainThread) [custom_components.powercalc.sensors.power] switch.exhust_fan_switch_1: State changed to "off". Power:0.00

2022-10-15 11:52:09.035 DEBUG (MainThread) [custom_components.powercalc.sensors.power] media_player.abood_tv: State changed to "on". Power:81.00
2022-10-15 11:52:09.035 DEBUG (MainThread) [custom_components.powercalc.sensors.power] climate.bedroom_ac_gree: Source entity has an invalid state, setting power sensor to unavailable
2022-10-15 11:52:09.036 DEBUG (MainThread) [custom_components.powercalc.sensors.power] climate.office_ac: State changed to "off". Power:0.00
2022-10-15 11:52:09.037 DEBUG (MainThread) [custom_components.powercalc.sensors.power] climate.living_room_ac: State changed to "off". Power:0.00
2022-10-15 11:52:09.045 DEBUG (MainThread) [custom_components.powercalc.sensors.power] climate.bedroom_ac_gree: Source entity has an invalid state, setting power sensor to unavailable
2022-10-15 11:52:09.046 DEBUG (MainThread) [custom_components.powercalc.sensors.power] climate.bedroom_ac_gree: Source entity has an invalid state, setting power sensor to unavailable
```
That is really unusual 🤔 they are not available cause tuya is loading at that point !!
In the screen shot how they looks after restarting then i need to reload each one separately.
I can't reload the whole integration at once ![SmartSelect_20221015_124826_Home Assistant.jpg](https://user-images.githubusercontent.com/96690422/195980157-9279e529-f61a-4930-bad9-8c59df597936.jpg)
Very very strange, there should be entities listed here. I am not able to reproduce yet, impossible to solve if I cannot debug the issue and have no clue what could be the cause. Also don't have TuYa, so cannot test with that. There is one other user reporting a similar issue on community forum and he has the androidTV integration. I will have a look if I can reproduce the issue with that one.
Could you have a look into the logs after you reload this one "Wh kitchen cabines", then we can compare if all these lines are exactly the same as the logs after HA startup.
And you were saying one powercalc sensor was working correctly after startup. Which one is that exactly?
I have similar issue maybe the same. I set up powercalc to calculate power usage on my lights controlled by smart switches and smart bulbs. Everything is working fine.
I set up powercalc to monitor my tv energy consumption but when I restart my ha power / energy sensors created by powercalc just dissaper.
When I reload powercalc for tv they just reaper.
I use android tv integration and had set up powercalc using media_player.android_tv_192_168_31_5 entity.
I don't have any error in logs related to powercalc, but I didn't enabled debug mode.
I set up power consumption on wifi smart switches integrated with tuya integration and zigbe smart switches and smart bulbs integrated with zigbee2mqtt and mqtt integration.
In my opinion this problem is somehow entities related but I don't know how exactly to trace this problem.
@dandek01 You only have a problem with a single powercalc sensor, right? The android TV one, all the others are working fine also after restart?
Could you have a look into `config/.storage/core.config_entries` and search for the powercalc sensor here. than copy the JSON data for that one and share it here.
Also which versions of HA and powercalc are you guys on?
Yes, I have only problem with single powerccalc sensor, everything else is working fine after restart.
This is androidtv related data
`{
        "entry_id": "49c3700f7523318e9e99b1b24654dbc6",
        "version": 1,
        "domain": "androidtv",
        "title": "192.168.31.5",
        "data": {
          "host": "192.168.31.5",
          "device_class": "androidtv",
          "port": 5555,
          "adb_server_port": 5037
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "04:7a:0b:a1:a7:3d",
        "disabled_by": null
      },`
`   {
        "entry_id": "95e78e9ed256afba0a94698f40006930",
        "version": 1,
        "domain": "nfandroidtv",
        "title": "Android TV",
        "data": {
          "host": "192.168.31.5",
          "name": "Android TV"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "192.168.31.5",
        "disabled_by": null
      },`
Maybe powercalc integration is loaded before android tv integration. 
Hi I'm using 26.1
this is what i found on that file:
the problem on my side is only with Tuya switches the rest like ACs TVs Sonoff lights is ok

```json
      {
        "entry_id": "3c1042d4cbf0739edf4a54770eacc09c",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh heater",
        "data": {
          "entity_id": "switch.heater_socket_1",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh heater",
          "fixed": {
            "power": 2100.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bfaab7ed5688fc5520uo7jswitch_1",
          "standby_power": 0.0
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bfaab7ed5688fc5520uo7jswitch_1",
        "disabled_by": null
      },
      {
        "entry_id": "3b00681d23ba11a260d43f36ff40fec8",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh abood tv",
        "data": {
          "entity_id": "media_player.abood_tv",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh abood tv",
          "fixed": {
            "power": 81.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "ba7c3cd6-9b15-a6eb-9748-78d22fc07970"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "ba7c3cd6-9b15-a6eb-9748-78d22fc07970",
        "disabled_by": null
      },
      {
        "entry_id": "4b884f7fcefe5d766befb56e3a8ef84d",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh exhust fan",
        "data": {
          "entity_id": "switch.exhust_fan_switch_1",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh exhust fan",
          "fixed": {
            "power": 40.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bf8893cd4ab7ad91311xtsswitch_1"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bf8893cd4ab7ad91311xtsswitch_1",
        "disabled_by": null
      },
      {
        "entry_id": "f9c63385bd959cd518ace920a94c6661",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh bed light",
        "data": {
          "entity_id": "light.bed",
          "mode": "linear",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh bed light",
          "standby_power": 0.0,
          "linear": {
            "min_power": 3.0,
            "max_power": 25.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bf8262a095cda3846emmxnswitch_led"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bf8262a095cda3846emmxnswitch_led",
        "disabled_by": null
      },
      {
        "entry_id": "c6286894f4825400f13bc83136b3a931",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh kitchen cabines",
        "data": {
          "entity_id": "light.left_ground_cabinet",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh kitchen cabines",
          "standby_power": 0.0,
          "fixed": {
            "power": 30.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bfce0866448823bca1qyq1switch_led"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bfce0866448823bca1qyq1switch_led",
        "disabled_by": null
      },
      {
        "entry_id": "6c0ee348679ab9d08069b1144dd0da12",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh water pump",
        "data": {
          "entity_id": "switch.aubess_smart_switch_em_switch_1",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh water pump",
          "standby_power": 0.0,
          "fixed": {
            "power": 700.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bf84a549f8de279e7dlvufswitch_1"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bf84a549f8de279e7dlvufswitch_1",
        "disabled_by": null
      },
      {
        "entry_id": "2e3466c8ee1c628d8c72fb519355ceb7",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh wall lights",
        "data": {
          "entity_id": "switch.wall_lights_switch_1",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh wall lights",
          "standby_power": 0.0,
          "fixed": {
            "power": 27.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bf7e3c5b8d03bc8358fsmkswitch_1"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bf7e3c5b8d03bc8358fsmkswitch_1",
        "disabled_by": null
      },
      {
        "entry_id": "577f3aab655f3fd9d7b766ee726caaf7",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh roof round light",
        "data": {
          "entity_id": "switch.roof_round_light_switch_1",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh roof round light",
          "standby_power": 0.0,
          "fixed": {
            "power": 95.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "tuya.bfbcfa5c9692fa4ef9rictswitch_1"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "tuya.bfbcfa5c9692fa4ef9rictswitch_1",
        "disabled_by": null
      },
      {
        "entry_id": "280497623fe2ed1daba6bf4775593f28",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh bedroom ac",
        "data": {
          "entity_id": "climate.bedroom_ac_gree",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh bedroom ac",
          "fixed": {
            "power_template": "{% if states ('climate.bedroom_ac_gree') == 'cool' and state_attr ('climate.bedroom_ac_gree' , 'current_temperature') - state_attr ('climate.bedroom_ac_gree' , 'temperature') >0 %}\n1000\n{% elif states ('climate.bedroom_ac_gree') == 'cool' and state_attr ('climate.bedroom_ac_gree' , 'current_temperature') - state_attr ('climate.bedroom_ac_gree' , 'temperature') <=0 %}\n100\n{% elif states ('climate.bedroom_ac_gree') == 'heat' and state_attr ('climate.bedroom_ac_gree' , 'current_temperature') - state_attr ('climate.bedroom_ac_gree' , 'temperature') <0 %}\n1600\n{% elif states ('climate.bedroom_ac_gree') == 'heat' and state_attr ('climate.bedroom_ac_gree' , 'current_temperature') - state_attr ('climate.bedroom_ac_gree' , 'temperature') >0 %}\n500\n{% elif states ('climate.bedroom_ac_gree') == 'fan_only' %}\n90\n{% endif %}",
            "states_power": {}
          },
          "sensor_type": "virtual_power",
          "unique_id": "502cc68516c2"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "502cc68516c2",
        "disabled_by": null
      },
      {
        "entry_id": "6aebb74dfa9f940a04b68740d6a715a9",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh office ac ",
        "data": {
          "entity_id": "climate.office_ac",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh office ac ",
          "fixed": {
            "power_template": "{% if states ('climate.office_ac') == 'cool' and state_attr ('climate.office_ac' , 'current_temperature') - state_attr ('climate.office_ac' , 'temperature') > 0 %}\n1000\n{% elif states ('climate.office_ac') == 'cool' and state_attr ('climate.office_ac' , 'current_temperature') - state_attr ('climate.office_ac' , 'temperature') <= 0 %}\n100\n{% elif states ('climate.office_ac') == 'heat' and state_attr ('climate.office_ac' , 'current_temperature') - state_attr ('climate.office_ac' , 'temperature') < 0 %}\n1500\n{% elif states ('climate.office_ac') == 'heat' and state_attr ('climate.office_ac' , 'current_temperature') - state_attr ('climate.office_ac' , 'temperature') > 0 %}\n400\n{% elif states ('climate.office_ac') == 'fan_only'%}\n90\n{% endif %}",
            "states_power": {}
          },
          "sensor_type": "virtual_power",
          "unique_id": "502cc6853a6d",
          "calculation_enabled_condition": "for:"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "502cc6853a6d",
        "disabled_by": null
      },
      {
        "entry_id": "1e2e295cf9278f667ccd3bccd2f6dea4",
        "version": 1,
        "domain": "powercalc",
        "title": "Wh living room ac",
        "data": {
          "entity_id": "climate.living_room_ac",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": false,
          "name": "Wh living room ac",
          "fixed": {
            "power_template": "{% if states ('climate.living_room_ac') == 'cool' and state_attr ('climate.living_room_ac' , 'current_temperature') - state_attr ('climate.living_room_ac' , 'temperature') > 0 %}\n1300\n{% elif states ('climate.living_room_ac') == 'cool' and state_attr ('climate.living_room_ac' , 'current_temperature') - state_attr ('climate.living_room_ac' , 'temperature') <= 0 %}\n90\n{% elif states ('climate.living_room_ac') == 'heat' and state_attr ('climate.living_room_ac' , 'current_temperature') - state_attr ('climate.living_room_ac' , 'temperature') < 0 %}\n2000\n{% elif states ('climate.living_room_ac') == 'heat' and state_attr ('climate.living_room_ac' , 'current_temperature') - state_attr ('climate.living_room_ac' , 'temperature') > 0 %}\n400\n{% elif states ('climate.living_room_ac') == 'fan_only' %}\n90\n{% endif %}",
            "states_power": {}
          },
          "sensor_type": "virtual_power",
          "unique_id": "502cc684ba5b"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "502cc684ba5b",
        "disabled_by": null
      },
      {
        "entry_id": "ded709d951c73307fe19acc362ae4236",
        "version": 1,
        "domain": "utility_meter",
        "title": "Wh kitchen consumption",
        "data": {},
        "options": {
          "name": "Wh kitchen consumption",
          "source": "sensor.water_pump_power",
          "cycle": "daily",
          "offset": 0.0,
          "tariffs": [],
          "net_consumption": false,
          "delta_values": false
        },
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": null,
        "disabled_by": null
      }
    ]
  }
}
```
> Yes, I have only problem with single powerccalc sensor, everything else is working fine after restart. This is androidtv related data `{ "entry_id": "49c3700f7523318e9e99b1b24654dbc6", "version": 1, "domain": "androidtv", "title": "192.168.31.5", "data": { "host": "192.168.31.5", "device_class": "androidtv", "port": 5555, "adb_server_port": 5037 }, "options": {}, "pref_disable_new_entities": false, "pref_disable_polling": false, "source": "user", "unique_id": "04:7a:0b:a1:a7:3d", "disabled_by": null },` ` { "entry_id": "95e78e9ed256afba0a94698f40006930", "version": 1, "domain": "nfandroidtv", "title": "Android TV", "data": { "host": "192.168.31.5", "name": "Android TV" }, "options": {}, "pref_disable_new_entities": false, "pref_disable_polling": false, "source": "user", "unique_id": "192.168.31.5", "disabled_by": null },`

There should also be an entry with `"domain": "powercalc"`, could you include that one as well, most interested in that.
> Very very strange, there should be entities listed here. I am not able to reproduce yet, impossible to solve if I cannot debug the issue and have no clue what could be the cause. Also don't have TuYa, so cannot test with that. There is one other user reporting a similar issue on community forum and he has the androidTV integration. I will have a look if I can reproduce the issue with that one. Could you have a look into the logs after you reload this one "Wh kitchen cabines", then we can compare if all these lines are exactly the same as the logs after HA startup. And you were saying one powercalc sensor was working correctly after startup. Which one is that exactly?

sorry overlooked this one it is Abood Tv (not android) it is webos LG and the integration used is media player
There it is

`
        "entry_id": "8c443e5aa70c76185dd57b83409c019d",
        "version": 1,
        "domain": "powercalc",
        "title": "Android TV",
        "data": {
          "entity_id": "media_player.android_tv_192_168_31_5",
          "mode": "fixed",
          "create_energy_sensor": true,
          "create_utility_meters": true,
          "name": "Android TV",
          "standby_power": 5.0,
          "fixed": {
            "power": 45.0
          },
          "sensor_type": "virtual_power",
          "unique_id": "04:7a:0b:a1:a7:3d"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "04:7a:0b:a1:a7:3d",
        "disabled_by": null
      }`
@screem20056 @dandek01 Thanks for providing this extra information. Hopefully I will be able to reproduce something. Not sure when I can have a further look, quite busy the coming days.

Only thing missing is which HA version you are using, and @dandek01 which powercalc version.

Would be nice if any of you would be able to test with latest v1.0.0 beta. Maybe it's already resolved in that one and we are wasting our precious time here.
I'm using
Home Assistant 2022.10.4
Supervisor 2022.10.0
and powercalc version .. 0.26.1

I'm installing v1.0.0.0 beta 6 to try it.
Im using HA version. core-2022.10.4
I tested 1.0.0 beta 6 same issue.
Thanks for support 
No, problem stays the same. There is no entities after restart, energy dashboard says
`Entity not defined
Check the integration or your configuration that provides:
sensor.android_tv_energy`
Reloading powercalc resolve the issue. Powercalc version v1.0.0-beta.6
I have no clue if this can help but once HA start to boot i do not lose entity immediately have a look to the restart record from my phone 😄

https://linksharing.samsungcloud.com/kvp3y51p4JHr
I have upgraded my test HA instance to 2022.10.4, Upgraded powercalc to v1.0.0-beta.6. 
Setup AndroidTV integration for my TV.
Next create a powercalc sensor using the GUI with fixed strategy for this android tv media player, similar configuration as @dandek01.
After every restart the entities are just create correctly and don't disappear for me :-(

<img width="299" alt="Screenshot 2022-10-15 at 13 23 20" src="https://user-images.githubusercontent.com/2345875/195983885-a00372df-1143-4823-9d36-5656bd5df3d9.png">

Maybe it's a timing issue between integrations which is triggered for you but nog for me. As a lot of things are happening in parallel in HA execution. My hardware is Intel NUC, so that's quite overpowered for running HA.

@dandek01 Could you try adding `androidtv` to dependencies in `config/custom_components/powercalc/manifest.json`, and restart another time?
Unfortunately it doesnt work. This is part of manifest,json 
 "dependencies": [
        "light",
        "group",
        "template",
        "select",
        "utility_meter",
        "androidtv"
    ], 
I'm running ha on debian linux. Comp has plenty of power, i3 3.4GHz cpu, 8 gb of ram, 2 ssd hdd. 
@dandek01 hmm that's a pitty. @dandek01 could you also share your powercalc logs after restart? Hopefully your logs give some pointers.
The only thing I can think of is that my tv is connected to my router via powerline adapter. I use static ip to ping my tv from ha. in combination with android remote app installed on tv I can use my tv as casting device for my dorbell. The problem might be somewhere in integrations startup, I don't know.
But I could try this integration on another tv, just to see will it work there. 
Yeah, I created two powercalc meters. One for my Samsung TV and other for my alexa. Both are connected over wifi and there is no problems with powerclac integration after restart. Entities are there. My android tv that is connected to my router with lan cable and has wifi disabled has the same problem. Entities disappeared after ha restart and have to manually reload it to reappear.  
I edit my configuration.yaml file with

logger:
  default: warning
  logs:
    custom_components.powercalc: debug

but there is no error in powercalc integration.
This problem is probably somehow network related and it occurs when system is restating. When system is fully reloaded there is no problem.
@dandek01 I meant the lines starting with `DEBUG (MainThread) [custom_components.powercalc`. I was not expecting errors on your HA as these would be logged previously as well without any additional logging configuration.
You can also enable android TV debug logging.

```yaml
logger:
  default: warning
  logs:
    custom_components.powercalc: debug
    homeassistant.components.androidtv: debug
```

How does your powercalc card for the androidTV look like after startup? It also does not list any entities and it's blue, right?
Similar as @screem20056 

I enabled androidtv and powercalc logger. Nothing in the logs. I disconnected my lan cable from the tv and connected it to wifi. Still nothing, problem is the same.
Removed androidtv integration and integrated tv again just using wifi, still no progress.
When I restarted ha all entities disappeared just like in @screem20056 case and have to manually reload it to reaper.
On startup it doesnt list any entities and it's blue configure button and I can open configuration.
> I enabled androidtv and powercalc logger. Nothing in the logs. I disconnected my lan cable from the tv and connected it to wifi. Still nothing, problem is the same.
> Removed androidtv integration and integrated tv again just using wifi, still no progress.
> When I restarted ha all entities disappeared just like in @screem20056 case and have to manually reload it to reaper.
> On startup it doesnt list any entities and it's blue configure button and I can open configuration.

😱😱😱😱 welcome to the club 
For the entities that work do you have tuya one  of them
Yes, I do. I have tuya wifi switches and they work with powercalc, no issue. I integrated them with localtuya and tuya integration. 
> Yes, I do. I have tuya wifi switches and they work with powercalc, no issue. I integrated them with localtuya and tuya integration. 

Do you have a repeater or wifi extender in the middle or it is directly connected to the router ?
I have wifi mesh network and I'm using xiaomi routers for it. I don't have wifi extender. I tried it and it doesnt work. All switches are directly connected to a router. But look I had a lot of problems with tuya wifi switches and majority was replaced with zigbee ones. Well they are again tuya based, its hard to dodge them, but now at they are working as expected.
My network is kinda complicated. I have two local routers, one cisco dual wan router served as a gateway for local network and two lte routers for internet access. So I have five routers in my house currently.
> I enabled androidtv and powercalc logger. Nothing in the logs.

You must have done something wrong configuration wise or not restarted after changing the configuration. Maybe you have several `logger:` entries, the second one overwriting the first.
There must be at least 10's of log lines after you restarted.


Yeah, you right. I should look at full log. This is part of it for androidtv

```
2022-10-16 13:32:29.843 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Aqara, model=Android TV)
2022-10-16 13:32:29.843 DEBUG (MainThread) [custom_components.powercalc] media_player.android_tv_192_168_31_5: Model not found in library, skipping discovery
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Aqara, model=Android TV)
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.android_tv_192_168_31_5 entity_category=None, sensor_name=Android TV power strategy=FixedStrategy manufacturer= model= standby_power=5.00 unique_id=04:7a:0b:a1:a7:3d)
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Android TV energy
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy daily (entity_id=sensor.android_tv_energy_daily)
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy weekly (entity_id=sensor.android_tv_energy_weekly)
2022-10-16 13:32:29.916 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy monthly (entity_id=sensor.android_tv_energy_monthly)
022-10-16 13:32:29.955 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_power to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:32:30.077 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:32:30.077 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_daily to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:32:30.078 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_weekly to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:32:30.083 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_monthly to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:32:39.865 DEBUG (MainThread) [custom_components.powercalc.sensors.power] media_player.android_tv_192_168_31_5: State changed to "off". Power:5.00
```

After restart there is no powercalc entities on android tv

After I reload androidtv in powercalc I got this in my logs

```
2022-10-16 13:41:02.643 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Beijing Xiaomi Electronics Co., Ltd., model=Android TV)
2022-10-16 13:41:02.643 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.android_tv_192_168_31_5 entity_category=None, sensor_name=Android TV power strategy=FixedStrategy manufacturer= model= standby_power=5.00 unique_id=04:7a:0b:a1:a7:3d)
2022-10-16 13:41:02.643 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Android TV energy
2022-10-16 13:41:02.644 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy daily (entity_id=sensor.android_tv_energy_daily)
2022-10-16 13:41:02.644 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy weekly (entity_id=sensor.android_tv_energy_weekly)
2022-10-16 13:41:02.644 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy monthly (entity_id=sensor.android_tv_energy_monthly)
2022-10-16 13:41:02.645 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_power to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:41:02.649 DEBUG (MainThread) [custom_components.powercalc.sensors.power] media_player.android_tv_192_168_31_5: State changed to "off". Power:5.00
2022-10-16 13:41:02.661 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:41:02.661 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_daily to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:41:02.662 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_weekly to device 97342280dac1048fa07af4313be90b34
2022-10-16 13:41:02.663 DEBUG (MainThread) [custom_components.powercalc.sensors.abstract] Binding sensor.android_tv_energy_monthly to device 97342280dac1048fa07af4313be90b34
```
Yes, this is all looking fine. Similar as @screem20056. I see no indications of any issues in the logs.
Could you check the following file `config/.storage/core.entity_registry` after startup and see if it contains the powercalc power and energy sensors?
I restarted again. No, there isn't. All others switches and lights have powercalc entities but androidtv doesnt have any.
This is all I have for androidtv. I didnt reload powercalc for androidtv

    {
        "area_id": "daniel_bedroom",
        "capabilities": {},
        "config_entry_id": "ccd4664092c798dbb883e7dc5648b072",
        "device_class": null,
        "device_id": "97342280dac1048fa07af4313be90b34",
        "disabled_by": null,
        "entity_category": null,
        "entity_id": "media_player.android_tv_192_168_31_5",
        "hidden_by": null,
        "icon": null,
        "id": "24b59ffa38c7b7765c35a11abfd6b319",
        "has_entity_name": false,
        "name": null,
        "options": {},
        "original_device_class": "tv",
        "original_icon": null,
        "original_name": "Android TV 192.168.31.5",
        "platform": "androidtv",
        "supported_features": 23997,
        "unique_id": "04:7a:0b:a1:a7:3d",
        "unit_of_measurement": null
      },
Could you add the following line at line 312 of `config/custom_components/powercalc/sensor.py`

`_LOGGER.debug("Adding entities: %s", [entity.entity_id for entity in entities.new if isinstance(entity, SensorEntity)])`

So it reads:

```python
    if entities:
        _LOGGER.debug("Adding entities: %s", [entity.entity_id for entity in entities.new if isinstance(entity, SensorEntity)])
        async_add_entities(
```

And restart again.
I did but there is no powercalc entites. I have in manifest.json

    "dependencies": [
        "light",
        "group",
        "template",
        "select",
        "utility_meter",
        "androidtv"
    ],
Didn't change it back.

This is sensor.py from line 311

    if entities:
        _LOGGER.debug("Adding entities: %s", [entity.entity_id for entity in entities.new if isinstance(entity, SensorEntity)])
        async_add_entities(
        )

In config/.storage/core.entity_registry nothing changed. There is no powercalc entities for android tv
This addition will only add an additional line to the logs, it will not solve anything yet as I still don't have any clue why it is not working as supposed to be. Adding some logs to get a grasp of what is going on, because I'm unable to reproduce it myself. Just search for `Adding entities:` in the full logs after restart and share the result.
This is what I found in the logs

```
2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Aqara, model=Android TV)
2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.android_tv_192_168_31_5 entity_category=None, sensor_name=Android TV power strategy=FixedStrategy manufacturer= model= standby_power=5.00 unique_id=04:7a:0b:a1:a7:3d)
2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Android TV energy
2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy daily (entity_id=sensor.android_tv_energy_daily)
2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy weekly (entity_id=sensor.android_tv_energy_weekly)
2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy monthly (entity_id=sensor.android_tv_energy_monthly)
2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.android_tv_power', 'sensor.android_tv_energy', 'sensor.android_tv_energy_daily', 'sensor.android_tv_energy_weekly', 'sensor.android_tv_energy_monthly']
```

and in the end I have this

```
2022-10-16 14:15:53.411 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.dinning_room_light_power', 'sensor.dinning_room_light_energy', 'sensor.dinning_room_light_energy_daily', 'sensor.dinning_room_light_energy_weekly', 'sensor.dinning_room_light_energy_monthly']
2022-10-16 14:15:53.414 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up powercalc platform for sensor
Traceback (most recent call last):
```

Dining room light has entites and its working.
I reloaded android tv and this is what I got

```
2022-10-16 14:37:08.812 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Beijing Xiaomi Electronics Co., Ltd., model=Android TV)
2022-10-16 14:37:08.812 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.android_tv_192_168_31_5 entity_category=None, sensor_name=Android TV power strategy=FixedStrategy manufacturer= model= standby_power=5.00 unique_id=04:7a:0b:a1:a7:3d)
2022-10-16 14:37:08.813 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Android TV energy
2022-10-16 14:37:08.813 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy daily (entity_id=sensor.android_tv_energy_daily)
2022-10-16 14:37:08.813 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy weekly (entity_id=sensor.android_tv_energy_weekly)
2022-10-16 14:37:08.813 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy monthly (entity_id=sensor.android_tv_energy_monthly)
2022-10-16 14:37:08.814 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.android_tv_power', 'sensor.android_tv_energy', 'sensor.android_tv_energy_daily', 'sensor.android_tv_energy_weekly', 'sensor.android_tv_energy_monthly']
2022-10-16 14:37:08.816 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up powercalc platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform
await asyncio.shield(task)
File "/config/custom_components/powercalc/sensor.py", line 290, in async_setup_entry
await _async_setup_entities(hass, sensor_config, async_add_entities)
File "/config/custom_components/powercalc/sensor.py", line 313, in _async_setup_entities
async_add_entities(
TypeError: EntityPlatform._async_schedule_add_entities_for_entry() missing 1 required positional argument: 'new_entities'
```
> This is what I found in the logs
> 
> ```
> 2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.power_profile.model_discovery] media_player.android_tv_192_168_31_5: Auto discovered model (manufacturer=Aqara, model=Android TV)
> 2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=media_player.android_tv_192_168_31_5 entity_category=None, sensor_name=Android TV power strategy=FixedStrategy manufacturer= model= standby_power=5.00 unique_id=04:7a:0b:a1:a7:3d)
> 2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.energy] Creating energy sensor: Android TV energy
> 2022-10-16 14:15:53.407 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy daily (entity_id=sensor.android_tv_energy_daily)
> 2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy weekly (entity_id=sensor.android_tv_energy_weekly)
> 2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensors.utility_meter] Creating utility_meter sensor: Android TV energy monthly (entity_id=sensor.android_tv_energy_monthly)
> 2022-10-16 14:15:53.408 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.android_tv_power', 'sensor.android_tv_energy', 'sensor.android_tv_energy_daily', 'sensor.android_tv_energy_weekly', 'sensor.android_tv_energy_monthly']
> ```
> 
> and in the end I have this
> 
> ```
> 2022-10-16 14:15:53.411 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.dinning_room_light_power', 'sensor.dinning_room_light_energy', 'sensor.dinning_room_light_energy_daily', 'sensor.dinning_room_light_energy_weekly', 'sensor.dinning_room_light_energy_monthly']
> 2022-10-16 14:15:53.414 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up powercalc platform for sensor
> Traceback (most recent call last):
> ```
> 
> Dining room light has entites and its working.

You suppressed this last `ERROR`, that's definitely something here and should give some further pointers. Didn't you notice that error before?
No, I didnt. I would saw it in short logs while it send any error message to it. That is why in the begging I didnt see any error.
Could you share full logs after startup please? Not suppressing anything. You can export to a txt file and include it here to not pollute the issue to much wich large comments.
This is full log after restart. No entities for android tv
[log.txt](https://github.com/bramstroker/homeassistant-powercalc/files/9794556/log.txt)

Ah I see you also changed `async_add_entities()` above, you only needed to add the `_LOGGER` line before this line (at 312), not change the other lines.
That's why you have all these new errors now.

On startup I see:
`Adding entities: ['sensor.android_tv_power', 'sensor.android_tv_energy', 'sensor.android_tv_energy_daily', 'sensor.android_tv_energy_weekly', 'sensor.android_tv_energy_monthly']`

Which is exactly as expected. I will do some digging through the HA core code to see if I can find anything which can cause the entities to not be created.
Can you post exactly how code from line 311 should look like. I copy pasted your code in the file.
Here you go: https://gist.github.com/bramstroker/db94897e836846215156703cde8a2749
As I was messing around with powercalc files, I re downloaded v1.0.0. beta 6. I paste sensor.py from you link and restarted.
I'm sending you log files after restart, and after I reload androidtv to get entities.

[reload_log.txt](https://github.com/bramstroker/homeassistant-powercalc/files/9794667/reload_log.txt)
[restart_log.txt](https://github.com/bramstroker/homeassistant-powercalc/files/9794668/restart_log.txt)

I still have no clue unfortunately, it's a very strange issue. As everything indicates the entity should be created correctly. I also compared you config entry to mine and it's exactly the same exact the unique_id (mac address) and the entity_id of the media player.

There is some strange error in your log which you should have a look at, but I doubt it's related to the powercalc issue.

```
2022-10-16 15:25:34.987 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 508, in async_update_ha_state
raise NoEntitySpecifiedError(
homeassistant.exceptions.NoEntitySpecifiedError: No entity id specified for entity 100 Registered
```

Could you try the following:
- Create a second config entry (virtual power -> fixed) for the same android TV and give it another name and define a unique_id yourself
- Create a powercalc sensor for the same android TV using YAML. Also give it another name and another unique_id

Restart and see if one of these options "survive" after restart (i.e. if the powercalc entities are created successfully)
For time being this just doesn't work. Never mind. This line you point out is perhaps my asteriks integration. This is the only entity 100. I'm experimenting with it. Trying to make successful call between my mob and intercom. For now I can call intercom from my mob, but I can't make call back. 
 
@screem20056 @dandek01 
I would definitely try to resolve this issue for you before releasing a v1.0.0 stable release, but not sure how to proceed as I'm unable to reproduce in any way.

I need your help in this to check if any of the following solves the issue.

Could you try disabling auto discovery in the YAML:

```
powercalc:
  enable_autodiscovery: false
```

And also try the options I asked to @dandek01 two replies above:

Could you try the following:

- Create a second config entry (virtual power -> fixed) for the same android TV and give it another name and define a unique_id yourself
- Create a powercalc sensor for the same android TV using YAML. Also give it another name and another unique_id

Restart and see if one of these options "survive" after restart (i.e. if the powercalc entities are created successfully)
Hi,
I disabled autodiscovery in configuration.yaml file. I created another powercalc meter for android tv. It didn't survive restart.
Here is the log.
[log.txt](https://github.com/bramstroker/homeassistant-powercalc/files/9846268/log.txt)

I also see lines like this:

`2022-10-23 12:24:58.729 DEBUG (MainThread) [custom_components.powercalc.sensors.power] media_player.android_tv_192_168_31_5: State changed to "standby". Power:5.00`

Which indicates that the entity should be in your HA instance. Maybe it's not bound to the config entry for whatever reason.

Could you search for `media_player.android_tv_192_168_31_5` in entities tab under "Settings" -> "Devices & Entities"? Can you find it there?

You are only using the GUI config flow or did you also define entities using the YAML approach?
Yes, I can find media_player.android_tv_192_168_31_5. It's there.
I didn't define anything in yaml, I just used gui.
Should I try something like
`utility_meter:
  daily_energy_daniel_bedroom:
    source: sensor.daniel_bedroom_smart_plug_energy
    cycle: daily`
> Yes, I can find media_player.android_tv_192_168_31_5. 

I'm sorry, I meant to ask for entity_id `sensor.android_tv_power`

>  I didn't define anything in yaml, I just used gui. Should I try something like `utility_meter: daily_energy_daniel_bedroom: source: sensor.daniel_bedroom_smart_plug_energy cycle: daily`

I think you are mixing up things here. 
Your problem was no powercalc entities were appearing after restart right (no power, energy and utility_meters)? Or do you only have a problem with the utility meters?
No, there isnt `sensor.android_tv_power`

`Your problem was no powercalc entities were appearing after restart right (no power, energy and utility_meters)? `
Yes, that is my problem.
You can try creating a sensor with YAML like this:

```yaml
sensor:
  - platform: powercalc
    entity_id: media_player.android_tv_192_168_31_5
    unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
    name: Android TV YAML
    standby_power: 5
    fixed:
      power: 45
```

Normally this would work without a unique_id, as it will take the unique_id from the source entity `media_player.android_tv_192_168_31_5`, but to make sure it does not give any conflicts with the powercalc sensors which are setup with the GUI flow I just gave it some unique id

After restarting you should have a sensor `sensor.android_tv_yaml_power`

One other thing you can try for the GUI sensor is to toggle both create energy sensors and utility meters off. Than it will only create a power meter and we have the most simple approach. 
Problems like this are best debugged by eliminating potential issues and bringing things back to basics as much as possible.
I can't create sensor in yaml as there is some error when I check configuration.
This is sensors.yaml file

```yaml
- platform: powercalc
  sensors:
      entity_id: sensor.android_tv_energy_daily
      unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
      name: Android TV YAML
      standby_power: 5
      fixed:
        power: 45
```

And this is error on check configuration
`Invalid config for [sensor.powercalc]: must contain at least one of entity_id, entities, include, daily_fixed_energy.. Got OrderedDict([('platform', 'powercalc'), ('sensors', OrderedDict([('entity_id', 'sensor.android_tv_energy_daily'), ('unique_id', '6cc63373-3f11-4785-9903-59870bb956f8'), ('name', 'Android TV YAML'), ('standby_power', 5), ('fixed', OrderedDict([('power', 45)]))]))]). (See ?, line ?).`

Spacing is oke in editor, but when I copy/paste it spacing was reformat.
I tried also with
`media_player.android_tv_192_168_31_5`

Edit:
I tried to toggle both create energy sensors and utility meters off but again the same problem. Sensor sensor.android_tv_power wasnt created, I had to reload integration.
When you place it in sensors you don't need to use sensors or sensor again.

Just put in:
```yaml
- platform: powercalc
  entity_id: sensor.android_tv_energy_daily
  unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
  name: Android TV YAML
  standby_power: 5
  fixed:
    power: 45
```
Try it it wont work that way. I have in sensors.yaml file for other sensors 
- platform: ___
  sensors:
     some line
It will give you an error using studio code server Missing property sensors. When you add a sensors: line error goes away but it throws other errors on config checking.

This `sensors` property is only needed for template sensors or maybe some other. 
But most platforms don't use that.
Also see here: https://www.home-assistant.io/docs/configuration/splitting_configuration/. And search for `sensor.yaml` there you'll find an example.

Make sure your indenting is correct because that's very important in YAML.

You config needs to look something like this:
```yaml
- platform: template
  sensors:
    ...
- platform: powercalc
  entity_id: sensor.android_tv_energy_daily
  unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
  name: Android TV YAML
  standby_power: 5
  fixed:
    power: 45
```

I checked my configuration.yaml file. I created this when I first started with home assistant.
I had this line
`sensor: !include sensors.yaml`
and I change this now to
`sensor: !include sensor.yaml`
I renamed sensors.yaml to sensor.yaml
Now. I can with out any errors add to my sensor.yaml file

- platform: powercalc
  entity_id: media_player.android_tv_192_168_31_5
  unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
  name: Android TV YAML
  standby_power: 5
  fixed:
    power: 45

When I restarted home assistant I saw this in my logs, didn't saw it befroe
`2022-10-23 19:16:47.118 WARNING (MainThread) [homeassistant.components.integration.sensor] sensor.android_tv_yaml_energy could not restore last state unknown: [<class 'decimal.ConversionSyntax'>]`

I have this in my configuration.yaml file
group: !include groups.yaml
automation: !include automations.yaml
script: !include script.yaml
scene: !include scene.yaml
sensor: !include sensor.yaml
binary_sensor: !include binary_sensor.yaml
template: !include templates.yaml 


That warning would be expected, because it takes some time for the energy sensor to calculate an initial value.

But you should have `sensor.android_tv_yaml_power` and `sensor.android_tv_yaml_energy` now. Just check if they are working as expected an are surviving restarts. 
When these are surviving restart there is something picky about the GUI setup, not sure what as it is using exactly the same logic and all your other GUI sensors are working fine.
But at least you'll have a "solution" now by just setting up this particular sensor with YAML, so you don't need to reload everytime.
Yes, senors sensor.android_tv_yaml_power and sensor.android_tv_yaml_energy survived restart but again I have to reload andoid tv to get sensors back. When I check it on developer tools -> sensors created by powercalc are not listed.
I tried this, instead of entity_id: media_player.android_tv_192_168_31_5 I put in sesnor.yaml 
`entity_id:` `sensor.android_tv_power`

I restarted home assistant and check for sensors in developer tools -> states. All sensors from powercalc integration for android tv are listed and available, I can open them to check current values but they doesn show on powercalc integration -> android tv and energy dashboard is complaining with 
`Check the integration or your configuration that provides:
`
`sensor.android_tv_energy`
When I open energy Android TV energy is listed no errors. Before it was listed as sensor.android_tv_power
YAML currently creates the following sensors:
- sensor.android_tv_yaml_power
- sensor.android_tv_yaml_energy

GUI creates the following sensors:
- sensor.android_tv_power
- sensor.android_tv_energy

Entity_id for the yaml sensor needs to be the entity id of the source (in this case the media player), so you must NOT change that.

Of course you still need to reload to get `sensor.android_tv_power` available as nothing has changed about them, they still are create by the GUI config, as it always was.

When you want the YAML configuration to create `sensor.android_tv_power` you can change the name to `Android TV`, and make sure to remove the configuration entry in the GUI.

Or you can just reference `sensor.android_tv_energy_yaml` in the energy dashboard.
I remove android tv from powercalc integration and went just for yaml file. This is what I have in sensor.yaml

```
 - platform: powercalc
   entity_id: sensor.android_tv_power
   unique_id: 6cc63373-3f11-4785-9903-59870bb956f8
   name: Android TV
   standby_power: 5
   fixed:
     power: 45
```
After restart I have sensor.android_tv_yaml_energy and it has survived. I added it to energy dashboard and there isnt any  errors any more. 
Hi i was travelling and not up to date so the idea now is to remove all gui created entities and shift them to yaml then they survive the restart 
Unusual...
At least that is working, but not a real solution. 
I have asked @dandek01 to try this method to see if they survive the restart. When the YAML approach also had the same issue I hoped to get some more insights in the root cause. However still have no clue why the GUI setup routine is behaving differently.
Walked through the core code a few times, but that's also complex as it's asynchronous in nature. I cannot figure out yet where the bug is.
Not sure how to make any further progress on this. Also you two guys are the only ones reporting this problem among thousands of users of the integration. which makes it very hard to debug. 
Will ask on discord channel if anyone has an idea.
Hi
Just worked like a crazy on yaml file i moved all the tuya devices to yaml and it worked out 
Same issue for me. Only for Tuya after restart and it worked when I move the config from GUI to yaml.
Sad to hear you are also experiencing issues with the power sensors configured using the GUI. When more people chime in which have also issues with TuYa I will consider buying a device myself so I will hopefully be able to reproduce and solve the issue.
It's Wifi, right? What do I need exactly to get a setup running?
I doesn't have to do with a device or integration. I have tuya wifi switches and there is no problem with your integration. Maybe we have something that is misconfigured in our setup and that we are not aware of. Like my sensor/sensors.yaml file. And this is probably more the case then your integration.
Hi,
I managed to fix my issues with android tv. I have android tv that has built in chromecast. I have google cast integration for that tv in home assistant. Instead of using media_player.android_tv_192_168_31_5 that is entity for my android tv I used media_player.bedroom_tv that is entity for the same device but on chromecast integration.
And it just works. When I restarted home assistant all powercalc entites for android tv were there. I removed lines from yaml file and created integration in gui. No more reload.
In my case I have two integration basically controlling on device.
One integration is android tv for tv itself and other one is google cast for controling built in chromecast in the same tv.
And for some reason entity created with android tv integration was or is making problems with powercalc integration.
Edit:
I also downgraded to the latest stable version of powercalc 0.26.1 and its working.
@dandek01 
Thanks for the extra information. Not sure how it would help me though as I was unable to reproduce the issue with android TV integration in the first place. 
But maybe this can be helpful for others.

> I also downgraded to the latest stable version of powercalc 0.26.1 and its working.

What do you mean with this? That it continued to work for `media_player.android_tv_192_168_31_5` again after downgrading? 
I'm currently using latest stable version of power calc. But I noticed one thing when I installed zigbee led strip on my tv. 
I made and automation to turn on led when tv is on and to turn if off when tv is off. It works but only when I'm watching youtube.
If I switch to terrestrial tv it turns led off. 
I went to powercalc and saw that measuring while watching live tv is 5w like when  tv is off. I don't know why. Perhaps casting is off.  
Edit:
It works with ```media_player.bedroom_tv``` but this is entity created by google cast integration as tv has built in chromecast device.
Entity ```media_player.android_tv_192_168_31_5``` never survived restart and this is entity created by android tv integration.
I don't know why is happening. The same device is using two different entities created by two different integrations.
Could you please create another issue when you have other questions, this particular issue is getting completely out of control this way.
I understand you but I was just describing what I done. 
I am closing this issue as I am unable to make any further progress on this. It's a very tough one as I am unable to reproduce on any of my HA installations.
When there are new insights or reproduction steps in the future we can have another look.

Resolving patch:
diff --git a/custom_components/powercalc/config_flow.py b/custom_components/powercalc/config_flow.py
--- a/custom_components/powercalc/config_flow.py
+++ b/custom_components/powercalc/config_flow.py
@@ -248,22 +248,20 @@ async def async_step_integration_discovery(
         )
 
         self.selected_sensor_type = SensorType.VIRTUAL_POWER
-        self.name = discovery_info[CONF_NAME]
-        unique_id = discovery_info[CONF_UNIQUE_ID]
+        self.source_entity = discovery_info[DISCOVERY_SOURCE_ENTITY]
+        del discovery_info[DISCOVERY_SOURCE_ENTITY]
+
+        self.source_entity_id = self.source_entity.entity_id
+        self.name = self.source_entity.name
+        unique_id = f"pc_{self.source_entity.unique_id}"
         await self.async_set_unique_id(unique_id)
         self._abort_if_unique_id_configured()
 
-        sensor_config = discovery_info.copy()
-
-        self.source_entity_id = discovery_info[CONF_ENTITY_ID]
-        self.source_entity = discovery_info[DISCOVERY_SOURCE_ENTITY]
-        del sensor_config[DISCOVERY_SOURCE_ENTITY]
-
         if DISCOVERY_POWER_PROFILE in discovery_info:
             self.power_profile = discovery_info[DISCOVERY_POWER_PROFILE]
-            del sensor_config[DISCOVERY_POWER_PROFILE]
+            del discovery_info[DISCOVERY_POWER_PROFILE]
 
-        self.sensor_config.update(sensor_config)
+        self.sensor_config = discovery_info.copy()
 
         self.context["title_placeholders"] = {
             "name": self.sensor_config.get(CONF_NAME),
@@ -300,11 +298,10 @@ async def async_step_virtual_power(
             self.source_entity = await create_source_entity(
                 self.source_entity_id, self.hass
             )
-            unique_id = (
-                user_input.get(CONF_UNIQUE_ID)
-                or self.source_entity.unique_id
-                or self.source_entity_id
-            )
+            unique_id = user_input.get(CONF_UNIQUE_ID)
+            if not unique_id:
+                source_unique_id = self.source_entity.unique_id or self.source_entity_id
+                unique_id = f"pc_{source_unique_id}"
 
             await self.async_set_unique_id(unique_id)
             self._abort_if_unique_id_configured()
diff --git a/custom_components/powercalc/discovery.py b/custom_components/powercalc/discovery.py
--- a/custom_components/powercalc/discovery.py
+++ b/custom_components/powercalc/discovery.py
@@ -188,8 +188,6 @@ def _init_entity_discovery(
             return
 
         discovery_data = {
-            CONF_UNIQUE_ID: source_entity.unique_id,
-            CONF_NAME: source_entity.name,
             CONF_ENTITY_ID: source_entity.entity_id,
             DISCOVERY_SOURCE_ENTITY: source_entity,
         }


Test patch:
diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py
--- a/tests/test_config_flow.py
+++ b/tests/test_config_flow.py
@@ -112,7 +112,7 @@ async def test_discovery_flow(hass: HomeAssistant):
         CONF_MANUFACTURER: "signify",
         CONF_MODEL: "LCT010",
         CONF_NAME: "test",
-        CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
+        CONF_UNIQUE_ID: f"pc_{DEFAULT_UNIQUE_ID}",
     }
 
 
@@ -180,7 +180,7 @@ async def test_discovery_flow_with_subprofile_selection(hass: HomeAssistant):
         CONF_MANUFACTURER: "lifx",
         CONF_MODEL: "LIFX Z/length_6",
         CONF_NAME: "test",
-        CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
+        CONF_UNIQUE_ID: f"pc_{DEFAULT_UNIQUE_ID}",
     }