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
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;
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.
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
[{"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}]