# Switch only on true for a change-over of the output

**URL:** https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021
**Category:** General
**Created:** [14 October 2022 09:07 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021 "2022-10-14T09:07:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Sweetpeet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sweetpeet/32/81246_2.png) [@Sweetpeet](https://discourse.nodered.org/u/Sweetpeet)
#### Post date: [14 October 2022 09:07 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/1 "2022-10-14T09:07:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [14 October 2022 09:33 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/2 "2022-10-14T09:33:14Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![Sweetpeet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sweetpeet/32/81246_2.png) [@Sweetpeet](https://discourse.nodered.org/u/Sweetpeet)
#### Post date: [14 October 2022 10:06 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/3 "2022-10-14T10:06:18Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [14 October 2022 10:12 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/4 "2022-10-14T10:12:54Z")

</div>

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 by `0`? does the GPIO send a `0` followed by `1`? )
- is the action of the switch a toggle? (i.e. does first press send a `1` and second press send a `0`?)
- What do you want so be the result of the flow - send strings like "ON" and "OFF"? send `true` / `false`? Send `1` / `0` ?

In other words, please be specific.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [14 October 2022 13:21 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/5 "2022-10-14T13:21:30Z")

</div>

This should work, each press toggles output 1 then 0 then 1 then 0

```auto
[{"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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [14 October 2022 19:39 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/6 "2022-10-14T19:39:31Z")

</div>

I'm not sure what you mean by your reply.

But how the output works:  
(From the code I posted)

```auto
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.

---

<div class="post-metadata">

### Author: ![Sweetpeet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sweetpeet/32/81246_2.png) [@Sweetpeet](https://discourse.nodered.org/u/Sweetpeet)
#### Post date: [16 October 2022 09:25 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/7 "2022-10-16T09:25:51Z")

</div>

> [@E1cid](#):
>
> `[{"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":[]}]`

Yes, thanks that works!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [30 October 2022 09:26 UTC](https://discourse.nodered.org/t/switch-only-on-true-for-a-change-over-of-the-output/69021/8 "2022-10-30T09:26:32Z")

</div>

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