JurajNyiri / HomeAssistant-Tapo-Control

Control for Tapo cameras as a Home Assistant component
Apache License 2.0
1.1k stars 88 forks source link

Feature Request: Battery Optimization #457

Closed mikeaumiller closed 2 months ago

mikeaumiller commented 10 months ago

Is your feature request related to a problem? Please describe. When the integration is configured for a battery-powered outdoor camera (TC85) it drains the battery within 12 hours. When the integration is disabled the battery remains at a constant state. It appears there's some type of constant connection of some sort to the device.

Describe the solution you'd like Would like the ability to limit how often any connection is made to a Tapo battery-powered camera to pull metrics or make a RTSP connection unless the camera card is clicked start a live stream.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context I have (3) Tapo TC85 outdoor cameras and noticed the battery draining 2% per hour when the Tapo-Control integration is enabled . For my dashboard, I have configured my custom card with WebRTC Camera which leverages [go2rtc] and have the initial stream set to "Paused by default" which only display a screenshot until you enable a live stream, but nothing has stopped the battery levels from dropping quickly.

JurajNyiri commented 10 months ago

Does this camera use a hub or is it directly connected to wifi?

mikeaumiller commented 10 months ago

These camera’s are wifi connected

JurajNyiri commented 10 months ago

Do you have onvif enabled (motion detection)? If so, try disabling it and see if it improves or not.

mikeaumiller commented 10 months ago

yeah, I thought about that as well... I'll disable (motion detection) and won't configure any camera card and will monitor battery usage over the next couple of hours.

JurajNyiri commented 10 months ago

In any case, integration does load up to date data every 30s in one request, we can tone it down, ideally automatically. Do you have battery sensor exposed in HA? Could you copy paste all the camera entity properties here?

mikeaumiller commented 10 months ago

I like the idea of automatically or if possible even give the user control of the heartbeat value with a slider or pulldown with predefined values

Below is a screenshot of the battery metric usage. I can run it longer if needed.

Tapo Camera TC85 Battery Metric

JurajNyiri commented 10 months ago

Could you copy paste all the camera entity properties here?

mikeaumiller commented 10 months ago

Here you go... Tapo TC85 - Device Info

Tapo TC85 - Configuration

Tapo TC85 - Diagnostics

JurajNyiri commented 10 months ago

Thank you, could you please provide Attributes of the camera entity? You can find these in /developer-tools/state. I am trying to find something that tells me this is battery operated camera (battery sensor) that works directly on wifi instead of a hub.

mikeaumiller commented 10 months ago

Here you go...

image image image image image image image image
mikeaumiller commented 10 months ago

Maybe this attribute under the HD stream: model_name: TC85

JurajNyiri commented 10 months ago

Thank you I now have everything I need to add support. We can use power value BATTERY along with detecting whether it is a child device or not via pytapo to determine it is a wifi camera on battery without a hub.

mikeaumiller commented 10 months ago

Ahh.. yeah that's a better idea.

ion-storm commented 10 months ago

Hey hey! I have these cameras also the TC85 model, will be willing to send the dev out a camera to expand support, reach me on twitter @ionstorm

ion-storm commented 10 months ago

The TC85 doesn't appear to have ONVIF, it has control port 443, rtsp 554, and a http video port on 8800, unsure of all the capabilities, there could be other ways to get the motion events. I also am noticing that the battery drain is 2% per hour at least with the plugin enabled. All other functionality works except motion detection.

mikeaumiller commented 8 months ago

Just circling back to see if there's any update on this original request to optimize battery-powered cameras?

Exelord commented 7 months ago

Same issue noticed for C425 which is TC85. 10% battery drain per day with integration turned on. Only webhooks enabled

jcconnell commented 6 months ago

Same issue noticed for C425 which is TC85. 10% battery drain per day with integration turned on. Only webhooks enabled

Somewhat unrelated - I'm trying to integrate a C425 with Home Assistant but I'm not getting motion detection events after having read through the items in the FAQ. I see you mention webhooks are enabled here. Are you able to get motion detection events from the C425 via webhooks?

marcosngomezi commented 5 months ago

the connection Information gives error on hub cameras, maybe useful to avoid the child check or to complement it

murtaugh82 commented 3 months ago

In any case, integration does load up to date data every 30s in one request, we can tone it down, ideally automatically.

Hi @JurajNyiri , i would like to change the update interval manually for the time being until there is another solution. However, i could not identify the corresponding place in the code. Could you please give me a hint where the update is triggered?

JurajNyiri commented 3 months ago

Hi, I do not own battery operated camera so it is very hard for me to develop this change without having access to one.

That being said, I tried something.

Could you please download https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/tree/experimental_battery_optimization manually and see what warnings do you get in the logs? This is a POC version, it changes update interval for powered devices to 1 minute and battery powered to 10, but as I said, I had no way to test, so it might not work...

Screenshot 2024-07-04 at 23 52 50

