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

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 :face_with_diagonal_mouth:)

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

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:

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

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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

Thank you!

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 )

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

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

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

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

Thank you very much! All working now :grinning:

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