Node-RED Home Assistant set_location issue

Greetings!

I'm pretty new to this automation world, but since a month or so playing around with Home Assistant and Node-RED in my home on wheels.

I installed on a Raspberry Pi 4 4GB, Home Assistant in Docker and first also had Node-RED in a container, but didn't like that I can't write files outside of this container. So I installed Node-RED with the install script locally. Added node-red-contrib-home-assistant-websocket and installed with HACS the connection according to: https://bit.ly/3tductt
All on the same Pi.

Since I'm really not a programmer, but very enthusiastic learner, I asked around on the web how people got info's through.

A nice thing I like is temperature and locations, So I wanted to get my GPS location to be updated as my home location.

My electrical system is only made out of Victron chargers/inverters and a CCGX with an USB GPS connected.
Unfortunately the GPS is pretty unstable lately, but a new one is on it's way (from Germany to Greece)

Now the issue:

I'm getting via MQTT an message from the CCGX with my location.
Since I'm not able to write a javascript, I'm "Changing" and "Joining" the message until it's ready to send to the homeassistant.set_location service into Home Assistant.

[{"id":"859a6112.889c4","type":"tab","label":"GPS location","disabled":false,"info":""},{"id":"1ff89969.5dfcb7","type":"mqtt in","z":"859a6112.889c4","name":"Longitude","topic":"N/7c010a8eed8e/gps/0/Position/Longitude","qos":"2","datatype":"auto","broker":"f8ba937.ec033f","x":100,"y":140,"wires":[["1687ce5c.d1295a"]]},{"id":"f29b69e9.530d28","type":"mqtt in","z":"859a6112.889c4","name":"Latitude","topic":"N/7c010a8eed8e/gps/0/Position/Latitude","qos":"1","datatype":"auto","broker":"f8ba937.ec033f","x":90,"y":200,"wires":[["fe73062.86306f8"]]},{"id":"1687ce5c.d1295a","type":"change","z":"859a6112.889c4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"value","fromt":"str","to":"longitude","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":140,"wires":[["8d207e09.6f965"]]},{"id":"fe73062.86306f8","type":"change","z":"859a6112.889c4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"value","fromt":"str","to":"latitude","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":200,"wires":[["e7b526d1.a2b17"]]},{"id":"8d207e09.6f965","type":"json","z":"859a6112.889c4","name":"","property":"payload","action":"obj","pretty":false,"x":490,"y":140,"wires":[["9bd3eded.b29f6"]]},{"id":"e7b526d1.a2b17","type":"json","z":"859a6112.889c4","name":"","property":"payload","action":"obj","pretty":false,"x":490,"y":200,"wires":[["9bd3eded.b29f6"]]},{"id":"9bd3eded.b29f6","type":"join","z":"859a6112.889c4","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":"30","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":660,"y":180,"wires":[["61487b0.821a104"]]},{"id":"3c72d85f.1e68e","type":"api-call-service","z":"859a6112.889c4","name":"","server":"161793d1.ba3dec","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"set_location","entityId":"","data":"msg.payload","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":950,"y":280,"wires":[["8dfc5bcd.f785e"]]},{"id":"61487b0.821a104","type":"delay","z":"859a6112.889c4","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":840,"y":180,"wires":[["3c72d85f.1e68e"]]},{"id":"8dfc5bcd.f785e","type":"debug","z":"859a6112.889c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":360,"wires":[]},{"id":"f8ba937.ec033f","type":"mqtt-broker","name":"CCGX","broker":"192.168.0.52","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"161793d1.ba3dec","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false}]

Now the strange thing happens:
My Memory (RAM) is getting flood.

I found this out, because I had a couple of times a complete freeze of my Pi.
I narrowed the problem down by not running Node-RED (but with MQTT being on via MQTT sensors in Home Assistant), and then deleted this flow.

Without this flow all is fine. Memory is nice and stable.

I know this flow can be optimized with a function node, but I've no idea how...

Anyone out there who can help?

So as your MQTT value is JSON, you should chose to parse the JSON into a native JS object in the MQTT node...
image

So, you wont need the JSON nodes.

Then there is no need to do string replacements in the 2 change nodes as we can do this later with JSONata or a function node.

Then set the join node to key/val...
image

What that does is use the topic to identify the property in the payload by using the topic as the final property name.

So to know what properties to use in your function, use the brilliant built in feature "copy path" from the debug sidebar to help you write your function...

So this is where you end up...

[{"id":"1ff89969.5dfcb7","type":"mqtt in","z":"859a6112.889c4","name":"Longitude","topic":"N/7c010a8eed8e/gps/0/Position/Longitude","qos":"2","datatype":"json","broker":"f03246cf.89b378","nl":false,"rap":false,"x":1280,"y":1200,"wires":[["9bd3eded.b29f6"]]},{"id":"f29b69e9.530d28","type":"mqtt in","z":"859a6112.889c4","name":"Latitude","topic":"N/7c010a8eed8e/gps/0/Position/Latitude","qos":"1","datatype":"json","broker":"","nl":false,"rap":false,"x":1270,"y":1260,"wires":[["9bd3eded.b29f6"]]},{"id":"9bd3eded.b29f6","type":"join","z":"859a6112.889c4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":"30","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1430,"y":1240,"wires":[["ac25f8ee.120388"]]},{"id":"8dfc5bcd.f785e","type":"debug","z":"859a6112.889c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":1240,"wires":[]},{"id":"ac25f8ee.120388","type":"function","z":"859a6112.889c4","name":"","func":"msg.payload = {\n    longitude: msg.payload[\"N/7c010a8eed8e/gps/0/Position/Longitude\"].value,\n    latitude: msg.payload[\"N/7c010a8eed8e/gps/0/Position/Latitude\"].value\n    \n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1560,"y":1240,"wires":[["8dfc5bcd.f785e"]]},{"id":"f03246cf.89b378","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"5","keepalive":"55","cleansession":true,"birthTopic":"demo/lwt/birth","birthQos":"0","birthPayload":"birth","birthMsg":{},"closeTopic":"demo/lwt/close","closeQos":"0","closePayload":"close","closeMsg":{},"willTopic":"demo/lwt/will","willQos":"0","willPayload":"will","willMsg":{},"sessionExpiry":""}]
1 Like

Ah, thank you so much Steve!

I implemented your optimisation, but unfortunately this doesn't seem to get my problem solved.

I'm still hogging memory, just until the freeze sets in.
I ran the profiler in HA and found out that the service I'm calling with set_location is hogging this memory.

Unless someone here is familiar with my combination, I'll ask the HA community if some has a clue there.

Thank you so much!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.