home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.13k stars 29.81k forks source link

PoE Camera lost Connection in HA but accessable in Browser #98530

Closed bernithiel closed 1 year ago

bernithiel commented 1 year ago

The problem

Hi Team, i use HA and the Integrations of Reolink incl. ONVIF

When i access my PoE Camera by Browser all working fine.

After i added device by Reolink or ONVIF it looses after 1-2d the Camera image

Config entry 'cam004 - ec:71:db:94:72:bb' for onvif integration not ready yet: Could not connect to camera 192.168.100.14:80: ; Retrying in background Config entry 'cam004' for reolink integration not ready yet: Error while trying to setup 192.168.100.14:443: Host 192.168.100.14:443: failed to subscribe push: Host 192.168.100.14:443: connection timeout exception.; Retrying in background

Host 192.168.100.14:443: API error: API returned HTTP status ERROR code 502/Bad Gateway. Error while logging out: API returned HTTP status ERROR code 502/Bad Gateway

aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

What version of Home Assistant Core has the issue?

2023.8.1

What was the last working version of Home Assistant Core?

2023.8.1

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @starkillerog, mind taking a look at this issue as it has been labeled with an integration (reolink) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `reolink` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign reolink` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


reolink documentation reolink source (message by IssueLinks)

home-assistant[bot] commented 1 year ago

Hey there @hunterjm, mind taking a look at this issue as it has been labeled with an integration (onvif) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `onvif` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign onvif` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


onvif documentation onvif source (message by IssueLinks)

starkillerOG commented 1 year ago

@bernithiel you schould not be using the ONVIF and reolink integrations at the same time. The camera has a limited number of connections and can get confused if there are 2 connections from the same IP.

So please chose either the Reolink or ONVIF integration.

bernithiel commented 1 year ago

@bernithiel you schould not be using the ONVIF and reolink integrations at the same time. The camera has a limited number of connections and can get confused if there are 2 connections from the same IP.

So please chose either the Reolink or ONVIF integration.

@starkillerOG i saw, i changed, but it looses allways connection in homeassistant, i checked by ping and their is no connection lost on network.

starkillerOG commented 1 year ago

@bernithiel do you have a static IP set for that camera? Are you sure the integration is using the correct IP?

Did you try power-cycling the camera? What model and hardware version is it and what firmware version is it running?

Please turn on debug logging for the reolink integration and post the debug logs here.

What kind of network configuration do you have? Is the HomeAssistant device in the same subnet/vlan as the Reolink PoE camera? No network restrictions on ports or anything between the HA device and camera?

starkillerOG commented 1 year ago

Is the PoE camera also connected to a NVR?

bernithiel commented 1 year ago

@starkillerOG
its not connected to a NVR

E1 Outdoor PoE build 23030800 IPC_560SD88MP v3.1.0.1856_23030800

i use in general my own DHCP so all devices are having a DHCP static ip mapped with MAC Address

bernithiel commented 1 year ago

so all possible restrictions like Port, IP, DNS, Forwards, Firewall and co i checked, as said it has connection than it looses.

only stable variante i found is over go2rtc but than i dont have all the control stuff which a reolink/onvif integration bring with

in general my camera setup looks like following

starkillerOG commented 1 year ago

i use in general my own DHCP so all devices are having a DHCP static ip mapped with MAC Address

@bernithiel So this means you set the static IP in the router right? That is the correct way, do not set a static IP in the camera settings, that can cause issues.

So frigate does not connect to the camera directly right? You use camera->HA reolink integration -> go2rtc -> frigate, right?

I know multiple connections to the same camera can cause issues like this. Maybe turn of frigate for testing and see if you then still see these errors (after a power cycle of the camera)? I think too many login attemps/connections that are dropped instead of properly closed can also cause simular issues.

If you go to the reolink pc client and go to settings->system->user management how many connected sessions/users do you see there?

bernithiel commented 1 year ago

i use in general my own DHCP so all devices are having a DHCP static ip mapped with MAC Address

@bernithiel So this means you set the static IP in the router right? That is the correct way, do not set a static IP in the camera settings, that can cause issues.

So frigate does not connect to the camera directly right? You use camera->HA reolink integration -> go2rtc -> frigate, right?

I know multiple connections to the same camera can cause issues like this. Maybe turn of frigate for testing and see if you then still see these errors (after a power cycle of the camera)? I think too many login attemps/connections that are dropped instead of properly closed can also cause simular issues.

If you go to the reolink pc client and go to settings->system->user management how many connected sessions/users do you see there?

correct, my Router make DHCP , behind i have a MAC Table where device allways get same IP.

actually, cause it wasnt working well i dont use ONVIF / Reolink integration actually, i use the config file of go2rtc and out of the go2rtc links i use this links to use inside Frigate and WebRTC

IP > Settings > System > User Management i only see the created user but no information about how much sessions in use.

garry0garry commented 1 year ago

