No connection error

So I have this flow that is suppose to active two Switchs three times each. Sometimes I get a “No connection error” which will then skip an activation on the first switch. Any thoughts our guidance on this?

[{"id":"28fef98818596f60","type":"tab","label":"Bed Warmer","disabled":false,"info":"","env":[]},{"id":"98805b1fceb0f656","type":"server-state-changed","z":"28fef98818596f60","name":"Night Time Mode Activated","server":"58789d9e.7d1274","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"scene.bed_time","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":390,"y":440,"wires":[["781d8babde1e323a"]]},{"id":"0f231fe4979f1b1f","type":"split","z":"28fef98818596f60","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":790,"y":500,"wires":[["0a93e223b2b8198a"]]},{"id":"781d8babde1e323a","type":"change","z":"28fef98818596f60","name":"Set array 1 to n","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0..2]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":500,"wires":[["0f231fe4979f1b1f"]]},{"id":"0a93e223b2b8198a","type":"delay","z":"28fef98818596f60","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"20","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":960,"y":500,"wires":[["ca94d21053e0e603"]]},{"id":"ca94d21053e0e603","type":"api-call-service","z":"28fef98818596f60","name":"Toggle Bed Left Side","server":"58789d9e.7d1274","version":5,"debugenabled":false,"domain":"switch","service":"toggle","areaId":[],"deviceId":["673182d2fb6e13ccd9030cb017b0084d"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1180,"y":500,"wires":[["34dd07f465d8c389"]]},{"id":"a2a50fb269b5697e","type":"join","z":"28fef98818596f60","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1370,"y":580,"wires":[[]]},{"id":"de16c07f02115655","type":"inject","z":"28fef98818596f60","name":"9:15","props":[],"repeat":"","crontab":"15 21 * * *","once":false,"onceDelay":0.1,"topic":"","x":450,"y":580,"wires":[["781d8babde1e323a"]]},{"id":"34dd07f465d8c389","type":"api-call-service","z":"28fef98818596f60","name":"Toggle Bed Right Side","server":"58789d9e.7d1274","version":5,"debugenabled":false,"domain":"switch","service":"toggle","areaId":[],"deviceId":["35dc5847a29bf11099a14c7a1319d2cf"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1180,"y":580,"wires":[["a2a50fb269b5697e"]]},{"id":"58789d9e.7d1274","type":"server","name":"Home Assistant","addon":true}]```

Are you using the addon? Where the debug is choose config and make sure there isn’t more than one ha server entry. If there is more than one, delete it and deploy. You will get a message that x nodes are not configured, choose the find option.

image

Also if a device provides entities always use the entity and don’t set the device in the call service.

Your flow shows that you are using Node-RED as an add-on, and have your Home Assistant server (configuration) node set accordingly.

However, you have no other settings ticked, so it may well be that the connection is closing (timing out) and not responding on your first call.

The HA WebSocket nodes use both a WebSocket connection and API calls to connect with Home Assistant. WebSocket connections are ‘always on’, however API calls are made as required - first a connection is setup, then the call. Hence if the connection has gone to sleep, it may take a few seconds (up to 10+ I think) for everything to come back ‘on-line’. This is often an issue when Node-RED restarts, as flows can be firing but the HA connection has not yet been made.

You can mitigate this by using the ‘delay connection attempts’ option that stalls calls to HA to allow more time for the connection to be made. Alternatively, there is the ‘enable heartbeat’ option, which will ping over the WebSocket to keep the connection alive.

The option I always use is the ‘Enable global context store’. This causes the server to pull in a complete state record from HA, which is stored in Global context and continually updated. There are advantages to having a copy of the HA entity state, which can be used and examined, and this also powers the ‘auto complete’ facility when adding entity id names to UI selection fields. The big value is that, as this keeps the HA state record up to date by listening to state event changes, it is also keeping the connection alive.

By using the Global context store option, and with several busy flows that run all the time, I find that my connection is always on and always responsive.

Add:

I use this short flow, which runs all the time and every 10 seconds sends a timestamp update from Node-RED to a sensor entity in Home Assistant. This allows me to easily see if NR has stopped, but also keeps the connection busy and therefore alive.

[{"id":"badbcb38e096aa2e","type":"inject","z":"4459582bc445079d","g":"904db99207642198","name":"Every 10 seconds","props":[],"repeat":"10","crontab":"","once":true,"onceDelay":"10","topic":"","x":170,"y":60,"wires":[["976fe0a61ab6765a"]]},{"id":"976fe0a61ab6765a","type":"ha-sensor","z":"4459582bc445079d","g":"904db99207642198","name":"HA2 Heart Beat -> HA1","entityConfig":"e7667a5632a512c3","version":0,"state":"$now('[FNn,3-3] [MNn,3-3] [D01] [Y] [H01]:[m01]:[s01] [z]')","stateType":"jsonata","attributes":[],"inputOverride":"merge","outputProperties":[],"x":410,"y":60,"wires":[[]],"server":""},{"id":"e7667a5632a512c3","type":"ha-entity-config","server":"","deviceConfig":"","name":"SC HA2 HBeat TS","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"HA2 Heart Beat TS"},{"property":"icon","value":"mdi:update"},{"property":"entity_category","value":""},{"property":"entity_picture","value":""},{"property":"device_class","value":"timestamp"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false}]

Interesting. I’m very new to all of this. So you just have this flow running like this all the time?

I have two Home Assistant devices (both running Node-RED as an addon) and a third Revolution Pi, running standalone Node-RED. All three instances of Node-RED connect back to my main Home Assistant.

My main machine runs many flows on continuous loops, polling inverter and battery state, and so API and WebSocket traffic between my (same machine) NR addon and the HA is at least every 5 or 10 seconds. I have never had any issue with websocket-node connectivity, probably because the connection never gets the opportunity to go to sleep.

I do have connection issues with my Modbus server. Most connections between computers do time-out and go to sleep, so I have to fire Modbus commands twice - the first wakes the connection back up, and then by the time the second command comes along, the connection is up and working.

My Revolution Pi is currently in soak-test for reliability, and being used for development of new Node-RED flows, so I am not running anything in this Node-RED. I do have the monitoring flow running so that it keeps the connection alive, and I can check on the state of Node-RED.

You can see that, last night, my Revolution Pi went AWOL (I never have this problem with any of my HA Odroid machines but the Rev Pi has an issue with the ethernet connection dropping every few days).

The point of the never-ending monitoring flow is

  • I can see that my Node-RED / devices / critical flows are working
  • I can keep the connection alive and responsive by keeping it busy
  • I can easily see at exactly what time something went wrong (history)

It costs nothing to add and run a flow like this. I also now have flows monitoring the machine and Node-RED JS heap memory usage, processor temperature and so on. The right hand table in my picture above is a circular list of the failure events in my Modbus inverter read, which is 9 blocks read every 20 seconds. When a block read fails, that timestamp/block is added to context memory and allows me to look for unusual events.

It also keeps the connection alive and healthy!