# Comparing two variables to see if one is at-least 50 higher than the other

**URL:** <https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [3 March 2023 21:52 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083 "2023-03-03T21:52:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MonolithNZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monolithnz/32/77080_2.png) [@MonolithNZ](https://discourse.nodered.org/u/MonolithNZ)\
**Post date:** [3 March 2023 21:52 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/1 "2023-03-03T21:52:08Z")

</div>

Hi there. Apologies If I posted this in the wrong section of the forum.

I have Node-Red installed on my home assistance instance. I'm quite new to Node-Red and I'm struggling to create a flow for my robot vacuum. I have a sensor which tells me the total area the robot has cleaned, in meters. I want to compare that number, before and after a clean, and if the number has increased by 50, I'll send the vacuum to our dustbin for easy emptying.

My current flow is as follows:

I check the total area cleaned, and set a variable "Area\_Before\_Clean", using a change node. When the vacuum is returning to the dock, I set a variable "Area\_After\_Clean". I'd hoped I could then use a switch node to check if "Area\_After\_Clean" was at least 50 higher than "Area Before Clean" but unfortunately it doesn't look like the switch node is capable of this?

How would I go about doing this? Ideally I want to avoid the function node, as I'd prefer a simple visual representation of what's going on (and I don't know how to code 🫤)

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e5e50bf2d2c1685941a0f98ce7fdc84614a251a.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:** [3 March 2023 22:09 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/2 "2023-03-03T22:09:05Z")

</div>

The property input fields in the switch node can be a JSONata expression  
So if you set the property to JSONata J: `Area_After_Clean - Area_Before_Clean`  
You should get the result required.  
Then set the rule to `> number 50`

---

<div class="post-metadata">

**Author:** ![MonolithNZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monolithnz/32/77080_2.png) [@MonolithNZ](https://discourse.nodered.org/u/MonolithNZ)\
**Post date:** [3 March 2023 22:32 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/4 "2023-03-03T22:32:57Z")

</div>

Thanks very much! I've had a go at implementing your solution but I think I've misunderstood. Do I need to change the global variables to Json?

Here's the flow:

````auto
[{"id":"c6eea34d387d13e4","type":"tab","label":"Vacuum","disabled":false,"info":"","env":[]},{"id":"59f9f0610947de5d","type":"server-state-changed","z":"c6eea34d387d13e4","name":"","server":"8a644e13.f9644","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"vacuum.roborock_s6_maxv","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"cleaning","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":210,"y":220,"wires":[["f12cdf4c3c57a4c8"],[]]},{"id":"f12cdf4c3c57a4c8","type":"api-current-state","z":"c6eea34d387d13e4","name":"Total cleaned area","server":"8a644e13.f9644","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_total_clean_area","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":160,"wires":[["73e371be0976c0c5","9a22bdeb6618f0f3"]]},{"id":"73e371be0976c0c5","type":"ha-wait-until","z":"c6eea34d387d13e4","name":"Wait until returning to docl","server":"8a644e13.f9644","version":2,"outputs":1,"entityId":"vacuum.roborock_s6_maxv","entityIdFilterType":"exact","property":"state","comparator":"is","value":"Returning to dock","valueType":"str","timeout":"0","timeoutType":"num","timeoutUnits":"seconds","checkCurrentState":true,"blockInputOverrides":true,"outputProperties":[],"entityLocation":"data","entityLocationType":"none","x":940,"y":180,"wires":[["bffbc608a513909c"]]},{"id":"bffbc608a513909c","type":"api-current-state","z":"c6eea34d387d13e4","name":"Total cleaned area","server":"8a644e13.f9644","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_total_clean_area","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1370,"y":180,"wires":[["5c43ee8169094f0b"]]},{"id":"92607ca65b38ebbd","type":"inject","z":"c6eea34d387d13e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":120,"wires":[["f12cdf4c3c57a4c8"]]},{"id":"f4803adf40a1cfd3","type":"inject","z":"c6eea34d387d13e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1080,"y":100,"wires":[["bffbc608a513909c"]]},{"id":"9a22bdeb6618f0f3","type":"change","z":"c6eea34d387d13e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Area_Before_Clean","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":260,"wires":[["14012b24ac7d9bbc"]]},{"id":"5c43ee8169094f0b","type":"change","z":"c6eea34d387d13e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Area_After_Clean","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":240,"wires":[["14012b24ac7d9bbc"]]},{"id":"14012b24ac7d9bbc","type":"switch","z":"c6eea34d387d13e4","name":"","property":"Area_After_Clean - Area_Before_Clean","propertyType":"jsonata","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1310,"y":400,"wires":[["30a355bcf03586cd"]]},{"id":"30a355bcf03586cd","type":"debug","z":"c6eea34d387d13e4","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1560,"y":660,"wires":[]},{"id":"8a644e13.f9644","type":"server","name":"Home Assistant","addon":true}] ```

````

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [3 March 2023 22:38 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/5 "2023-03-03T22:38:21Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![MonolithNZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monolithnz/32/77080_2.png) [@MonolithNZ](https://discourse.nodered.org/u/MonolithNZ)\
**Post date:** [3 March 2023 22:40 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/6 "2023-03-03T22:40:39Z")

</div>

Thank you!

---

<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:** [3 March 2023 22:53 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/7 "2023-03-03T22:53:14Z")

</div>

Doubt i can help much as i don't have HA installed.  
You would have to do your flow inline and move each payload property, not sure if the HA nodes will pass on the existing msg properties.  
You could try something like this(untested )

```auto
[{"id":"9a22bdeb6618f0f3","type":"change","z":"c6eea34d387d13e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Area_Before_Clean","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":160,"wires":[["73e371be0976c0c5"]]},{"id":"f12cdf4c3c57a4c8","type":"api-current-state","z":"c6eea34d387d13e4","name":"Total cleaned area","server":"8a644e13.f9644","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_total_clean_area","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":160,"wires":[["9a22bdeb6618f0f3"]]},{"id":"73e371be0976c0c5","type":"ha-wait-until","z":"c6eea34d387d13e4","name":"Wait until returning to docl","server":"8a644e13.f9644","version":2,"outputs":1,"entityId":"vacuum.roborock_s6_maxv","entityIdFilterType":"exact","property":"state","comparator":"is","value":"Returning to dock","valueType":"str","timeout":"0","timeoutType":"num","timeoutUnits":"seconds","checkCurrentState":true,"blockInputOverrides":true,"outputProperties":[],"entityLocation":"data","entityLocationType":"none","x":940,"y":180,"wires":[["bffbc608a513909c"]]},{"id":"59f9f0610947de5d","type":"server-state-changed","z":"c6eea34d387d13e4","name":"","server":"8a644e13.f9644","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"vacuum.roborock_s6_maxv","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"cleaning","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":210,"y":220,"wires":[["f12cdf4c3c57a4c8"],[]]},{"id":"92607ca65b38ebbd","type":"inject","z":"c6eea34d387d13e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":120,"wires":[["f12cdf4c3c57a4c8"]]},{"id":"bffbc608a513909c","type":"api-current-state","z":"c6eea34d387d13e4","name":"Total cleaned area","server":"8a644e13.f9644","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_total_clean_area","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1210,"y":180,"wires":[["5c43ee8169094f0b"]]},{"id":"5c43ee8169094f0b","type":"change","z":"c6eea34d387d13e4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Area_After_Clean","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1420,"y":180,"wires":[["14012b24ac7d9bbc"]]},{"id":"14012b24ac7d9bbc","type":"switch","z":"c6eea34d387d13e4","name":"","property":"Area_After_Clean - Area_Before_Clean","propertyType":"jsonata","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1590,"y":180,"wires":[["30a355bcf03586cd"]]},{"id":"30a355bcf03586cd","type":"debug","z":"c6eea34d387d13e4","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1740,"y":180,"wires":[]}]

```

Or you will have to join the two message streams as your two values are in different message streams. [Create a single message from separate streams of messages : Node-RED](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

**Author:** ![MonolithNZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monolithnz/32/77080_2.png) [@MonolithNZ](https://discourse.nodered.org/u/MonolithNZ)\
**Post date:** [3 March 2023 23:29 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/8 "2023-03-03T23:29:42Z")

</div>

Thanks again. I'm now using a join node, which outputs the following:

{"Area\_Before\_Clean":"6338.13","Area\_After\_Clean":"6338.13"}

It's then set to a switch node, which I've configured for testing purposes as follows:  
 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/297bf4dfa97fc0777b055be0c8ab20af1b4c2cf3.png)

I'm not receiving an output on the switch node though

---

<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:** [3 March 2023 23:44 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/9 "2023-03-03T23:44:51Z")

</div>

The expession should be  
`payload.Area_After_Clean - payload.Area_Before_Clean`  
If you have joined the payloads.

---

<div class="post-metadata">

**Author:** ![MonolithNZ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monolithnz/32/77080_2.png) [@MonolithNZ](https://discourse.nodered.org/u/MonolithNZ)\
**Post date:** [3 March 2023 23:58 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/10 "2023-03-03T23:58:04Z")

</div>

Thank you very much! All working now 😀

---

<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:** [17 March 2023 23:58 UTC](https://discourse.nodered.org/t/comparing-two-variables-to-see-if-one-is-at-least-50-higher-than-the-other/76083/11 "2023-03-17T23:58:56Z")

</div>

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