The camera has a limited number of connections

After the disappearance of the reolink_cctv integration, I switched to your integration. Now periodically I observe the impossibility of receiving the main stream from the cameras. But if I try to get the main stream from a Windows or Android application, I never had a problem with the stream. What could it be? Is there a workaround in the form of a timeout setting? Why does it always work with the GUI, but not always with the integration? Internal network 1Gb, no errors were found on CISCO switches. I don't use go2rtc and frigate.

List of my cameras (17 pcs., Ethernet only): Reolink RLC-510A, hw IPC_523128M5MP_V2, sw v3.1.0.2109_23050501 - 2 pcs. Reolink RLC-511WA, hw IPC_523128M5MP, sw v3.1.0.1983_23040614 - 1 pc. Reolink RLC-520A, hw IPC_523128M5MP, sw v3.1.0.1983_23040605 - 9 pcs. Reolink RLC-520A, hw IPC_523128M5MP_V2, sw v3.1.0.2109_23050502 - 3 pcs. Reolink RLC-811A, hw IPC_523128M8MP, sw v3.1.0.1983_23040613 - 1 pc. Reolink RLC-811A, hw IPC_560B158MP, sw v3.1.0.2144_23042803 - 1 pc.

Examples of unavailability:

Logger: homeassistant.helpers.service
Source: helpers/service.py:833
First occurred: 27 августа 2023 г. в 21:24:48 (3 occurrences)
Last logged: 21:08:32

Referenced entities camera.cam8_main are missing or not currently available
Logger: reolink_aio.api
Source: components/reolink/host.py:260
First occurred: 08:59:43 (7 occurrences)
Last logged: 08:59:43

Host 192.168.50.19:80: API error: API returned HTTP status ERROR code 502/Bad Gateway.
Error while logging out: API returned HTTP status ERROR code 502/Bad Gateway
Logger: reolink_aio.api
Source: components/reolink/camera.py:85
First occurred: 08:58:43 (1 occurrences)
Last logged: 08:58:43

