# Setting message payload based on condition

**URL:** <https://discourse.nodered.org/t/setting-message-payload-based-on-condition/43049>\
**Category:** General\
**Created:** [22 March 2021 21:52 UTC](https://discourse.nodered.org/t/setting-message-payload-based-on-condition/43049 "2021-03-22T21:52:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajocius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ajocius/32/19024_2.png) [@ajocius](https://discourse.nodered.org/u/ajocius)\
**Post date:** [22 March 2021 21:52 UTC](https://discourse.nodered.org/t/setting-message-payload-based-on-condition/43049/1 "2021-03-22T21:52:08Z")

</div>

I am measuring temp diff and saving it to flow context value 'Temp-Diff'. I then would like to use that context value to turn sonoff "on" or "off" by setting message payload accordingly and at a next step sending that message via mqtt. It should turn sonoff 'on' if temperature difference (TempD) is higher than 8. Here is the code that I use within function node :

var TempD = flow.get("Temp-Diff") || 0;  
if (TempD \> 8){msg.payload='on';}  
if (TempD \<= 8){msg.payload='off';}  
return msg;

I have then started inject note with timestamp every 1 minute. Thought temp diff would be checked every minute and depending on value would either turn sonoff 'on' or 'off'. Howerver, I can see that sonoff is not reacticting to tempD value and simply turns on and off every minute

---

<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:** [22 March 2021 21:55 UTC](https://discourse.nodered.org/t/setting-message-payload-based-on-condition/43049/2 "2021-03-22T21:55:49Z")

</div>

Please stick to one thread - posting the same question in two places causes confusion.

Link to other thread [here](https://discourse.nodered.org/t/diff-between-values/43035/5)

---

<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:** [22 March 2021 21:56 UTC](https://discourse.nodered.org/t/setting-message-payload-based-on-condition/43049/3 "2021-03-22T21:56:10Z")

</div>


