# How to control a "switch" button by an input to it...?

**URL:** <https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119>\
**Category:** General\
**Created:** [31 March 2023 16:21 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119 "2023-03-31T16:21:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [31 March 2023 16:21 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119/1 "2023-03-31T16:21:04Z")

</div>

High,  
I have a switch button which controls a pump by sending an ON String or an OFF string. That works fine if I switch the button from ON to OFF by means of the Node-red panel.  
Now I want to control this switch button by means of a Node-Red input message ON or OFF, and the switch should show its new state - either ON or OFF - on my Node-Red display.  
What I have to do to obtain this behaviour?

Thanks,  
Alf Italy

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [31 March 2023 16:52 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119/2 "2023-03-31T16:52:43Z")

</div>

You can send a msg.payload in to the Switch node to force it to a particular state.  
The type of msg.payload needs to match the On/Off Payload type.

![switch_payload](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/bed6296af5d555d7d20d3b945d3850bd5d9f0d5a.png)

Switch settings - note the type is Boolean  
 ![switch_send](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03b4a6884ecd228deba93dbf014c48634a83492c.png)

This is what is in the Inject node - note the type is Boolean  
 ![switch_payload_B](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42ab1717f8d6567496e551fc1f24c886def24f01.png)

There is useful 'Help' information in the right-hand window pane.

![switch_help](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52da96024624f249bafc7578b12d8fde5bee55d6.png)

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [31 March 2023 17:24 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119/3 "2023-03-31T17:24:40Z")

</div>

High,  
thanks for your prompt answer.  
Since my payload messages are strings "PN" for pump on and "PF" for pump off. my MQTT input node should send "PN" and "PF"?  
thanks, Alfitaly

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [31 March 2023 17:42 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119/4 "2023-03-31T17:42:05Z")

</div>

high dynamicdave,  
thanks again, I did as you described and it works, Thank you!!  
Alfitaly

---

<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 May 2023 17:42 UTC](https://discourse.nodered.org/t/how-to-control-a-switch-button-by-an-input-to-it/77119/5 "2023-05-30T17:42:43Z")

</div>

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