Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuya entities sometimes don't start (Hue always does) + suggestion #3032

Open
5 tasks done
Dream1975 opened this issue Jan 30, 2025 · 20 comments
Open
5 tasks done

Tuya entities sometimes don't start (Hue always does) + suggestion #3032

Dream1975 opened this issue Jan 30, 2025 · 20 comments
Labels
bug Something isn't working

Comments

@Dream1975
Copy link

System Health details

System Information

version core-2025.1.4
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.13.1
os_name Linux
os_version 6.6.73-haos
arch x86_64
timezone Europe/Amsterdam
config_dir /config
Home Assistant Community Store
GitHub API ok
GitHub Content ok
GitHub Web ok
HACS Data ok
GitHub API Calls Remaining 5000
Installed Version 2.0.5
Stage running
Available Repositories 1506
Downloaded Repositories 13
Home Assistant Cloud
logged_in false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Home Assistant Supervisor
host_os Home Assistant OS 14.2
update_channel stable
supervisor_version supervisor-2024.12.3
agent_version 1.6.0
docker_version 27.2.0
disk_total 30.8 GB
disk_used 5.9 GB
healthy true
supported true
host_connectivity true
supervisor_connectivity true
ntp_synchronized true
virtualization kvm
board ova
supervisor_api ok
version_api ok
installed_addons Samba share (12.4.0), Terminal & SSH (9.16.0), Matter Server (7.0.0), Get HACS (1.3.1), Home Assistant Google Drive Backup (0.112.1), Mosquitto broker (6.5.0), SQLite Web (4.3.0)
Dashboards
dashboards 11
resources 1
views 2
mode storage
Recorder
oldest_recorder_run 19 januari 2025 om 23:57
current_recorder_run 30 januari 2025 om 10:53
estimated_db_size 326.94 MiB
database_engine sqlite
database_version 3.47.1

Checklist

  • I have read the FAQ to see if there is a known solution to my problem.
  • I have enabled debug logging for my installation.
  • I have filled out the issue template to the best of my ability.
  • This issue only contains 1 issue (if you have multiple issues, open one issue for each issue).
  • This issue is not a duplicate issue of currently previous issues..

Describe the issue

