Split topic and set flow variable within the same change node using part of topic

I hope someone can help me with my problem.

Situation: I receive data from a sensor (counter and topic are relevant for me). I have several sensors in use that provide me with counter data. I have a change node in which I process and set various data from the input.

My goal is, to read a flow variable that belongs to the respective sensor. I would like to execute everything in one change node if possible. In the change node in rule 3 I split msg.topic by regex to write the single topic elements into an array.

Topic example: sensor0/garden/water_tap_garden/SENSOR

In rule 4 I write the sensor name from the array to msg.sensor. The sensor name is the first part from the topic up to the first forward slash. So far everything seems to work fine.

In rule 5 I would like to read the flow variable "lastCount_SENSORx and write it to msg.lastCount, where SENSORx is the respective sensor. The variable lastCount_SENSORx is set at a later stage in the flow.

Unfortunately the last rule does not work. How can I set the flow variable lastCounter_xxxx in the same change node?

I made a quick flow to simulate the input.

[{"id":"42eb3fc4.68678","type":"inject","z":"718e29fe.ac8918","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"sensor0/garden/water_tap_garden/SENSOR\",\"payload\":{\"Time\":\"2021-07-21T13:42:43\",\"COUNTER\":{\"C1\":190}},\"qos\":0,\"retain\":false,\"_msgid\":\"a2d432de.610ec\"}","payloadType":"json","x":1170,"y":920,"wires":[["b935ca1c.23f238"]]},{"id":"b935ca1c.23f238","type":"function","z":"718e29fe.ac8918","name":"reformat","func":"//set flow variable for test purposes only\nflow.set(\"lastCount_sensor0\", 118)\n\n//set example input data to msg.payload. For test purposes only\nmsg = msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1300,"y":920,"wires":[["c93b0a66.01c9e8","73a83cab.e3cc34"]]},{"id":"c93b0a66.01c9e8","type":"debug","z":"718e29fe.ac8918","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1430,"y":880,"wires":[]},{"id":"73a83cab.e3cc34","type":"change","z":"718e29fe.ac8918","name":"counter","rules":[{"t":"move","p":"payload.COUNTER.C1","pt":"msg","to":"newCount","tot":"msg"},{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"sensor","pt":"msg","to":"$match(topic, /(?:[^\\/\\n]|\\/\\/)+/)\t","tot":"jsonata"},{"t":"set","p":"sensor","pt":"msg","to":"sensor[0].match","tot":"msg"},{"t":"move","p":"lastCount_{{{sensor}}}","pt":"flow","to":"lastCount","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1440,"y":920,"wires":[["1c4f8fb5.1b07f"]]},{"id":"1c4f8fb5.1b07f","type":"debug","z":"718e29fe.ac8918","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1570,"y":880,"wires":[]}]

Thanks

Might be easier to make the flow context variable lastCount an object then you can simply merge the new count of property senser0

e.g.

[{"id":"42eb3fc4.68678","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"sensor0/garden/water_tap_garden","payload":"{\"Time\":\"2021-07-21T13:42:43\",\"COUNTER\":{\"C1\":190}}","payloadType":"json","x":250,"y":2460,"wires":[["b935ca1c.23f238"]]},{"id":"b935ca1c.23f238","type":"function","z":"c74669a0.6a34f8","name":"reformat","func":"//set flow variable for test purposes only\nflow.set(\"lastCount.sensor0\", 118)\n\n//set example input data to msg.payload. For test purposes only\n//msg = msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":2460,"wires":[["c93b0a66.01c9e8","73a83cab.e3cc34"]]},{"id":"c93b0a66.01c9e8","type":"debug","z":"c74669a0.6a34f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":2420,"wires":[]},{"id":"73a83cab.e3cc34","type":"change","z":"c74669a0.6a34f8","name":"counter","rules":[{"t":"move","p":"payload.COUNTER.C1","pt":"msg","to":"newCount","tot":"msg"},{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"sensor","pt":"msg","to":"$split(topic, \"/\")[0]\t","tot":"jsonata"},{"t":"set","p":"lastCount","pt":"flow","to":"$merge([$flowContext(\"lastCount\"),{sensor:newCount}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2460,"wires":[["1c4f8fb5.1b07f"]]},{"id":"1c4f8fb5.1b07f","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":2420,"wires":[]}]

i believe from 1.3 on you can also use lastCount[msg.payload]. You can not use mustache.

Thanks, I get an error with your adapted flow. In the function node the flow context seems not to work. At least I cannot see it under flow context.

flow.set("lastCount.sensor0", 118)

I assume lastCount.sensor0 is not a valid name for a context name. I guess this is then causing the error in the change node as the flow context is not set.

Invalid JSONata expression: Argument 1 of function "merge" must be an array of "objects"

I use version 1.3.5.

Any idea how to fix that?

Hi
Just using the function node sets the context
see image

Then joining the change node, you see the context update.

Yes if context is not an object(can be empty) then the merge will produce an error. You can expand the expression to account for this. I was just giving a simple example. for testing you can use an inject and achange node to initialise the context to an empty object e.g. {}

[edit]
my inject is different to your original, i put topic in its own property and set msg.payload on it's own. I also commented out msg = msg.payload; in the function node.

With initialization of the flow context {} it worked. Thanks for your help.

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