Increment number with true and false

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;

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):

// 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...

1 Like

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

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;
1 Like

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 :grimacing: :grimacing:

1 Like

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

1 Like

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

1 Like

Maybe, time will tell.

2 Likes

@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

1 Like

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

1 Like

yes, that is correct, I can change it easily

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

1 Like

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

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

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