This is what I see for my mains powered devices, let me know if you see something similar (but with longer update interval) for battery cameras.

slartibartfast11 commented 3 months ago

Thanks, but I think a custom poll time might be required so users of the integration can adjust it to poll the camera just often enough to conserve the battery but also to not let it fully go to sleep.

Looking at my C425 camera, it seems like it uses a proprietary wake-up protocol with TP-Link and the official app, which gives the camera just enough connectivity to wait for the wake-up event from TP-Link cloud. Once the camera has gone asleep, there's nothing other than the app (or, obviously, a PIR motion event) that will wake it.

This assumes TP-Link are not willing to come to the party to release a public API to their app wireless camera wake up protocol, of course...

JurajNyiri commented 3 months ago

Yes that is the plan. I need the poc tested first though to ensure the logic to differentiate cameras work.

murtaugh82 commented 3 months ago

Hi @JurajNyiri,

many thanks for the adjustment! I have a C410 and operate it with a solar panel. Under _basicinfo there is an attribute _powermode (with value SOLAR or BATTERY) instead of power for this model.

2024-07-05 12:23:52.639 DEBUG (MainThread) [custom_components.tapo_control] {'getDeviceInfo': [{'device_info': {'basic_info': {'device_type': 'SMART.IPCAMERA', 'device_info': 'C410 1.0 IPC', 'features': 3, 'barcode': '', 'device_model': 'C410', 'sw_version': '1.0.16 Build 240430 Rel.63325n', 'device_name': 'C410 1.0', 'hw_version': '1.0', 'device_alias': 'Tapo_C410_Backyard', 'mobile_access': '0', 'mac': 'xxx', 'dev_id': 'xxx', 'hw_id': 'xxx', 'oem_id': 'xxx', 'hw_desc': '00000000000000000000000000000000', 'power_mode': 'SOLAR', 'battery_percent': 100, 'battery_charging': 'NO', 'battery_overheated': False, 'power_save_mode': 'off', 'power_save_status': 'off', 'low_battery': False, 'manufacturer_name': 'TP-LINK', 'ffs': False, 'is_cal': True, 'avatar': '', 'has_set_location_info': 1, 'longitude': xxx, 'latitude': xxx}}}] ...

I have therefore adapted the code a little (sorry for the mess - I have neither pyhton experience nor a proper ide :wink: )...

controllerData["isRunningOnBattery"] = (
    True
    if (
        "basic_info"
        in updateDataForAllControllers[controller]
        and (
                (
                    "power"
                    in updateDataForAllControllers[controller][
                        "basic_info"
                    ]
                    and updateDataForAllControllers[controller][
                        "basic_info"
                    ]["power"]
                    == "BATTERY"
                )  or  (
                    "power_mode"
                    in updateDataForAllControllers[controller][
                        "basic_info"
                    ]
                    and (
                        (
                            updateDataForAllControllers[controller][
                                "basic_info"
                            ]["power_mode"]
                            == "BATTERY"                                        
                        ) or (
                            updateDataForAllControllers[controller][
                                "basic_info"
                            ]["power_mode"]
                            == "SOLAR"                                                      
                        )
                    )
                )
        )
    )
    else False

...and now get the desired behavior :smile:

2024-07-05 13:04:50.811 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Terrace running on battery: False
2024-07-05 13:04:50.811 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Terrace...
2024-07-05 13:04:52.072 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: False
2024-07-05 13:04:52.072 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Backyard...
2024-07-05 13:04:54.157 WARNING (MainThread) [homeassistant.components.select] Setup of select platform tapo_control is taking over 10 seconds.
2024-07-05 13:05:02.067 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Terrace running on battery: True
2024-07-05 13:05:02.067 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Terrace...
2024-07-05 13:05:03.053 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: True
2024-07-05 13:05:03.053 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Backyard...
2024-07-05 13:05:04.205 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' accesses hass.helpers.event. This is deprecated and will stop working in Home Assistant 2024.11, it should be updated to import functions used from event directly at custom_components/hacs/base.py, line 642: self.hass.helpers.event.async_track_time_interval(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-05 13:05:14.171 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Entrance running on battery: False
2024-07-05 13:05:14.171 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Entrance...
2024-07-05 13:05:20.791 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Terrace running on battery: True
2024-07-05 13:05:20.791 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Terrace...
2024-07-05 13:05:21.768 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: True
2024-07-05 13:05:21.768 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Backyard...
2024-07-05 13:05:24.171 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.tapo_c410_entrance_battery is taking over 10 seconds
2024-07-05 13:05:27.844 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.tapo_c410_entrance_hd_stream is taking over 10 seconds
2024-07-05 13:05:31.079 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Terrace running on battery: True
2024-07-05 13:05:31.079 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Terrace...
2024-07-05 13:05:32.205 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: True
2024-07-05 13:05:32.205 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Backyard...
2024-07-05 13:05:37.856 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Entrance running on battery: True
2024-07-05 13:05:37.856 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Entrance...

....

2024-07-05 13:15:00.828 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Terrace...
2024-07-05 13:15:02.068 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: True
2024-07-05 13:15:02.069 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Backyard...
2024-07-05 13:15:07.956 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Entrance running on battery: True
2024-07-05 13:15:07.957 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Entrance...
2024-07-05 13:15:12.085 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Terrace running on battery: True
2024-07-05 13:15:12.086 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Terrace...
2024-07-05 13:15:12.994 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Backyard running on battery: True
2024-07-05 13:15:12.994 WARNING (MainThread) [custom_components.tapo_control] Skipping update for Tapo_C410_Backyard...
2024-07-05 13:15:17.959 WARNING (MainThread) [custom_components.tapo_control] Tapo_C410_Entrance running on battery: True
2024-07-05 13:15:17.959 WARNING (MainThread) [custom_components.tapo_control] Updating Tapo_C410_Entrance...
JurajNyiri commented 3 months ago

@murtaugh82 that is great!

You can do second point along with the first one, so that we know what the recommended value should be for a good balance of battery life vs updates in HA.

murtaugh82 commented 2 months ago

@JurajNyiri

I have set the update interval to 30 minutes and the logs also show that the update is only performed every 30 minutes. Unfortunately, this does not reduce the discharge of the battery.

grafik

Is it possible that the connection to the camera remains active even there is no update? with the old version of the integration, i could see that the status led was always on until i deactivated the integration.

Unfortunately, I can't check this at the moment as my working cameras are over 1000km away. I have two more devices on site, but they are no longer accessible due to a firmware update (Tapo C410(EU)_V1_1.0.17 Build 240524). So that I can test them, these cameras need to be downgraded to the Tapo C410(EU)_V1_1.0.16 Build 240430 firmware. However, I don't currently know how to get this specific version and would be grateful for any help.

murtaugh82 commented 2 months ago

Thanks to some help, my local cameras are now running again. I can now physically see from the led that the connection to the camera is only closed for 10 - 12 seconds at a time. This corresponds with pytapo log messages that appear at the same interval.

2024-07-18 11:35:17.740 DEBUG (SyncWorker_48) [custom_components.tapo_control] [pytapo] New request:
2024-07-18 11:35:17.740 DEBUG (SyncWorker_48) [custom_components.tapo_control] [pytapo] {'data': {'method': 'securePassthrough', 'params': {'request': '...'}}, 'headers': {'Host': 'REDACTED', 'Referer': 'REDACTED', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Tapo CameraClient Android', 'Connection': 'close', 'requestByApp': 'true', 'Content-Type': 'application/json; charset=UTF-8', 'Seq': '200', 'Tapo_tag': 'REDACTED'}, 'verify': False}

@JurajNyiri Can you tell me where in the code this interval is defined for pytapo, then I could also test with this value?

JurajNyiri commented 2 months ago

Here https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/457#issuecomment-2211332674 https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/blob/experimental_battery_optimization/custom_components/tapo_control/const.py#L58

murtaugh82 commented 2 months ago

I had already set this value to 30 * 60 and the update messages were then only logged every 30 minutes. (see https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/issues/457#issuecomment-2210746714)

However, I have now noticed that the pytapo request logs are also appearing but at much shorter intervals.

Edit: After i have deactivated the camera entities HD Stream & SD Stream, the set interval is maintained. Is it possible that the query for these entities is triggered by ha?

murtaugh82 commented 2 months ago

The next attempt looks more promising. Within about 24 hours, the cameras have lost only 1%.

Update interval: 30 minutes Camera streams: deactivated

grafik

JurajNyiri commented 2 months ago

Implemented in https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/releases/tag/5.4.27 . Thank you @murtaugh82 !

JurajNyiri commented 2 months ago

Going to reopen to add the solar part (forgot about it, oops)

JurajNyiri commented 2 months ago

Released in https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/releases/tag/5.4.28.

@murtaugh82 please let me know if it works as expected

JurajNyiri commented 2 months ago

Reopening again to make this user configurable.

JurajNyiri commented 2 months ago

Update interval is now configurable in https://github.com/JurajNyiri/HomeAssistant-Tapo-Control/releases/tag/5.4.29 .

Screenshot 2024-07-20 at 13 38 44
murtaugh82 commented 2 months ago

@murtaugh82 please let me know if it works as expected

feature works as expected. thx 😃

iKaew commented 1 month ago

Hi @murtaugh82,

I have the same C410 model which then having the same issue that latest firmware 1.0.17 caused it can't access locally. Not sure how you managed to fix it ?

carontony commented 1 month ago

Hi @murtaugh82,

I have the same C410 model which then having the same issue that latest firmware 1.0.17 caused it can't access locally. Not sure how you managed to fix it ?

You can maybe follow this informations to downgrade your firmware : https://sizeof.cat/post/tp-link-tapo-c310-camera/

Let us know.