# Increment number with true and false

**URL:** <https://discourse.nodered.org/t/increment-number-with-true-and-false/42106>\
**Category:** General\
**Created:** [5 March 2021 17:45 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106 "2021-03-05T17:45:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [5 March 2021 17:45 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/1 "2021-03-05T17:45:05Z")

</div>

Looking for some help on writing a function node.

i have a variable hbtset which i need to increase or decrease from either buttons on a dash board or in the flow elswhere. on pressing the button on the dash i want to send true or false to incrment or decrement the value by one, something like below, any hel please appreciated

// get values  
var hbtset = flow.get("hbtset");

// MAIN  
if (msg.topic = hbtset && msg.payload = true)  
var hbtsetnew = hbtset+1;  
var hbtset = hbtsetnew  
return msg;

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [5 March 2021 18:24 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/2 "2021-03-05T18:24:28Z")

</div>

Seems to me that you could make this a generic increment/decrement function, using the `msg.topic` as the lookup key in the flow context. This would save you from having to copy the node for use with another topic string. Something like this (untested):

```auto
// get current numeric value from the flow context
var topic = msg.topic || "missing"; // catch-all for missing topics
var value = flow.get(topic) || 0; // if not yet defined, assume 0

// increment or decrement the value, based on a boolean payload
var delta = (msg.payload === true) ? 1 : -1;
value = value + delta;

// save the new value to the flow context, and the outgoing msg payload
flow.set(topic, value);
msg.payload = value;
return msg;

```

Of course, this will create new flow variables for every new topic that arrives (which may not be what you want). In that case, you could just test the topic at the beginning and return the original msg if it's not a topic that you would expect. Hope this gives you some ideas...

---

<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:** [5 March 2021 18:59 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/3 "2021-03-05T18:59:15Z")

</div>

Not sure what msg.topic is, as htbset is a count.  
something like below should work to increment/deincrement the count

```auto
if (typeof(msg.payload)==="boolean"){ // payload has to be true or false
    var hbtset = flow.get("hbtset") || 0; //fetch count or default 0
    if (msg.payload === true) {
        hbtset++; // if true plus 1
    }else{
        hbtset--; //if false minus 1
    }
flow.set("hbtset", hbtset) // save count
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [5 March 2021 19:04 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/4 "2021-03-05T19:04:17Z")

</div>

> [@E1cid](#):
>
> `var hbtset = flow.get("hbtset") || 0; //fetch count or default 0`

Is that correct? if the flow context is Boolean false. won't it pick up the 0 instead.  
Using comparisons with boolean values always gives me an headache 😬 😬

---

<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:** [5 March 2021 19:06 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/5 "2021-03-05T19:06:07Z")

</div>

> [@davenrea](#):
>
> i have a variable hbtset which i need to increase or decrease fr

I read it as the var hbtset is a count not a boolean, I may have misunderstood.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [5 March 2021 19:11 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/6 "2021-03-05T19:11:09Z")

</div>

> [@E1cid](#):
>
> I read it as the var hbtset is a count not a boolean

Re reading the above... it's probably me that misunderstood the OP.

---

<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:** [5 March 2021 19:15 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/7 "2021-03-05T19:15:18Z")

</div>

Maybe, time will tell.

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [5 March 2021 19:37 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/8 "2021-03-05T19:37:29Z")

</div>

@E1cid @shrickus @Paul-Reed Guys, thanks so much for stepping in I will start looking if I can get it working. hbtset is the value I am trying to change (it's the temperature I set the heating to come on and heat a thermal store) I have 2 pi's one running the control panel and the other the heating and either pi can change the value by sending a message. I also am using hbtset as the topic as well.  
Thanks

---

<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:** [5 March 2021 19:41 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/9 "2021-03-05T19:41:02Z")

</div>

> [@davenrea](#):
>
> I also am using hbtset as the topic as well.

Well that's very confusing. are you saying msg.topic has the value "hbtset" a string?

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [5 March 2021 19:45 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/10 "2021-03-05T19:45:22Z")

</div>

yes, that is correct, I can change it easily

---

<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:** [5 March 2021 20:01 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/11 "2021-03-05T20:01:08Z")

</div>

```auto
if (typeof(msg.payload)==="boolean" && msg.topic === "hbtset"){ 

```

just change first line then.  
[edit] You can all so change the `flow.get("hbtset") || 0` to make it a default temp, if flow context is undefined for any reason. i.e. `flow.get("hbtset") || 20`

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [5 March 2021 20:54 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/12 "2021-03-05T20:54:47Z")

</div>

nearly there, its not shouting at me but when I look at the output of the function using ui-text on the dashboard I don't get a payload with the hbtset value just the Boolean value

---

<div class="post-metadata">

**Author:** ![davenrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/davenrea/32/32842_2.png) [@davenrea](https://discourse.nodered.org/u/davenrea)\
**Post date:** [5 March 2021 21:05 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/13 "2021-03-05T21:05:15Z")

</div>

worked it out!, used change read flow and set to payload, thanks guys for all your help

---

<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:** [4 May 2021 21:05 UTC](https://discourse.nodered.org/t/increment-number-with-true-and-false/42106/14 "2021-05-04T21:05:26Z")

</div>

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