# Looking for a relay switch function

**URL:** https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065
**Category:** General
**Created:** [12 September 2018 02:38 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065 "2018-09-12T02:38:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [12 September 2018 02:38 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/1 "2018-09-12T02:38:56Z")

</div>

Hello

I am looking for a function so the flow of messages will only be allowed when a mqtt status is  
revived,but been able to have control independently of one

example  
input - mqtt - cmd heater on  
input - mqtt - cmd pump on

input to alow traffic of heater to be sent and is always checking  
input - stat pump power on

in an electrical system it is called a relay  
the coil would be the stat msg

Thank you  
chris

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4de99afd56b2cb02a9f63cfc86960754fed33824.jpeg)

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 September 2018 06:37 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/3 "2018-09-12T06:37:58Z")

</div>

If you feed your control messages to flow variables, then you can read the value of those flow values in a switch node and route you messages as required

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e204bd2325b2ab7bc9fbd4a85dc8db1a25676be4.png)

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 September 2018 06:53 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/4 "2018-09-12T06:53:11Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fdf7986c391c7814d2d9e86965d1649f3d2780d5.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1e9b2aeba7ff0020ef44c8976d071adb94003bc2.png)

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [12 September 2018 21:58 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/5 "2018-09-12T21:58:40Z")

</div>

sorry i cant see how this works i am tired to be honest so probably not the best time to be looking at it  
so i need the status of the pump to be on, to allow the command of the heater power on when it is called for but also turning the heater off is the pump stops running this is controlling my hot tub

i have a manual fail safe built in on the circuits but i would like the logical one to be primary in case either fail

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 September 2018 22:02 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/6 "2018-09-12T22:02:37Z")

</div>

I was just showing how to implement the relay bit - not how to do everything sorry

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [13 September 2018 02:31 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/7 "2018-09-13T02:31:48Z")

</div>

The change node in the example sends its payload to a flow context called "flow.heater." For example, if the mqtt topic you are subscribing to gets a message of "on" you'd have flow.heater="on" in your flow context store, which is accessible to any nodes in this flow.

