# Relay switch via dashboard led

**URL:** <https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757>\
**Category:** General\
**Created:** [16 May 2020 14:25 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757 "2020-05-16T14:25:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![duffmans](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@duffmans](https://discourse.nodered.org/u/duffmans)\
**Post date:** [16 May 2020 14:25 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/1 "2020-05-16T14:25:41Z")

</div>

Hey people, I am trying to config a switch that can run an on/off python relay script and display a light on dashboard to show if on or off. I have tried several nodes but admitting defeat and need some guidance

Current flow is just 2 buttons running 2 pythonshell nodes and that has been the only way i could get the relay to switch on/off. python,pi and node red, all very new to me.

I keep reading their is an easier way to achieve this using mqtt or execute but i haven't been able to figure it out.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 May 2020 19:39 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/2 "2020-05-16T19:39:33Z")

</div>

What make/model of relay switch is it, is it a retail item or something that you have built?  
(there may be an easier way to control it).  
Oh, and welcome to the forum @duffmans!

---

<div class="post-metadata">

**Author:** ![duffmans](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@duffmans](https://discourse.nodered.org/u/duffmans)\
**Post date:** [16 May 2020 20:20 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/3 "2020-05-16T20:20:56Z")

</div>

Thanks for the welcome @Paul-Reed

The relay I bought and its a 5v 4 channel with optocoupler, I wanted the option to control some pc fans independent of each other (or all together) for a pc workbench. Im sure their are controllers out their but i had a pi that the kids showed no interest in and had plenty of time on my hands so built a little enclosure for it all so i am keen on the completing this final hurdle.

It is all built and i can remotely control 3 fans from a node red dashboard. But i can't figure out how to display the status of the pythonshell node on dashboard. I have seen and played with some other flows but couldn't implement them into my own.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 May 2020 21:17 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/4 "2020-05-16T21:17:53Z")

</div>

> [@duffmans](#):
>
> The relay I bought and its a 5v 4 channel with optocoupler

So are you hard wiring the optocoupler's to the pins of a Raspberry Pi?  
Your mention of mqtt suggested that your relay board was wifi connected.

---

<div class="post-metadata">

**Author:** ![duffmans](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@duffmans](https://discourse.nodered.org/u/duffmans)\
**Post date:** [16 May 2020 23:29 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/5 "2020-05-16T23:29:19Z")

</div>

I am yes,pi4 with wifi and powering via a 5v pin direct from pi. I haven't been able to figure the mqtt function out to be honest.

---

<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:** [17 May 2020 00:15 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/6 "2020-05-17T00:15:25Z")

</div>

Hi @duffmans.

I think you may need to step back a bit at this point with what you are wanting to do.

Couple of things of slight concern to me:  
The RasPie puts out 3.3v.  
Be careful not to try and source too much from the pins either.

Can you _draw_ a basic circuit with which you are driving the relays/opto couplers?  
(At a pinch use a print program to draw what you have)

Basically you are _activating_ a GPIO pin and that will turn the fan _on_ or _off_.

Before you start with `MQTT` and all that, it may be a good idea to get the relays working _manually_ from the machine.

To do this you will (probably) need the `raspigpio` node.  
`node-red-node-pi-gpio`

That will allow you to control the GPIO pins.  
Be it via _direct_ control or `MQTT`.  
Install that node and make a little flow to control the fans from a flow on that machine and work up from there.

It may save you a lot of headaches.

Because once you have done that and see it working, it is very easy to simply add a `MQTT in` node and control it from a remote machine.

Basic idea for circuit:  
The + is +5v. - is 0v. You may want to add fly-back diodes around the relay and its contacts.  
The resistor is needed to limit current through the LED and not burn out the RasPie's GPIO pin.

But as said: This is a basic idea of how to do it.

![GPIO-FAN-idea](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/4322fe787093408a0f3c622692af851aa6381d89.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 May 2020 05:55 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/7 "2020-05-17T05:55:03Z")

</div>

@Trying_to_learn he says " It is all built and i can remotely control 3 fans from a node red dashboard. But i can't figure out how to display the status of the pythonshell node on dashboard. ". From that I take it that the basic electronics is working.

---

<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:** [17 May 2020 08:28 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/8 "2020-05-17T08:28:30Z")

</div>

Sorry. My bad.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [17 May 2020 09:39 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/9 "2020-05-17T09:39:42Z")

</div>

> [@duffmans](#):
>
> I haven't been able to figure the mqtt function out to be honest.

To use mqtt, you would need some wifi connected hardware, such as a [shelly](https://shelly.cloud/shelly1-open-source/) relay or even adding a [wifi equipped module](https://docs.wemos.cc/en/latest/) to your existing relays.  
The advantage of using mqtt is that you would not need to physically connect the pi to the relay, so no wires. Your pi would send instructions to the relay via wifi instead.

But back to your current project, can you paste your current flow, so that we can see what you have so far. - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![duffmans](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@duffmans](https://discourse.nodered.org/u/duffmans)\
**Post date:** [20 May 2020 19:23 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/10 "2020-05-20T19:23:49Z")

</div>

Sorry been a busy few days, I honestly did not know about the wifi relays and would of probably been the better route to take for me. I would still really like to finish what i started tho.I have managed to get the fans working and an led status for on/off on the dashboard. But my plan all along was for a switch so i want a switch 😀

my current flow using 2 buttons instead

```auto
[{"id":"a5e2be61.e18a2","type":"pythonshell in","z":"a5a4c878.efcb48","name":"relay 1 on","pyfile":"/home/pi/relay1on.py","virtualenv":"","continuous":false,"stdInData":false,"x":360,"y":220,"wires":[[]]},{"id":"18a18085.1dcfff","type":"ui_button","z":"a5a4c878.efcb48","name":"","group":"42ec51ce.866f6","order":3,"width":1,"height":1,"passthru":false,"label":"on","tooltip":"","color":"black","bgcolor":"green","icon":"","payload":"0","payloadType":"num","topic":"","x":180,"y":220,"wires":[["a5e2be61.e18a2","4ae8f6a9.072b38"]]},{"id":"4c3bce45.5a496","type":"ui_button","z":"a5a4c878.efcb48","name":"","group":"42ec51ce.866f6","order":5,"width":1,"height":1,"passthru":false,"label":"off","tooltip":"","color":"black","bgcolor":"red","icon":"","payload":"1","payloadType":"num","topic":"","x":190,"y":340,"wires":[["46eceef8.f5205","4ae8f6a9.072b38"]]},{"id":"46eceef8.f5205","type":"pythonshell in","z":"a5a4c878.efcb48","name":"relay 1 off","pyfile":"/home/pi/relay1off.py","virtualenv":"","continuous":false,"stdInData":false,"x":360,"y":340,"wires":[[]]},{"id":"4ae8f6a9.072b38","type":"ui_led","z":"a5a4c878.efcb48","group":"42ec51ce.866f6","order":4,"width":1,"height":1,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"","x":430,"y":280,"wires":[]},{"id":"42ec51ce.866f6","type":"ui_group","z":"","name":"main","tab":"61e0a8ed.2916f8","order":1,"disp":true,"width":8,"collapse":false},{"id":"61e0a8ed.2916f8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 May 2020 20:25 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/11 "2020-05-20T20:25:49Z")

</div>

Something like this?

![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77d36027fb41200dc82aad85b13e7d97096e0eeb.jpeg)

```auto
[{"id":"28d53a94.df1696","type":"ui_led","z":"c9c4eeb8.bad8e","group":"cb6df7f2.076ee8","order":4,"width":1,"height":1,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"","x":370,"y":2360,"wires":[]},{"id":"ef7cf5b3.be7b78","type":"pythonshell in","z":"c9c4eeb8.bad8e","name":"relay 1 on","pyfile":"/home/pi/relay1on.py","virtualenv":"","continuous":false,"stdInData":false,"x":540,"y":2340,"wires":[[]]},{"id":"f9666944.150c38","type":"pythonshell in","z":"c9c4eeb8.bad8e","name":"relay 1 off","pyfile":"/home/pi/relay1off.py","virtualenv":"","continuous":false,"stdInData":false,"x":540,"y":2300,"wires":[[]]},{"id":"d9b795c4.968428","type":"ui_switch","z":"c9c4eeb8.bad8e","name":"","label":"switch","tooltip":"","group":"cb6df7f2.076ee8","order":0,"width":"2","height":"1","passthru":false,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":230,"y":2320,"wires":[["d530109c.6f6c6","28d53a94.df1696"]]},{"id":"d530109c.6f6c6","type":"switch","z":"c9c4eeb8.bad8e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":2320,"wires":[["f9666944.150c38"],["ef7cf5b3.be7b78"]]},{"id":"cb6df7f2.076ee8","type":"ui_group","z":"","name":"camAlerte","tab":"49bc4576.14e3fc","order":10,"disp":true,"width":6,"collapse":true},{"id":"49bc4576.14e3fc","type":"ui_tab","z":"","name":"Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![duffmans](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@duffmans](https://discourse.nodered.org/u/duffmans)\
**Post date:** [20 May 2020 21:47 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/12 "2020-05-20T21:47:02Z")

</div>

Perfect, thank you. I changed the 1/0 around to work for me.

---

<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:** [19 July 2020 21:47 UTC](https://discourse.nodered.org/t/relay-switch-via-dashboard-led/26757/13 "2020-07-19T21:47:14Z")

</div>

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