zachowj / hass-node-red

Companion Component for node-red-contrib-home-assistant-websocket to help integrate Node-RED with Home Assistant Core
MIT License
442 stars 181 forks source link

NoConnectionError with the sensor node #236

Closed LucasJanin closed 6 months ago

LucasJanin commented 6 months ago

Version of the custom_component

hass-node-red: 3.1.2 node-red-contrib-home-assistant-websocket: 0.62.2 Node-RED : 3.1.3 Home Assistant: 2023.12.3

Describe the bug

Multiple times a day, sensor node of hass-node-red get disconnected from HA. The only solution is to restart the node-RED to resolve the issue.

node-red-contrib-home-assistant-websocket original node doesn't get affected and still working fine. The problem exists with Node-RED in HASSIO or outside in Docker too.

Screenshot 2023-12-15 at 23 21 45 Screenshot 2023-12-15 at 23 55 17

My test code in Node-RED

[{"id":"6e56358c5a8fe42b","type":"inject","z":"e3dcd162200ebc4e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":300,"wires":[["ebd0676054a41f57"]]},{"id":"ebd0676054a41f57","type":"function","z":"e3dcd162200ebc4e","name":"function 1","func":"const timestamp = msg.payload;\nconst date = new Date(timestamp);\nmsg.payload = date.toISOString();\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":300,"wires":[["5afb5fdd3292bd45"]]},{"id":"5afb5fdd3292bd45","type":"ha-sensor","z":"e3dcd162200ebc4e","name":"Node-RED Connection Docker","entityConfig":"c94d8ba86d4a210d","version":0,"state":"payload","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":950,"y":300,"wires":[[]]},{"id":"c94d8ba86d4a210d","type":"ha-entity-config","server":"250493a1475b0a1e","deviceConfig":"","name":"Node-RED Connection Docker","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Node-RED Connection Docker"},{"property":"icon","value":"mdi:connection"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":"timestamp"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false},{"id":"250493a1475b0a1e","type":"server","name":"Home Assistant","version":5,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":": ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"default","statusTimeFormat":"h:m:s","enableGlobalContextStore":false}]

Debug log

HA log

2023-12-14 23:39:55.386 DEBUG (MainThread) [custom_components.nodered] Entity Update: {'type': 'nodered/entity', 'server_id': '31f480d3.0e3b28', 'node_id': '6d2309cf7ded322f', 'state': '2023-12-15T04:39:55.377Z', 'attributes': {}, 'id': 73}

Node-RED log

15 Dec 23:39:52 - [info] [server:Home Assistant] Connecting to https://mxxxxx
15 Dec 23:39:56 - [info] [server:Home Assistant] Connected to https://mxxxxx
15 Dec 23:39:56 - [info] [server:Home Assistant] Connection closed to https://mxxxxx
15 Dec 23:39:56 - [info] [server:Home Assistant] Connecting to https://mxxxxx
15 Dec 23:39:57 - [error] [ha-sensor:Node-RED Connection Docker] NoConnectionError
15 Dec 23:40:00 - [info] [server:Home Assistant] Connected to https://mxxxxx
15 Dec 23:40:00 - [info] [server:Home Assistant] Connection closed to https://mxxxxx
15 Dec 23:40:00 - [info] [server:Home Assistant] Connecting to https://mxxxxx
15 Dec 23:40:04 - [info] [server:Home Assistant] Connected to https://mxxxxx
15 Dec 23:40:04 - [info] [server:Home Assistant] Connection closed to https://mxxxxx
15 Dec 23:40:04 - [info] [server:Home Assistant] Connecting to https://mxxxxx
15 Dec 23:40:08 - [info] [server:Home Assistant] Connected to https://mxxxxx
15 Dec 23:40:09 - [info] [server:Home Assistant] Connection closed to https://mxxxxx
zachowj commented 6 months ago

If the current state and sensor node use the same server config then it's the same connection. I would check your HA logs to see if there's a reason the disconnect is happening.

There's also a heartbeat check in the server config settings but I wouldn't call that a solution to your issue.

LucasJanin commented 6 months ago

Hi @zachowj

I confirm the current state and sensor node use the same server config

HA logs during the moment of issue start, I don't see anything special.

2023-12-15 21:23:45.715 DEBUG (MainThread) [custom_components.nodered.discovery] Discovery message: {'type': 'nodered/discovery', 'server_id': '250493a1475b0a1e', 'node_id': 'c94d8ba86d4a210d', 'config': {'name': 'Node-RED Connection Docker', 'icon': 'mdi:connection', 'device_class': 'timestamp'}, 'component': 'sensor', 'id': 54}
2023-12-15 21:23:45.717 INFO (MainThread) [custom_components.nodered.discovery] Updating sensor 250493a1475b0a1e c94d8ba86d4a210d
2023-12-15 21:23:51.323 DEBUG (MainThread) [custom_components.nodered.discovery] Discovery message: {'type': 'nodered/discovery', 'server_id': '250493a1475b0a1e', 'node_id': 'c94d8ba86d4a210d', 'config': {'name': 'Node-RED Connection Docker', 'icon': 'mdi:connection', 'device_class': 'timestamp'}, 'component': 'sensor', 'id': 84}
2023-12-15 21:23:51.324 INFO (MainThread) [custom_components.nodered.discovery] Updating sensor 250493a1475b0a1e c94d8ba86d4a210d
2023-12-15 21:23:51.771 WARNING (MainThread) [homeassistant.helpers.service] Referenced entities light.screen are missing or not currently available
2023-12-15 21:23:54.063 WARNING (MainThread) [homeassistant.components.media_player] Updating webostv media_player took longer than the scheduled update interval 0:00:10
2023-12-15 21:23:56.454 DEBUG (MainThread) [custom_components.nodered.discovery] Discovery message: {'type': 'nodered/discovery', 'server_id': '250493a1475b0a1e', 'node_id': 'c94d8ba86d4a210d', 'config': {'name': 'Node-RED Connection Docker', 'icon': 'mdi:connection', 'device_class': 'timestamp'}, 'component': 'sensor', 'id': 62}
2023-12-15 21:23:56.454 INFO (MainThread) [custom_components.nodered.discovery] Updating sensor 250493a1475b0a1e c94d8ba86d4a210d
2023-12-15 21:23:57.033 DEBUG (MainThread) [custom_components.nodered] Entity Update: {'type': 'nodered/entity', 'server_id': '250493a1475b0a1e', 'node_id': 'c94d8ba86d4a210d', 'state': '2023-12-16T02:23:57.025Z', 'attributes': {}, 'id': 64}

heartbeat is on, set to 30 seconds

zachowj commented 6 months ago

The current state node will not throw an error if it has loaded the entities at least once from Home Assistant. It could have been disconnected at the time it received input and it would have outputted the last cached value.

You could try turning up your logging level in HA to see if anything shows why it was disconnecting or could it be a networking issue?

LucasJanin commented 6 months ago

Thanks for your reply. I wasn't aware of the caching system. You are right. I have trouble with my networks. I'm investigating the issue.

LucasJanin commented 6 months ago

I had the idea to disable Node-RED's "heartbeat" and miraculously, the problem seems to be resolved for now. I'm hoping this is the solution to my issue. I will update the ticket in a few days.

LucasJanin commented 6 months ago

The issue didn't come back after disable Node-RED's "heartbeat"