My issue is somewhat like issue 2962 only mine is not since a recent release but has always been so (that's why I made a separate issue).

I have both lights from Tuya and Philips. When HASS starts from scratch (VM in off state and then turned on) half the time all lights have entities and half the time only the HUE ones (which has them always).

It would seem something like Powercalc is checking for the Tuya devices too early and therefore doesn't see them available (thus not making a sensor, as shown in the screen print below). Which could be logical as the Tuya integration also communicates with Tuya cloud in setting up the devices. It could very well be that this process is still happening when Powercalc also checks the devices.
Ps. If I press the 3 dots and select reload, the entity is made immediately (only that has to be done for all lights, which are quite a few).

So my questions:

  1. Any idea what is might be and if I can solve it (for example by delaying the Powercalc startup a little)?
  2. Can this reload maybe also be done for all lights at once somehow? Possibly even through an automation, then I can set one up to run every hour or so, ensuring all entities will become available eventually even if not direct at startup.

Reproduction steps

Image

Debug logs

.

Diagnostics dump or YAML config

No response

@Dream1975 Dream1975 added the bug Something isn't working label Jan 30, 2025
@bramstroker
Copy link
Owner

Yes this might be very well due to loading order.
Where sometimes Powercalc starts earlier than Tuya and sometimes the other way around.
I had another proposed fix in the past, which never ended up in a release yet, but might be a fix for your case.

Could you please try applying this patch to your config/custom_components/powercalc/sensors/power.py file?
https://github.com/bramstroker/homeassistant-powercalc/pull/2926/files

Restart, and see if everything works and keeps working after multiple restarts.

@Dream1975
Copy link
Author

Dream1975 commented Jan 30, 2025

I did the change and first did a restart of HASS, everything kept working.
Secondly, I did a complete reboot of the VM/HASS OS and the Tuya's were again without entity.
I reloaded only 2 lights by hand and then did a reload configuration which still didn't give entities (although the 2 started by hand kept on working), but...
after this, I did a restart of HASS (without restarting the VM) and then everything came back up again. Although I'm not sure if the change helped in that matter, as I've seen this before also.

It would seem that with a hot reboot everything starts up just a little faster so Powercalc sees the devices, while with a VM/HASS OS restart they just aren't ready in time.

But given the above, I'm quite confident this then could be solved by introducing a small delay in starting Powercalc (or do a second reload after a few seconds). Can I put a timeout somewhere in the code maybe to try (and if so how/where)?

@bramstroker
Copy link
Owner

Reading your initial bug report again you don't have any entities at all added to HA, which is kinda strange.
My first suggestion for the patch, was only a potential fix for sensors keeping unavailable state after startup. But that does not apply to your problem.

Could you enabled debug logs and share logs after startup?

You could try placing await asyncio.sleep(30) on this line: https://github.com/bramstroker/homeassistant-powercalc/blob/master/custom_components/powercalc/sensor.py#L303.
But that won't be something I will add in the integration code itself. There must be another solution, adding sleeps is never a correct solution.

We just need to figure out what exactly is unavailable during setup which prevents powercalc to create / register the entities correctly. Hopefully the logs will provide insights.
Would be nice to have a log when the entities are created, and one when the entities are not created.
That would allow us to compare.

@Dream1975
Copy link
Author

Dream1975 commented Jan 30, 2025

Lucky strike, the first cold reboot after turning on debugging everything worked, then did a second cold reboot and then it didn't.

Enclosed the 2 debug logs (one working one not) for comparison

Edit log removed

@bramstroker
Copy link
Owner

bramstroker commented Jan 31, 2025

Very strange, as I also see these entries in not working log:

Creating power sensor (entity_id=light.fl06_fittinglamp_gang_bg1 entity_category=None, sensor_name=FL06 - Fittinglamp Gang BG1 power strategy=lut manufacturer=tuya model=ANTELA-9W-RGB+CCT unique_id=c2b31ff5-efd1-4554-b402-2f2d9da2e720)

Which indicate the sensor should be created.
Seems something goes wrong between this and the actual creation of the entity in HA.

Could you try adding following to this line: https://github.com/bramstroker/homeassistant-powercalc/blob/master/custom_components/powercalc/sensor.py#L366?

_LOGGER.debug("Adding entities: %s", [entity.entity_id for entity in entities_to_add])

@Dream1975
Copy link
Author

Dream1975 commented Jan 31, 2025

I changed it like this (hope that's what you ment and I did it right)

         if existing_entry and existing_entry.disabled_by == RegistryEntryDisabler.DEVICE:
            entities_to_add.remove(entity)
            _LOGGER.debug("Adding entities: %s", [entity.entity_id for entity in entities_to_add])

    async_add_entities(entities_to_add)

The log download gave 2 (see enclosed). They are both from the same startup, as I can see in the TS, whereby the Hue entities did but the Tuya didn't come up.

** Edit: remove logs **

@bramstroker
Copy link
Owner

@Dream1975 Sorry, you should not put the log inside the if.
But just above async_add_entities line. With same indenting as that.

@Dream1975
Copy link
Author

Dream1975 commented Jan 31, 2025

Changed it and made a new log of a start with no Tuya entities

** Edit: remove logs **

@bramstroker
Copy link
Owner

@Dream1975 I see following in the logs:
Focussing on sensor.fl06_fittinglamp_gang_bg1_power atm, because that's the same as I checked in last log.

2025-01-31 19:13:26.572 DEBUG (MainThread) [custom_components.powercalc.sensors.power] Creating power sensor (entity_id=light.fl06_fittinglamp_gang_bg1 entity_category=None, sensor_name=FL06 - Fittinglamp Gang BG1 power strategy=lut manufacturer=tuya model=ANTELA-9W-RGB+CCT unique_id=c2b31ff5-efd1-4554-b402-2f2d9da2e720)
2025-01-31 19:13:26.573 DEBUG (MainThread) [custom_components.powercalc.sensor] Adding entities: ['sensor.fl06_fittinglamp_gang_bg1_power']

Also the new Adding entities is being logged now, which means powercalc completed preparing the entities to be added and registered to HA, and delegates the entity creation to HA.
After this sensor.fl06_fittinglamp_gang_bg1_power should be created, but it's not in your case, right?
Seems something goes wrong in HA core, but I have no clue. Will ask some HA core dev if he has any idea what can be the cause.

@Dream1975
Copy link
Author

Dream1975 commented Feb 1, 2025

Indeed, with a startup that has the problem for your example sensor.fl06_fittinglamp_gang_bg1_power is not there (like FL02 in the screenprint in my first post). WHen I do the 3 dots and reload it is immediately there like FL01 in the screenprint (as is it with some starts).

Thank you and I'll wait patiently (it has been like this since I started using Powercalc so am in no hurry.

@Dream1975
Copy link
Author

I noticed something new today. When all the Powercalc sensors are working, but then I reload the Tuya integration (to update devices) they are gone again. So the startup issue is probably related (that Powercalc doe make the entities but in the startup procedure Tuya is sometimes just before and sometimes just behind it).

@bramstroker
Copy link
Owner

Very interesting find.
Maybe Tuya integration has some logic to remove all entities from a device.
Powercalc adds entities to the same device as Tuya. This device is managed by Tuya.

Could you test creating a HA helper, for example Integral? Pretty sure these are also added to same device as the source entity.
When this is the case please try reloading Tuya again and see if those disappear as well.

@Dream1975
Copy link
Author

Something interesting happens there also. When making the helper and connecting it to a Tuya sensor leads to the entities for all devices being gone also.

But I have other helpers also connected to Tuya devices (fe an energy integral vor the power on smart plugs) and these never had any issue. What I see in the Tuya integration if I compare:

The helper made in HASS itself is linked to the device
Image

And if I look at a powercalc device (with, off course, making sure the entity is running) that sensor is not visible under the device

Image

@bramstroker
Copy link
Owner

Something interesting happens there also. When making the helper and connecting it to a Tuya sensor leads to the entities for all devices being gone also.

What do you mean with this exactly the entities for all devices being gone?
Do you mean the helper entity is also removed from the system, and not working anymore?

@Dream1975
Copy link
Author

With all devices I mean the Tuya devices helpers created by Powercalc (all others are fine, even manual made integrals linked to Tuya devices).

"Do you mean the helper entity is also removed from the system, and not working anymore?"
Indeed

@bramstroker
Copy link
Owner

Fyi I have ordered a 10 bucks tuya plug, so I can setup that integration to see if I can reproduce.

Also working on a reload service: #3079
Which will reload all config entries, but also can reload YAML config without the need to restart HA.

@Dream1975
Copy link
Author

Whoah, that's great.

Hope you didn't buy it only for this issue and can/will use it anyway. If not, let me know and I'll gladly reimburse you for it. Ps. You might also need a Tuya light with it to test. If so let me know and I can send you one (I have spares, I also live in NL).

@bramstroker
Copy link
Owner

Whoah, that's great.

Hope you didn't buy it only for this issue and can/will use it anyway. If not, let me know and I'll gladly reimburse you for it. Ps. You might also need a Tuya light with it to test. If so let me know and I can send you one (I have spares, I also live in NL).

@Dream1975 I bought it particularly for debugging this issue ;-). Have no use for it. But I can return it of course, bought a amazon.
Would be nice if you have a tuya bulb laying around which I can test with. Do you have discord, then I can sent you my address in PM. #bramski3414 is my username there.

@Dream1975
Copy link
Author

@bramstroker no discord, but I see a mail address in your profile. So I can send you a mail you can reply on (I have to leave now, but will do that tonight or tomorrow then).

Or..... you order one (or more) at https://nl.aliexpress.com/w/wholesale-tuya-light.html?spm=a2g0o.productlist.search.0 so you'll have a/several lights for your own use. I bought them for EUR 3,25 there with free shipping so might be better to reïmburse that as it is even cheaper as shipping costs within NL (unbelievable).

Let me know which one you prefer and you'll get a mail from me by tomorrow.

@bramstroker
Copy link
Owner

I have purchased tuya light for 2euro, with free shipping.
How is that even possible lol?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants