# Function with payload + X

**URL:** <https://discourse.nodered.org/t/function-with-payload-x/66796>\
**Category:** General\
**Created:** [23 August 2022 06:47 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796 "2022-08-23T06:47:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 06:47 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/1 "2022-08-23T06:47:52Z")

</div>

Hello  
I'm new here and need a little help ☹

At node red, I do air conditioning automation.

I would like the air conditioning to turn on when the room temperature is higher by, for example, 2 than the currently set value.

currently I have a reading of temp = payload

If temp is\>= Payload, it will turn on the climate.

I wish it was on an enable if \>= payload "+2" degrees (or more/less).

How could I get it?

I have tried hystersis but it's not a good nod (or i cant set it)  
i try too function (but probably is a wrong)

**var payload = msg.payload; //get payload**  
**payload = msg.payload + 2.0;**  
**msg.payload = payload;**  
**return msg;**

I am counting on your help !

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 06:50 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/2 "2022-08-23T06:50:53Z")

</div>

> [@Peres](#):
>
> I have tried hystersis but it's not a good nod (or i cant set it)

Likely the latter. Search the forum for [hysteresis](https://discourse.nodered.org/search?q=%60hysteresis%60) - @colin has posted many threads and helped tonnes of users with this topic.

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 06:53 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/3 "2022-08-23T06:53:51Z")

</div>

i think the function is better for me. hystersis is more advanced. i need a really simple script.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:08 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/4 "2022-08-23T07:08:23Z")

</div>

> [@Peres](#):
>
> i think the function is better for me

I understand but for temperature control, hysteresis is **definitely** the way to go.

In the mean time, something to get you moving...

![chrome_V1NCTVW9x0](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74bd1f1f9da371832ca5b05c093e9c3fbf50371d.gif)

```auto
[{"id":"4b0b1c007ea0da93","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"room/temperature","payload":"16","payloadType":"num","x":880,"y":1380,"wires":[["e120e68bf3917c41"]]},{"id":"41d0eef7162a2e69","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"room/temperature","payload":"19","payloadType":"num","x":880,"y":1420,"wires":[["e120e68bf3917c41"]]},{"id":"69527427be2a656b","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"room/temperature","payload":"23","payloadType":"num","x":880,"y":1460,"wires":[["e120e68bf3917c41"]]},{"id":"4f890478a14bba74","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"room/temperature","payload":"25","payloadType":"num","x":880,"y":1500,"wires":[["e120e68bf3917c41"]]},{"id":"710241db0e60e4e1","type":"inject","z":"6f3a24bd7ed7326b","name":"aircon settings (on at 26, off at 18)","props":[],"repeat":"","crontab":"","once":true,"onceDelay":"0.3","topic":"","x":920,"y":1280,"wires":[["7bc39a04d3a7292e"]]},{"id":"7bc39a04d3a7292e","type":"change","z":"6f3a24bd7ed7326b","name":"","rules":[{"t":"set","p":"high","pt":"flow","to":"26","tot":"num"},{"t":"set","p":"low","pt":"flow","to":"18","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":1280,"wires":[["08b36c60364ede98"]]},{"id":"a149dc02851d7925","type":"comment","z":"6f3a24bd7ed7326b","name":"set high and low values","info":"","x":860,"y":1240,"wires":[]},{"id":"e120e68bf3917c41","type":"change","z":"6f3a24bd7ed7326b","name":"store room_temp","rules":[{"t":"set","p":"room_temp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1150,"y":1440,"wires":[["d22c9643ed96bb2e"]]},{"id":"d22c9643ed96bb2e","type":"link out","z":"6f3a24bd7ed7326b","name":"link out 17","mode":"link","links":["50c3c3e310290586"],"x":1315,"y":1440,"wires":[]},{"id":"08b36c60364ede98","type":"link out","z":"6f3a24bd7ed7326b","name":"link out 18","mode":"link","links":["50c3c3e310290586"],"x":1315,"y":1280,"wires":[]},{"id":"50c3c3e310290586","type":"link in","z":"6f3a24bd7ed7326b","name":"link in 10","links":["d22c9643ed96bb2e","08b36c60364ede98"],"x":785,"y":1640,"wires":[["45469485f49c576c"]]},{"id":"45469485f49c576c","type":"switch","z":"6f3a24bd7ed7326b","name":"high? low?","property":"room_temp","propertyType":"flow","rules":[{"t":"gt","v":"high","vt":"flow"},{"t":"lt","v":"low","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":930,"y":1640,"wires":[["3bb7ffaa7a26a528"],["00ec317796212dd6"],[]]},{"id":"3bb7ffaa7a26a528","type":"change","z":"6f3a24bd7ed7326b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"AIRCON ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":1620,"wires":[["c42a5b7bcb4cf41d"]]},{"id":"c42a5b7bcb4cf41d","type":"debug","z":"6f3a24bd7ed7326b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1330,"y":1660,"wires":[]},{"id":"00ec317796212dd6","type":"change","z":"6f3a24bd7ed7326b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"AIRCON OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":1660,"wires":[["c42a5b7bcb4cf41d"]]},{"id":"6fd5ec19cb2f07f2","type":"comment","z":"6f3a24bd7ed7326b","name":"Simulate temparature changes","info":"","x":890,"y":1340,"wires":[]},{"id":"c4b2e7242e1e308a","type":"comment","z":"6f3a24bd7ed7326b","name":"Control AIRCON","info":"","x":840,"y":1600,"wires":[]},{"id":"d156998b76450d1a","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"room/temperature","payload":"27","payloadType":"num","x":880,"y":1540,"wires":[["e120e68bf3917c41"]]}]

```

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [23 August 2022 07:09 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/5 "2022-08-23T07:09:32Z")

</div>

@Steve-Mcl, what is the tool you're using to make the tiny screen captures?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:12 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/6 "2022-08-23T07:12:56Z")

</div>

I use sharex (for windows only)

There are plenty of them around & has been covered on forum a few times. Search "animated gif" or "sharex" on the forum for other ideas 🙂

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 07:19 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/7 "2022-08-23T07:19:51Z")

