Node RED function node with two inputs calculate subtraction

Hi,

I have a flow with two inputs. One counter and one sensor input. I want to subtract the sensor value from the counter and write the result into the counter. I don’t get the function to work. Can someone help me?

Thanks.

[{"id":"b0f1329d53fd4f4c","type":"api-current-state","z":"292bbd722256ac0f","name":"Fläche bis Mopp waschen","server":"d6e1a14d.333e1","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"counter.staubsauger_restflache_bis_mopp_waschen","state_type":"num","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":520,"y":100,"wires":[["87f1300c41162117"]]},{"id":"c142f475c883f893","type":"api-current-state","z":"292bbd722256ac0f","name":"Fläche der letzten Reinigung","server":"d6e1a14d.333e1","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_letzte_reinigung_flache","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","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":520,"y":160,"wires":[["87f1300c41162117"]]},{"id":"87f1300c41162117","type":"function","z":"292bbd722256ac0f","name":"function 2","func":"","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":120,"wires":[["fd1c1b3b4fde4276"]]},{"id":"2d5b99a243d8c4d7","type":"inject","z":"292bbd722256ac0f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":120,"wires":[["b0f1329d53fd4f4c","c142f475c883f893"]]},{"id":"fd1c1b3b4fde4276","type":"api-call-service","z":"292bbd722256ac0f","name":"Berechneter Wert in Zähler eintragen","server":"d6e1a14d.333e1","version":5,"debugenabled":false,"domain":"counter","service":"set_value","areaId":[],"deviceId":[],"entityId":[],"data":"{\"value\":\"msg.payload\"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":120,"wires":[[]]},{"id":"d6e1a14d.333e1","type":"server","name":"Home Assistant","addon":true}]

There is a few issues here.
Both of your current state nodes sends the values to msg.payload.

Because you fork the automation you create two paths, two messages that does not know of each other.

What I suggest is to place the two current state nodes after each other like this:

And then change the message parameters like this:

And the same with the other current state node.
The function node then needs to be something like:

msg.payload = msg.payload_Reinigung-msg.payload_mopp
1 Like

Awesome! Thank you! Didn’t expect it to be this easy. Works great :blush::ok_hand:

You can also bring other entity’s from home assistant into the current state using Jsonata $entities() You would put the sensor you want to compare against the sensor set in the current state

You could use

$round($number($entity().state) - $number($entities('sensor.flue_temp_2').state),2)

in the output property and just use one current state node.

[{"id":"fae1eb5e8d2a1c59","type":"debug","z":"60f2d2277843c698","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":760,"y":620,"wires":[]},{"id":"5a91e446120949de","type":"api-current-state","z":"60f2d2277843c698","name":"","server":"6b1110b5.183a4","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"sensor.fire_box_2","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$round($number($entity().state) - $number($entities('sensor.flue_temp_2').state),2)","valueType":"jsonata"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":620,"wires":[["fae1eb5e8d2a1c59"]]},{"id":"da55aaf4e328efca","type":"inject","z":"60f2d2277843c698","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":620,"wires":[["5a91e446120949de"]]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

1 Like

I still have one problem. If my function outputs a negative value, the action node does not write the value into the counter (the counter minimum is set to -500).

HomeAssistantError: value must be at least 0 for dictionary value @ data[‘value’]

Is there any workaround to solve this problem?

[{"id":"bcca6872ed300e86","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"ba69fc9108e3f5a6","type":"api-current-state","z":"bcca6872ed300e86","name":"Fläche bis Mopp waschen","server":"d6e1a14d.333e1","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"counter.staubsauger_restflache_bis_mopp_waschen","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload_mopp_waschen","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data_mopp_waschen","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic_mopp_waschen","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":680,"y":220,"wires":[["b7c5207f18aff70c"]]},{"id":"9743ae8f6cf84dcf","type":"api-current-state","z":"bcca6872ed300e86","name":"Fläche der letzten Reinigung","server":"d6e1a14d.333e1","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.roborock_s6_maxv_letzte_reinigung_flache","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload_flache","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data_flache","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic_flache","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":400,"y":220,"wires":[["ba69fc9108e3f5a6"]]},{"id":"b7c5207f18aff70c","type":"function","z":"bcca6872ed300e86","name":"function 5","func":"msg.payload = msg.payload_mopp_waschen - msg.payload_flache;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":220,"wires":[["4924d33235c949a3","0e8eaf3545ea340e"]]},{"id":"4924d33235c949a3","type":"api-call-service","z":"bcca6872ed300e86","name":"Berechneter Wert in Zähler eintragen","server":"d6e1a14d.333e1","version":5,"debugenabled":false,"domain":"counter","service":"set_value","areaId":[],"deviceId":[],"entityId":["counter.staubsauger_restflache_bis_mopp_waschen"],"data":"{\"value\":msg.payload}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1150,"y":220,"wires":[[]]},{"id":"0e8eaf3545ea340e","type":"debug","z":"bcca6872ed300e86","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":160,"wires":[]},{"id":"80af628ff6c7b9a7","type":"inject","z":"bcca6872ed300e86","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":220,"wires":[["9743ae8f6cf84dcf"]]},{"id":"d6e1a14d.333e1","type":"server","name":"Home Assistant","addon":true}]

Change the counter limits or have it output to an input text or a sensor

1 Like