# Maths expression in switch node (jsonata?)

**URL:** <https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918>\
**Category:** General\
**Created:** [19 September 2022 11:19 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918 "2022-09-19T11:19:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [19 September 2022 11:19 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/1 "2022-09-19T11:19:40Z")

</div>

Hello. I am trying to model out a potential application in Node-RED whereby I gradually reduce the speed of a pump that is filling a tank, so that the flowrate gradually decreases as the tank fills up. Think of slowing down the flow of water from the pump when filling up a fishtank as it starts to reach the maximum level, so as to avoid overfilling.

In my mockup, I assumed a 750 liter tank and a pump that flows at 10 liters/min. The pump runs at full flow rate up to the point where the tank is filled to 250 liters. I set up a switch node to decrease the pump flow rate as the tank is filled more and more. I have the fill\_level set as a flow.context, which is used by the switch node.

The attached flow works fine, but the switch node uses static ranges for the tank fill\_level (e.g. \<250, 251-400, 401-550, etc.). I would like to do the same general switch node, but using percentages, i.e. if tank is 50% full, then send to output 2; if tank is 75% full, then send to output 3; etc. But I do not see any obvious options in the switch node settings for how this can be done. Any hints or suggestions are appreciated. I have a feeling that a JSONata expression can achieve this.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2dda1783e961255a3cdf04193dd8acfdcdabfd9a.png)