</div>

i think about it. but in hystersis aircontidioner will start about 26 (or other what i set).  
if i make by my idea, with payload + x then i change temp in HA nodered will get value from payload and airconditioner will start by value in payload.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:22 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/8 "2022-08-23T07:22:31Z")

</div>

that demo above was NOT using hysteresis

> [@Steve-Mcl](#):
>
> In the mean time, something to get you moving...
> 
> ![chrome_V1NCTVW9x0](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74bd1f1f9da371832ca5b05c093e9c3fbf50371d.gif)
> 
> ```auto
> [{"id":"4b0b1c007ea0da93","type":"inject","z":"6f3a24bd7ed7326b","name":"","props":[{"
> 
> ```

Using pure node-red nodes (low-code, no functions, no hysteresis, something simple to get you started on a basic control). Simply set the `high` and `low` values you want and it will work as you describe

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 07:24 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/9 "2022-08-23T07:24:57Z")

</div>

for me, the value of high and low varies depending on the choice of temperature from the drop-down list...

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 07:28 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/10 "2022-08-23T07:28:30Z")

</div>

but i try with it. maybe i will do it ;-).

if anybody have idea with function payload +X send it please.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:29 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/11 "2022-08-23T07:29:46Z")

</div>

Yes, I understand that - so link these values to the `flow.high` and `flow.low` - I only used an inject because I dont have your values, I dont have your system, I dont have access to your HS server. - I simulated it.

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

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 07:32 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/12 "2022-08-23T07:32:36Z")

</div>

thanks, i try with it for moment \<3  
first i go to eat breakfast 😃

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:33 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/13 "2022-08-23T07:33:15Z")

</div>

> [@Peres](#):
>
> if anybody have idea with function payload +X send it please.

This makes zero sense.

You have 3 values to consider.

- Current Temperature
- High point
- Low point

You need all 3 values to be able to determine what to do.

So, use `flow.xxxx` to store them.

Then use change nodes to compare them

Then you can control the AIRCON

Pretty much as I wrote for you!

If you REALY REALY want to compare to xxx + 2 - then simply add 2 to the `flow.high`

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 08:36 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/14 "2022-08-23T08:36:24Z")

</div>

hmm its really look good and work! 😃 thanks.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 August 2022 08:37 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/15 "2022-08-23T08:37:09Z")

</div>

Just to clarify the terminology here, hysteresis, in this context, is exactly what is being talked about, namely turning on the cooling a bit above a setpoint and turning it off again when it has reached a defined lower temperature. Whether it is done in a function node or in a node with hysteresis in the name is irrelevant, it is still hystersis.

```auto
var payload = msg.payload; //get payload
payload = msg.payload + 2.0;
msg.payload = payload;
return msg;

```

@Peres that should do exactly what you have asked for, adding 2 to msg,payload, though it can be simplified to

```auto
msg.payload = msg.payload + 2
return msg

```

or even further to

```auto
msg.payload += 2
return msg

```

Did it not work for you? If not then add debug nodes showing what is going into and out of the function node and show us the results.

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 08:39 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/16 "2022-08-23T08:39:55Z")

</div>

this flow what Steve send me is really ok (thanks!!). But maybe i try to use +2 in another flow (if i will need). thanks a lot \<3

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 09:30 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/17 "2022-08-23T09:30:31Z")

</div>

ok i do it to my airconditioner.  
have next problem....

if i set temp (on my selectable list) like ' set temp to 23' and i want to airconditioner is off less like 23 (22.99 or less) but start again if temp is about 24-25 (still is payload +2). and i cant to do it with this flow from Steve... if i make paylad + 2 then i have temp 23.02 (not 23+2 =25) just 23.02 ☹

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 10:08 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/18 "2022-08-23T10:08:27Z")

</div>

![airconditioner](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9ef58db7a65c11f25d7284f11a135d3d0059ad12.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 August 2022 10:37 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/19 "2022-08-23T10:37:33Z")

</div>

> [@Peres](#):
>
> if i make paylad + 2 then i have temp 23.02 (not 23+2 =25) just 23.02

Export your current flow so we can see what you have done.

---

<div class="post-metadata">

**Author:** ![Peres](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peres](https://discourse.nodered.org/u/Peres)\
**Post date:** [23 August 2022 10:42 UTC](https://discourse.nodered.org/t/function-with-payload-x/66796/20 "2022-08-23T10:42:29Z")

</div>

```auto
[{"id":"72fdce8e1fab005b","type":"api-current-state","z":"17369459177ad140","name":"1","server":"497764a0.a9625c","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"sensor.ustawiona_temp_klima_sypialnia","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","x":1130,"y":1780,"wires":[["e5ae592d3a93ce23"]]},{"id":"e5ae592d3a93ce23","type":"function","z":"17369459177ad140","name":"","func":"msg.payload += 2\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":1780,"wires":[["dd18a6913a195641"]]},{"id":"dd18a6913a195641","type":"debug","z":"17369459177ad140","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1470,"y":1780,"wires":[]},{"id":"16b14a5c44198005","type":"inject","z":"17369459177ad140","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":940,"y":1780,"wires":[["72fdce8e1fab005b"]]},{"id":"497764a0.a9625c","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

```

[Next page](https://discourse.nodered.org/t/function-with-payload-x/66796.md?page=2)
