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