Hi There, I'm quite new to NR and I'm trying to use a pulse switch on the GPIO board to output a JSON dimmer value to my dimmers. What I want is: On the first push of the switch the output is 1 and on the second push of the switch the output is zero. I sound simple but I can't make it work.
Hi and welcome to the forum.
There is a toggle
switch node that would get around this problem.
But I'm guessing there will be more than 1 GUI button to control things.
The pulse switch
. As is button
?
Going to the next step you will need to use the thing called context
.
This will suffice but also has a bit of extra code in it also.
It goes in a function
node.
var x = context.get("counter") || 0;
if (msg.payload == 0)
{
context.set("counter",0);
msg.payload = 0;
return msg;
}
if (msg.payload == 1)
{
context.set("counter",1);
msg.payload = 1;
return msg;
}
if (x === 0)
{
msg.payload = 1;
} else
if (x === 1)
{
msg.payload = 0;
}
x = (x + 1) % 2;
context.set("counter",x);
return msg;
The extra part is that normally you send something as the msg.payload
to toggle the output.
But if you send a 0
or 1
it forces the output to be either OFF or ON.
So as long as you don't send a msg.payload
of either 0
or 1
it should work fine.
Oh, you will/may have to edit/change what is sent for the ON
and OFF
conditions.
You haven't said what you send to the GPIO pin.
Thanks for your reply and I think I almost there but whenI use the toggle it goes ON and OFF. The functions output is then 1 or 0 and it doesn't only change when the toggle goes to ON. Do I miss something?
Can you state what the input value is from your GPIO and what output you expect?
e.g.
- is the action of the switch momentary (i.e. does the GPIO send a
1
followed by0
? does the GPIO send a0
followed by1
? ) - is the action of the switch a toggle? (i.e. does first press send a
1
and second press send a0
?) - What do you want so be the result of the flow - send strings like "ON" and "OFF"? send
true
/false
? Send1
/0
?
In other words, please be specific.
This should work, each press toggles output 1 then 0 then 1 then 0
[{"id":"8a9d0c48438c6c7d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":250,"y":980,"wires":[["c0a46a18812e14e5"]]},{"id":"c0a46a18812e14e5","type":"change","z":"366a43adb328cf95","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"switch_toggle","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"$$.payload = 1 ? 0 : 1","tot":"jsonata"},{"t":"set","p":"switch_toggle","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":980,"wires":[["afed0c6bfa9fbd91"]]},{"id":"afed0c6bfa9fbd91","type":"debug","z":"366a43adb328cf95","name":"debug 93","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":980,"wires":[]}]
[edit] misread, edited for use with a input of a gpio.
I'm not sure what you mean by your reply.
But how the output works:
(From the code I posted)
if (x === 0)
{
msg.payload = 1; // This is the payload for ON
} else
if (x === 1)
{
msg.payload = 0; // This is the payload for OFF
}
So you edit those two lines to be what you want to send to the GPIO pin.
Also: To clarify.
You only send something like X
into the function
node (well: that's what I use) and the ouput sends alternate 0
and 1
each time you send X
into the node.
IF you want to force the output, you send a 0
or 1
to force it OFF or ON respectively.
Yes, thanks that works!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.