# I need help for subtraction 2numbers output result

**URL:** <https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 April 2022 12:23 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061 "2022-04-10T12:23:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![etmomoabc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/etmomoabc/32/53077_2.png) [@etmomoabc](https://discourse.nodered.org/u/etmomoabc)\
**Post date:** [10 April 2022 12:23 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/1 "2022-04-10T12:23:55Z")

</div>

```auto
[{"id":"6b3775681e84575b","type":"server-state-changed","z":"4d1e24cc7a676365","name":"curren temp","server":"771a2737.fb6358","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":"温度"},{"property":"icon","value":"temperature"}],"entityidfilter":"sensor.zhi_neng_guang_gan_wen_du_ji_zigbeeban_temperature","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"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":90,"y":820,"wires":[["223d2af65e5291bb"]]},{"id":"c4d78c8452cd5157","type":"server-state-changed","z":"4d1e24cc7a676365","name":"target temp","server":"771a2737.fb6358","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.room1_temp","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"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":90,"y":760,"wires":[["223d2af65e5291bb"]]},{"id":"1797480146a4648f","type":"debug","z":"4d1e24cc7a676365","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":800,"wires":[]},{"id":"223d2af65e5291bb","type":"join","z":"4d1e24cc7a676365","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":800,"wires":[["42175ef90592bb81","1797480146a4648f"]]},{"id":"42175ef90592bb81","type":"function","z":"4d1e24cc7a676365","name":"Subtraction","func":"msg.payload = sensor.zhi_neng_guang_gan_wen_du_ji_zigbeeban_temperature - input_number.room1_temp;\nmsg.topic = \"Number\";\nreturn msg;\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":740,"wires":[["1797480146a4648f"],[]]},{"id":"771a2737.fb6358","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

```

I want to make an automatic air conditioner, but the difference is that when the temperature is high, I want to control the increase in air volume instead of directly adjusting the temperature of the air conditioner.  
I used hass to make an auxiliary that can be of type number as the target temperature

The temperature sensor is used as the current temperature! How to subtract them to get the difference.  
Both payloads are numbers. I use join to put them together and then...

Here are the two payloads and topic

```auto
{"sensor.zhi_neng_guang_gan_wen_du_ji_zigbeeban_temperature":"28.5","input_number.room1_temp":"23.0"}

```

---

<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:** [10 April 2022 12:31 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/2 "2022-04-10T12:31:49Z")

</div>

your payloads are strings, you would need to convert to numbers.  
e.g

```auto
msg.payload = Number(sensor.zhi_neng_guang_gan_wen_du_ji_zigbeeban_temperature) - Number(input_number.room1_temp);
msg.topic = "Number";
return msg;

```

Strings are shown in debug with quotes around and red, numbers are blue

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [10 April 2022 12:32 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/3 "2022-04-10T12:32:06Z")

</div>

Its a little off-topic, but here is an Idea.

Use the range node?  
transpose from 1 range to another.

This will allow a linear air volume output based on temp.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c77df9803baded437b39c94867c45d1fabbe6dc.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/089e88d128c2436ae076d863836467f593166642.png)

but as @E1cid mentioned - ensure they are numeric values

---

<div class="post-metadata">

**Author:** ![etmomoabc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/etmomoabc/32/53077_2.png) [@etmomoabc](https://discourse.nodered.org/u/etmomoabc)\
**Post date:** [10 April 2022 12:47 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/4 "2022-04-10T12:47:59Z")

</div>

"ReferenceError: sensor is not defined (line 1, col 36)"

I made changes. It still doesn't work, I get this

---

<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:** [10 April 2022 12:53 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/5 "2022-04-10T12:53:12Z")

</div>

```auto
msg.payload = Number(msg.payload["sensor.zhi_neng_guang_gan_wen_du_ji_zigbeeban_temperature"]) - Number(msg.payload["input_number.room1_temp"]);
msg.topic = "Number";
return msg;

```

---

<div class="post-metadata">

**Author:** ![etmomoabc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/etmomoabc/32/53077_2.png) [@etmomoabc](https://discourse.nodered.org/u/etmomoabc)\
**Post date:** [10 April 2022 12:58 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/6 "2022-04-10T12:58:01Z")

</div>

> [@E1cid](#):
>
> ```auto
> 
> ```

MAGIC~! it worked...

thanks you so much!

---

<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:** [24 April 2022 12:58 UTC](https://discourse.nodered.org/t/i-need-help-for-subtraction-2numbers-output-result/61061/7 "2022-04-24T12:58:44Z")

</div>

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