Convert one value into two different ones

Hello guys,

I have a switch module from Hue. The module only ever gives me one value when I press it: "short_release". I now have to turn this value alternately into a 1 and a 0. How can I do this? Do i have to save the first state in node context or something like that?

You can send the value into a trigger node to output 1 then after a delay output 0.

I was thinking about it, but i need sometimes double click, so there is not much time for reset threw trigger.

It really helps if you give all the info. Please post all info and maybe a example flow with all possible inputs in separate inject nodes, then we can offer some suggestions.
example distinguish taps

[{"id":"eb984ad239fa4efc","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":4940,"wires":[["63a3efd6b1ea20df"]]},{"id":"63a3efd6b1ea20df","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"0.5","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":4940,"wires":[["3dbc77afa0116aa6"]]},{"id":"3dbc77afa0116aa6","type":"switch","z":"d1395164b4eec73e","name":"","property":"$count($$.payload)","propertyType":"jsonata","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":710,"y":4940,"wires":[["d4d7b1ad3676c75b"],["bb1fdd45cb06e61f"],[]]},{"id":"d4d7b1ad3676c75b","type":"debug","z":"d1395164b4eec73e","name":"single tap","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":4920,"wires":[]},{"id":"bb1fdd45cb06e61f","type":"debug","z":"d1395164b4eec73e","name":"double tap","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":4960,"wires":[]}]

I really dont know how to explain better:

1st message is I toggle the switch on and second is i toggle switch to off. I need this node to do so. Like i need some function i would write in javascript like this:

let status = false;
if (msg.payload.action=="short_release" && status == false){
    status = true;
    return msg.payload = 1;

}else if (msg.payload.action=="short_release" && status == true){
return msg.payload = 0;
}


return msg;

but i dont know how to store this variable "status" int othis function node, until msg.payload triggers it again. Thats my problem actually.

try

let status = context.get("status") || false;
if (msg.payload.action=="short_release" && status == false){
    status = true;
    msg.payload = 1;
}else{
    msg.payload = 0;
    status = false;
}
context.set("status", status);
return msg;

great! it works as expected. Thank you!

Reading between the lines, if you want to handle different incoming button presses messages, then you would do something like this

let status = context.get("status") || {};
let presses = msg.payload.action
if (status[presses] == false){
    status[presses] = true;
    msg.payload.output = 1;
}else{
    msg.payload.output = 0;
    status[presses] = false;
}
context.set("status", status);
return msg;

example flow

[{"id":"b596ca3877dfdba2","type":"function","z":"d1395164b4eec73e","name":"function 154","func":"let status = context.get(\"status\") || {};\nlet presses = msg.payload.action\nif (status[presses] == false){\n    status[presses] = true;\n    msg.payload.output = 1;\n}else{\n    msg.payload.output = 0;\n    status[presses] = false;\n}\ncontext.set(\"status\", status);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":592.0000076293945,"y":5003.0006465911865,"wires":[["0c0506aaee8ed0f5"]]},{"id":"eb984ad239fa4efc","type":"inject","z":"d1395164b4eec73e","name":"short_press","props":[{"p":"payload.action","v":"short_release","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":432.00000762939453,"y":4963.0006465911865,"wires":[["b596ca3877dfdba2"]]},{"id":"6d5f85063334dab5","type":"inject","z":"d1395164b4eec73e","name":"double_press","props":[{"p":"payload.action","v":"double_press","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":432.00000762939453,"y":5023.0006465911865,"wires":[["b596ca3877dfdba2"]]},{"id":"0c0506aaee8ed0f5","type":"debug","z":"d1395164b4eec73e","name":"debug 2471","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":772.0000076293945,"y":5043.0006465911865,"wires":[]}]

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