```auto
[{"id":"a185545da272dacb","type":"inject","z":"3b9f706f88df3650","name":"Pump Flowrate","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":130,"y":480,"wires":[["633040094a5811be"]]},{"id":"c2867097a9f33e74","type":"debug","z":"3b9f706f88df3650","name":"10% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":420,"wires":[]},{"id":"633040094a5811be","type":"switch","z":"3b9f706f88df3650","name":"","property":"fill_level","propertyType":"flow","rules":[{"t":"lt","v":"250","vt":"num"},{"t":"btwn","v":"251","vt":"num","v2":"400","v2t":"num"},{"t":"btwn","v":"401","vt":"num","v2":"550","v2t":"num"},{"t":"btwn","v":"551","vt":"num","v2":"700","v2t":"num"},{"t":"btwn","v":"701","vt":"num","v2":"750","v2t":"num"}],"checkall":"true","repair":false,"outputs":5,"x":290,"y":480,"wires":[["25002a565c6e77aa"],["b6869bbc3ddda501"],["f61c6ec48a30d565"],["f91fa1bdba4d3ffa"],["df563ce77daef57a"]]},{"id":"5794b071a373e98a","type":"inject","z":"3b9f706f88df3650","name":"750","props":[{"p":"topic","v":"750","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":80,"wires":[["2766485d95329272"]]},{"id":"47e6ccd0f3908757","type":"inject","z":"3b9f706f88df3650","name":"725","props":[{"p":"topic","v":"725","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":120,"wires":[["2766485d95329272"]]},{"id":"3f3001e270419cec","type":"inject","z":"3b9f706f88df3650","name":"600","props":[{"p":"topic","v":"600","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":160,"wires":[["2766485d95329272"]]},{"id":"f947756b414de641","type":"inject","z":"3b9f706f88df3650","name":"550","props":[{"p":"topic","v":"550","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":200,"wires":[["2766485d95329272"]]},{"id":"43e4e03aaf23d1f5","type":"inject","z":"3b9f706f88df3650","name":"400","props":[{"p":"topic","v":"400","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":240,"wires":[["2766485d95329272"]]},{"id":"74088ce02a700617","type":"inject","z":"3b9f706f88df3650","name":"350","props":[{"p":"topic","v":"350","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":280,"wires":[["2766485d95329272"]]},{"id":"78bc2de2535e563e","type":"inject","z":"3b9f706f88df3650","name":"240","props":[{"p":"topic","v":"240","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":320,"wires":[["2766485d95329272"]]},{"id":"2766485d95329272","type":"change","z":"3b9f706f88df3650","name":"Fill Level","rules":[{"t":"set","p":"fill_level","pt":"flow","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":220,"wires":[[]]},{"id":"b6869bbc3ddda501","type":"function","z":"3b9f706f88df3650","name":"Reduce flowrate by 10%","func":"msg.payload = msg.payload*.90\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":420,"wires":[["c2867097a9f33e74"]]},{"id":"25002a565c6e77aa","type":"debug","z":"3b9f706f88df3650","name":"No reduction in flow","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":380,"wires":[]},{"id":"f61c6ec48a30d565","type":"function","z":"3b9f706f88df3650","name":"Reduce flowrate by 30%","func":"msg.payload = msg.payload*.70\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":460,"wires":[["97abd5489ba572d1"]]},{"id":"97abd5489ba572d1","type":"debug","z":"3b9f706f88df3650","name":"30% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":460,"wires":[]},{"id":"f91fa1bdba4d3ffa","type":"function","z":"3b9f706f88df3650","name":"Reduce flowrate by 40%","func":"msg.payload = msg.payload*.60\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":500,"wires":[["5f6357ba7383ef7f"]]},{"id":"5f6357ba7383ef7f","type":"debug","z":"3b9f706f88df3650","name":"40% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":500,"wires":[]},{"id":"df563ce77daef57a","type":"function","z":"3b9f706f88df3650","name":"Reduce flowrate by 50%","func":"msg.payload = msg.payload*.50\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":540,"wires":[["296939d31ae973a3"]]},{"id":"296939d31ae973a3","type":"debug","z":"3b9f706f88df3650","name":"50% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":540,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 September 2022 11:24 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/2 "2022-09-19T11:24:42Z")

</div>

I'm sure that someone with a bigger brain than mine will be able to come up with some clever JSONata to do this. But for myself, I'd generally use a function node since you can work through the problem and build up the answer more easily.

---

<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:** [19 September 2022 11:40 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/3 "2022-09-19T11:40:47Z")

</div>

This should work you need to feed in the tank volume, i have hard coded at 750.

```auto
[{"id":"a185545da272dacb","type":"inject","z":"bf9e1e33.030598","name":"Pump Flowrate","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":1110,"y":1960,"wires":[["633040094a5811be"]]},{"id":"633040094a5811be","type":"switch","z":"bf9e1e33.030598","name":"","property":"$flowContext(\"fill_level\") / (750/100)","propertyType":"jsonata","rules":[{"t":"gt","v":"50","vt":"num"},{"t":"gt","v":"40","vt":"str"},{"t":"gt","v":"30","vt":"str"},{"t":"gt","v":"10","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":5,"x":1280,"y":1960,"wires":[["df563ce77daef57a"],["f91fa1bdba4d3ffa"],["f61c6ec48a30d565"],["b6869bbc3ddda501"],["25002a565c6e77aa"]]},{"id":"df563ce77daef57a","type":"function","z":"bf9e1e33.030598","name":"Reduce flowrate by 50%","func":"msg.payload = msg.payload*.50\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1500,"y":2020,"wires":[["296939d31ae973a3"]]},{"id":"f91fa1bdba4d3ffa","type":"function","z":"bf9e1e33.030598","name":"Reduce flowrate by 40%","func":"msg.payload = msg.payload*.60\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1500,"y":1980,"wires":[["5f6357ba7383ef7f"]]},{"id":"f61c6ec48a30d565","type":"function","z":"bf9e1e33.030598","name":"Reduce flowrate by 30%","func":"msg.payload = msg.payload*.70\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1500,"y":1940,"wires":[["97abd5489ba572d1"]]},{"id":"b6869bbc3ddda501","type":"function","z":"bf9e1e33.030598","name":"Reduce flowrate by 10%","func":"msg.payload = msg.payload*.90\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1500,"y":1900,"wires":[["c2867097a9f33e74"]]},{"id":"25002a565c6e77aa","type":"debug","z":"bf9e1e33.030598","name":"No reduction in flow","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1460,"y":1860,"wires":[]},{"id":"296939d31ae973a3","type":"debug","z":"bf9e1e33.030598","name":"50% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":2020,"wires":[]},{"id":"5f6357ba7383ef7f","type":"debug","z":"bf9e1e33.030598","name":"40% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":1980,"wires":[]},{"id":"97abd5489ba572d1","type":"debug","z":"bf9e1e33.030598","name":"30% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":1940,"wires":[]},{"id":"c2867097a9f33e74","type":"debug","z":"bf9e1e33.030598","name":"10% reduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":1900,"wires":[]}]

```

You could probably do this in a change node and output the flow rate also, without the need to save a flow var, do the calc on change of fill level input.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [19 September 2022 12:24 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/4 "2022-09-19T12:24:53Z")

</div>

Thanks to both of you. I had thought of (and might still attempt to write) a function node, but there is something about the Switch node that I find easier to use (even though a function node with multiple outputs looks the same from afar). The JSONata expression that @E1cid wrote will work fine and I can modify accordingly to not use hard-coded values in the future.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [20 September 2022 11:28 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/5 "2022-09-20T11:28:39Z")

</div>

Trying to apply this same logic to a weight-based program and had just one follow-up re: JSONata expressions that specify flowContext. Here is what my context variables look like:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9de4eaaea18fd6af48cb95841550712cc4f80d18.png)

In the Switch node JSONata expression, how would one specify scale\_weight as the property? Using `$flowContext("scale_weight")` does not appear to be working. Does one need to include the word "data" in there?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d53b02b017717fe51f6ab3370f9a083a015d7aaf.png)

---

<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:** [20 September 2022 12:10 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/6 "2022-09-20T12:10:31Z")

</div>

Yes, That would be `"data.scale_weight" `

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [20 September 2022 12:37 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/7 "2022-09-20T12:37:08Z")

</div>

Thank you! That worked perfectly.

---

<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:** [4 October 2022 12:37 UTC](https://discourse.nodered.org/t/maths-expression-in-switch-node-jsonata/67918/8 "2022-10-04T12:37:42Z")

</div>

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