Host 192.168.50.19:80: unknown exception "Session is closed" occurred, traceback: Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/reolink_aio/api.py", line 3687, in send_chunk response = await self._aiohttp_session.get(url=self._url, params=param, allow_redirects=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 560, in _request await resp.start(conn) File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 894, in start with self._timer: File "/usr/local/lib/python3.11/site-packages/aiohttp/helpers.py", line 721, in __exit__ raise asyncio.TimeoutError from None TimeoutError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/reolink_aio/api.py", line 3700, in send_chunk response = await self._aiohttp_session.post(url=self._url, json=body, params=param, allow_redirects=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 400, in _request raise RuntimeError("Session is closed") RuntimeError: Session is closed
garry0garry commented 1 year ago

But if I try to get the main stream from a Windows or Android application, I never had a problem with the stream.

UPD. This also works through the FireFox.

starkillerOG commented 1 year ago

@garry0garry you could try out diffrent protocols (you can switch in the configuration options of the reolink integration). The official integration uses RTSP as standard, the old HACS reolink_cctv uses RTMP as standard, so you could try switching to RTMP or try out FLV protocols. See https://www.home-assistant.io/integrations/reolink/#options. The webbrowser client of reolink uses the FLV protocol if I remeber correctly. The reolink app/windows client of reolink uses there own locked down baichuan protocol that is not available for outside use.

By the way the old HACS integration was most likely ignoring network errors and keeping the entities in the available state instead of marking them as unavailable.

@garry0garry do you also use a other program which is accesing the camera like firgate or blue iris?

The unknown exception "Session is closed" occurred is a legit error, I will look into that one.

bernithiel commented 1 year ago

@starkillerOG this is the main idea not to use the "Reolink" Integration , to came mainly out of "go2rtc" cause i learned in last weeks that Reolinks PoE Cameras struggle when to much connections in one stream and with go2rtc you have 1 Reolink Stream by Camera and the other Streams are like substreams but from go2rtc

so the issue seems inside frigate or go2rtc coming

starkillerOG commented 1 year ago

@bernithiel In that case you could just simply disable the camera entities of the reolink integration and use the camera streams from frigate/go2rtc. (if you want to use the controls supplied by the reolink integration). Not sure if the motion/AI subscription will impact frigate/go2rtc stability....

garry0garry commented 1 year ago

@garry0garry do you also use a other program which is accesing the camera like firgate or blue iris?

No, I do not use.

Today I found that the message "Referenced entities camera.cam8_main are missing or not currently available" is correct because cam8_main was not activated.

starkillerOG commented 1 year ago

@garry0garry this code change https://github.com/starkillerOG/reolink_aio/commit/92cac1fcbdcafbfb6d9f12842631a1c40241c884 schould at least catch the Session closed error if it occurs due to another error in a parrallel call.

garry0garry commented 1 year ago

@starkillerOG, thank you! I changed my api.py and reloaded HA. I monitor the log.

garry0garry commented 1 year ago

Why does the integration check port 443 if port 80 is specified in the settings?

Logger: homeassistant.components.reolink
Source: helpers/update_coordinator.py:229

Integration: Reolink IP NVR/camera (documentation, issues)
First occurred: 07:53:23 (9 occurrences)
Last logged: 15:44:15

Error fetching reolink.cam15 data: Host 192.168.50.153:443: connection error: Cannot connect to host 192.168.50.153:443 ssl:default [Connect call failed ('192.168.50.153', 443)]
Error fetching reolink.cam14 data: Host 192.168.50.7:443: connection error: Cannot connect to host 192.168.50.7:443 ssl:default [Connect call failed ('192.168.50.7', 443)]
Error fetching reolink.cam3 data: Host 192.168.50.62:443: connection error: Cannot connect to host 192.168.50.62:443 ssl:default [Connect call failed ('192.168.50.62', 443)]
Error fetching reolink.cam16 data: Host 192.168.50.202:443: connection error: Cannot connect to host 192.168.50.202:443 ssl:default [Connect call failed ('192.168.50.202', 443)]
Error fetching reolink.cam18 data: Host 192.168.30.179:443: Timeout error:
starkillerOG commented 1 year ago

@garry0garry So this is how it works: During config flow (initial setup), it first does not ask for a port/ssl combination and just tries first the port 443 and then the port 80, the first one that works will be used. If however an error occurs it will give you the option to specify a custom port/ssl combination, both the port and SSL box must be provided in order for it to be used (so you may need to click and unclick the SSL box). If the port and SSL are both specified that one is used during config flow.

As soon as a succesfull config flow is reached, the port and SSL are saved and always used. If you want to change this, you can setup the integration again (withouth removing the already configured integration), it will then enter the reautentication flow which allows you to go through the config flow again and change the username/password/port/ssl for an existing integration.

So apperently in your config port 443 is saved and not port 80, which ports are open on your camera's?

garry0garry commented 1 year ago

@starkillerOG I have configured all integration cameras on port 80. All cameras are available on ports 80 and 443. How can I check the IP:Port integration settings? изображение

I have now run tcpdump. I see that some cameras use 443 instead of 80... I don't understand anything.

starkillerOG commented 1 year ago

@garry0garry there is no easy way to see that (not something normal users should worry about). You can see it in the config directery under .storage (this is a hidden folder) in the core.config_entries file. You can open that file in notepad and search for "domain": "reolink".

BE WARNED: watch out with those files and DO NOT CHANGE ANYTHING, messing up those files will break your HomeAssistant causing it to not start anymore. Only way to solve that is (undowing what you did if you have the knoladge) or removing all of the configuration causing a full reset of HomeAssistant whith everything beeing lost (fresh install).

garry0garry commented 1 year ago

@starkillerOG .storage/core.config_entries 9 cameras on 80 (HTTP), 8 cameras on 443 (HTTPS).

Ok. Why was the notification in the camera unreachable log only on HTTPS cameras, despite the fact that the network was physically unavailable on HA for all cameras? There was no alert about HTTP.

Do I understand correctly that I can't replace values in a file? from

          "port": 443,
          "use_https": true

to

          "port": 80,
          "use_https": false

?

starkillerOG commented 1 year ago

Hmmm that is a good point, no idea why HTTPS would give issues while HTTP does not. Maybe because of the added load on the camera to encrypt the messages, but I think that schould not be a problem.

Well actually you could replace them, but be sure to make a backup of the file before messing with it. make the change and immediatly restart the complete HomeAssistant. But of course these files are not ment to be messed with.

Easier would be to close the HTTPS ports in the setting of the camera's (reolink desktop client (if you want to be using HTTP only). Then restart the reolink integrations. Look which camera's are now having trouble setting up and are erroring, for those cameras find the IP, username and password. Then setup the reolink integration again just like you would add a new camera, but instead use the already configured IP username password combination. You should get some message like "this camera was already configured", but in fact the connection details will be updated and it schould automatically switch to HTTP (port 80) since port 443 is now closed. You can check again in the .storage/core.config_entries file afterwards.

garry0garry commented 1 year ago

I am editing .storage/core.config_entries. Now not 9 cameras on 80 (HTTP) and 8 cameras on 443 (HTTPS), but 1 camera on 80 (HTTP) and 16 cameras on 443 (HTTPS). Changed without me ... Replaced everywhere by 80. Restarted HA. Works. I need port 443 on the cameras for other purposes.

It seems to me that port self-replacement is not the best choice of algorithm.

bernithiel commented 1 year ago

@garry0garry in reolink webinterface you see that either port 80 or 443 cause when both the reolink integration struggle, their is no need for an http/80 so enable https/443 only and all should work fine :)

starkillerOG commented 1 year ago

If you appreciate the reolink integration and want to support its development, please consider sponsering the upstream library or purchase Reolink products through this affiliate link.