The switch node is set to evaluate the flow context "flow.heater" that you set with the change node. If flow.heater=on your payload being sent to the switch is sent out the top output. If it's anything else at all, it's sent out the bottom. (It's basically set up as an if/else.)

Since you put it in EE terms, you can think of the switch node as acting as your relay, with the flow.heater context in the example being whether you are applying your trigger voltage or not.

Also, if you want to genericize this, each tab gets its own flow context and flows can't see each others contexts (but there is also a global context that is shared among all flows. Subflows are treated as basically being their own tab, so if you want to do a lot of copy-pasted or referenced logic it's a good idea to wrap it in sub flows so you don't have to re-declare variables all over the place.

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [14 September 2018 19:28 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/8 "2018-09-14T19:28:27Z")

</div>

hi been looking at this all day without luck can u do a quick example so i can see

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [14 September 2018 19:33 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/9 "2018-09-14T19:33:32Z")

</div>

are you able to show please i haven't go a clue lol

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [14 September 2018 19:40 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/10 "2018-09-14T19:40:19Z")

</div>

Play with this flow until it makes sense.

`[{"id":"ea278027.db323","type":"switch","z":"fd401019.9d69d","name":"","property":"relayOn","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":380,"wires":[["2bc818cc.e4eee"],["3c79f8df.236648"]]},{"id":"5937761d.c49ce8","type":"inject","z":"fd401019.9d69d","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":200,"wires":[["970fbcde.71d9d"]]},{"id":"d52ee9ef.e2282","type":"inject","z":"fd401019.9d69d","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":240,"wires":[["970fbcde.71d9d"]]},{"id":"970fbcde.71d9d","type":"change","z":"fd401019.9d69d","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"relayOn","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":220,"wires":[[]]},{"id":"830e7a54.bfa478","type":"inject","z":"fd401019.9d69d","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":340,"wires":[["ea278027.db323"]]},{"id":"cc839177.1610c8","type":"inject","z":"fd401019.9d69d","name":"","topic":"","payload":"Test message 1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":380,"wires":[["ea278027.db323"]]},{"id":"a8c67ad0.9d5ee","type":"inject","z":"fd401019.9d69d","name":"","topic":"","payload":"Another test message","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":420,"wires":[["ea278027.db323"]]},{"id":"59dc60e2.65226","type":"comment","z":"fd401019.9d69d","name":"This sets flow.relayOn","info":"","x":250,"y":160,"wires":[]},{"id":"ee876a11.61fac","type":"comment","z":"fd401019.9d69d","name":"This evaluates for flow.relayOn true/false","info":"","x":700,"y":340,"wires":[]},{"id":"2bc818cc.e4eee","type":"debug","z":"fd401019.9d69d","name":"if flow.relayOn is true, message comes out here","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1040,"y":360,"wires":[]},{"id":"3c79f8df.236648","type":"debug","z":"fd401019.9d69d","name":"if flow.relayOn is false, message comes out here","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1040,"y":400,"wires":[]}]`

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [14 September 2018 19:42 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/11 "2018-09-14T19:42:42Z")

</div>

nothing in message ialso need this for a automatic dimming on dusk dawn for zwave lighting too

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [14 September 2018 19:43 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/12 "2018-09-14T19:43:55Z")

</div>

go it mate thankyou ill have a look

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [14 September 2018 19:44 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/13 "2018-09-14T19:44:04Z")

</div>

Check again. I updated.

I recommend looking over the example until you fully understand what's going on before adding complexity. Ask questions as you go if anything is stumping you.

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [14 September 2018 19:49 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/14 "2018-09-14T19:49:08Z")

</div>

i get how its changing but i dont understand how it alows the other mqtt topic to turn on  
topic heater  
topic pump?

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [14 September 2018 19:52 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/15 "2018-09-14T19:52:30Z")

</div>

The flow i gave isn't doing anything with mqtt yet. The cluster of nodes at top set the gate for the switch node. The switch node simply routes traffic to either the top or bottom output. That traffic can either be passed elsewhere, or used to trigger other events.

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [15 September 2018 01:43 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/16 "2018-09-15T01:43:10Z")

</div>

> [@Zwave day night auto dimming with times and max brightness hassio](https://discourse.nodered.org/t/zwave-day-night-auto-dimming-with-times-and-max-brightness-hassio/3157):
>
> Hello have a look and see what you all think if someone could do the join node better but it works, also anyway of simplifying it i have 24 ways to do on these [{"id":"b84a7f9a.24408","type":"server-events","z":"cf1115c.2dcd6e8","name":"HA All Events","server":"d0be6c93.2903a","x":110,"y":140,"wires":[["fd7c9ec6.5f502"]]},{"id":"29e7db9a.11e0a4","type":"function","z":"cf1115c.2dcd6e8","name":"Brightness Value","func":"var temp=msg.payload.event.new\_state.attributes.brightness;\nvar msg = {pay…

still havent got the relay tho 😫

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [15 September 2018 18:05 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/18 "2018-09-15T18:05:51Z")

</div>

> [@chris1988](#):
>
> still havent got the relay tho 😫

How do the flows/ideas submitted by Simon and Jay _not_ provide you a solution?

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [17 September 2018 22:18 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/19 "2018-09-17T22:18:10Z")

</div>

i cant see it sorry for not getting it is the 2 message payloads been put unto one message? cant see how 2 different message payloads are been controlled or stopped

how does it know what state the flow relay is ? when the message is heater nothing in message contains relay on or off or false true but and also the message will be at different times never at same time?

hope you can make me see sense ☹

Thank you  
chris

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [17 September 2018 22:32 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/20 "2018-09-17T22:32:58Z")

</div>

[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

In summary, each tab has its own store of variables referred to as its "context." Say you want there to be a variable called "heater": if you set flow.heater to a value (let's say we set it to "true" fow now) then node-red stores a value called flow.heater as "true." Any node that evaluates for "flow.heater" looks at the value currently stored in that space and acts accordingly. Those two strings of nodes don't need to be connected. One sets the variable in the flow context. The other looks at the value in the flow context to decide where to route traffic.

---

<div class="post-metadata">

### Author: ![chris1988](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@chris1988](https://discourse.nodered.org/u/chris1988)
#### Post date: [17 September 2018 22:42 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/21 "2018-09-17T22:42:18Z")

</div>

ok could u do a flow showing me please, when you get chance  
I sort of understand

---

<div class="post-metadata">

### Author: ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)
#### Post date: [17 September 2018 23:19 UTC](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065/22 "2018-09-17T23:19:55Z")

</div>

The one i posted 3 days ago includes this. What version are you runnng? 0.19 adds some features that make this _much_ easier to wrap your head around if it hasn't clicked yet.

[Next page](https://discourse.nodered.org/t/looking-for-a-relay-switch-function/3065.md?page=2)
