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

**URL:** <https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710>\
**Category:** General\
**Created:** [21 July 2021 13:10 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710 "2021-07-21T13:10:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [21 July 2021 13:10 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/1 "2021-07-21T13:10:50Z")

</div>

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.

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 July 2021 15:35 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/2 "2021-07-21T15:35:53Z")

</div>

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.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [21 July 2021 19:10 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/3 "2021-07-21T19:10:56Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 July 2021 19:49 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/4 "2021-07-21T19:49:58Z")

</div>

Hi  
Just using the function node sets the context  
see image

 ![Screenshot_20210721-204433](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92b2a065458d4c46473f81e5ed5e9d4ea487aa4c.png)

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

 ![Screenshot_20210721-204516](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7f8032438d4d22c729620977fa6751084c58120.png)

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.

---

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [22 July 2021 06:22 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/5 "2021-07-22T06:22:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 August 2021 06:22 UTC](https://discourse.nodered.org/t/split-topic-and-set-flow-variable-within-the-same-change-node-using-part-of-topic/48710/6 "2021-08-05T06:22:45Z")

</div>

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