Function to operate a switch node using a button

2,211 views
Skip to first unread message

Trinesh Palakurthy

unread,
Nov 27, 2016, 1:28:31 AM11/27/16
to Node-RED
I have written a Js function to operate a switch using a button by using a flag with context variable.By default it will on and off for every click. What I want now is based on the switch current state it should negate the switch.
Please let me know if u didn't understand the question

Ben Hardill

unread,
Nov 27, 2016, 4:32:45 AM11/27/16
to Node-RED
When you asked this on Stack Overflow I asked you to include the code, you need to do that here as well.

Trinesh Palakurthy

unread,
Nov 28, 2016, 11:20:52 AM11/28/16
to Node-RED


On Sunday, 27 November 2016 11:58:31 UTC+5:30, Trinesh Palakurthy wrote:
I have written a Js function to operate a switch using a button by using a flag with context variable.By default it will on and off for every click. What I want now is based on the switch current state it should negate the switch.
Please let me know if u didn't understand the question

My code is:
context.global.value = context.global.value || false;
if (msg.payload === 1){
    if (context.global.value === true){
        context.global.value = false;
        msg.payload = 0;
        return msg;
    }else if (context.global.value === false){
        context.global.value = true;
        msg.payload = 1;
        return msg;
   }

Walter Kraembring

unread,
Nov 29, 2016, 1:36:36 AM11/29/16
to Node-RED

context.set('value', context.get('value')||false);    
var value = context.get('value');

switch (value) {
    case true:
        context.set('value', false);
        msg.payload = 0;
        break;
    case false:
        context.set('value', true);
        msg.payload = 1;
        break;
    default:
        context.set('value', false);
        msg.payload = 0;
        break;
}

return msg;



Trinesh Palakurthy

unread,
Nov 29, 2016, 10:11:17 AM11/29/16
to Node-RED
Still the output is same as my code

Walter Kraembring

unread,
Nov 29, 2016, 11:04:00 AM11/29/16
to Node-RED
In that case I must admitt I do not understand your requirement. I think you have to describe it better,maybe in steps

Ben Hardill

unread,
Nov 29, 2016, 4:28:33 PM11/29/16
to Node-RED
The problem will be that the gpio node will be sending 2 messages for every button push,

1) for the state change when it goes from low to high (when the button is pressed)
2) for the state change whne it goes from hight to low (when the button is release)

You need to add an extra if statement to only check for the first of these messages which will have a msg.payload of 1

Trinesh Palakurthy

unread,
Nov 29, 2016, 10:12:03 PM11/29/16
to Node-RED
The state of the switch should change based on the current state.
The switch will be operated both manually and using the button.
Current code drawbacks:
When the state of the switch is changed manually by clicking on the switch, in order to change the state again the button need to be pressed 2 times since the flag inside the function do not know the state change.

Requirement:
Though the state of the switch is changed manually the function should capture that and act accordingly.


On Tuesday, 29 November 2016 21:34:00 UTC+5:30, Walter Kraembring wrote:

Neil Cherry

unread,
Nov 29, 2016, 10:25:07 PM11/29/16
to Node-RED
On Tuesday, November 29, 2016 at 10:12:03 PM UTC-5, Trinesh Palakurthy wrote:
The state of the switch should change based on the current state.
The switch will be operated both manually and using the button.
Current code drawbacks:
When the state of the switch is changed manually by clicking on the switch, in order to change the state again the button need to be pressed 2 times since the flag inside the function do not know the state change.

Requirement:
Though the state of the switch is changed manually the function should capture that and act accordingly.

Trinesh, I hope this is not homework. Ben gave you the solution, Walter gave you the code (missing Ben's added comments). I've tested the solution and I can inject a 1, 0 or null and it behaves correctly (toggle state, nothing and nothing respectively). You'll need to clean up the message a bit bit I was pumping into a debug output node.

if(msg.payload == 1) {
   
// Don't make change the ending false to a true as that will
   
// always set value true ( false||true == true )

    context
.set('value', context.get('value')||false);    
   
var value = context.get('value');


   
// Toggle values true or false

   
switch (value) {
       
case true:
            context
.set('value', false);

             msg
.payload = { 'value': false, 'origValue': value };

           
break;
       
case false:
            context
.set('value', true);

            msg
.payload = { 'value': true, 'origValue': value };

           
break;
       
default:
            context
.set('value', false);

            msg
.payload = { 'value': flase, 'origValue': value };
           
break;

   
}
   
return msg;
}
// if we get some other value then don't do anything

 

Trinesh Palakurthy

unread,
Nov 30, 2016, 12:06:05 PM11/30/16
to Node-RED
I am sorry to tell that this is also not working.I will be glad if someone modify my code and give the required code. I hope 1 or 2 steps modification gives the solution.

Colin Law

unread,
Nov 30, 2016, 12:18:57 PM11/30/16
to node...@googlegroups.com
You need to post the whole relevant parts of the flow so we can
understand what you are doing. You can select the nodes and export to
the clipboard then paste it here.

Colin

On 30 November 2016 at 17:06, Trinesh Palakurthy
<trineshp...@gmail.com> wrote:
> I am sorry to tell that this is also not working.I will be glad if someone modify my code and give the required code. I hope 1 or 2 steps modification gives the solution.
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
> To post to this group, send an email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/9bd4fd46-fce7-429f-8652-fa0defe4831d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Trinesh Palakurthy

unread,
Nov 30, 2016, 10:17:51 PM11/30/16
to Node-RED
Flow.jpg

Walter Kraembring

unread,
Dec 1, 2016, 1:40:04 AM12/1/16
to Node-RED
[{"id":"b93abb95.263c28","type":"function","z":"54834ff4.f3537","name":"","func":"context.set('value', context.get('value')||false);    \nvar value = context.get('value');\n\nswitch (value) {\n    case true:\n        context.set('value', false);\n        msg.payload = 'Off';\n        break;\n    case false:\n        context.set('value', true);\n        msg.payload = 'On';\n        break;\n    default:\n        context.set('value', false);\n        msg.payload = 'Off';\n        break;\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":430,"y":200,"wires":[["915fc979.bc5cf8"]]},{"id":"927a816b.0d6e3","type":"debug","z":"54834ff4.f3537","name":"","active":true,"console":"false","complete":"true","x":850,"y":180,"wires":[]},{"id":"1671a01.7b0756","type":"ui_button","z":"54834ff4.f3537","name":"","group":"5a57ad77.2449f4","order":0,"width":0,"height":0,"label":"button","color":"","icon":"","payload":"run","payloadType":"str","topic":"","x":240,"y":220,"wires":[["b93abb95.263c28"]]},{"id":"915fc979.bc5cf8","type":"ui_switch","z":"54834ff4.f3537","group":"5a57ad77.2449f4","order":0,"width":0,"height":0,"name":"","label":"switch","topic":"","style":"","onvalue":"On","onvalueType":"str","onicon":"","oncolor":"","offvalue":"Off","offvalueType":"str","officon":"","offcolor":"","x":640,"y":240,"wires":[["927a816b.0d6e3"]]},{"id":"5a57ad77.2449f4","type":"ui_group","z":"","name":"Log & Charts","tab":"8131d51b.5ab418","order":2,"disp":true,"width":"20"},{"id":"8131d51b.5ab418","type":"ui_tab","z":"","name":"Log","icon":"dashboard","order":2}]


Reply all
Reply to author
Forward
0 new messages