Turn the whole flow on or off using telegram bot


I am trying to turn my flow on/off using telegram bot, I have tried several things but none of them worked so far..
My project is some kind of alarm that sends a message when the PIR sensor detects movement. I tried using an AND gate so that when I send "/on" the pir sensor would be able to detect and when i send "/off" the pir sensor wouldn't do anything.
However, when I send the command ("/on" or "/off"), it just passes the AND gate and sets my system to high causing it to send me a message.

Basically i am trying to set my system to stand by with /on and off with /off.
I dont want the pir sensor to constantly detect movement when im home ofcourse...

Any suggestions?
I know this is probably a dumb question, but i'm a noob at node-red :slight_smile:



First off, you don't want to turn the 'flow' off because how would you turn it back on if it was off?

What you want to do is prevent a portion of your flow from executing if the /off has been received, so why not save the state of the on/off in a flow variable (you can do this in a change node) and then in the PIR code, only pass the PIR value if the flow variable is 'on'



So I won't be needing the AND gate then? Or do I get this wrong?



And how exactly should I save the state?



It might be simpler to use node-red-contrib-simple-gate which blocks or allow messages through based on control messages that you send it. When you receive a message from telegram use Switch and/or change Nodes to set the appropriate control message and pass it to the gate. No need to save things in the context.

1 Like


I would have one flow that listened for the temegtaph message and stored the value in a flow context variable (see https://nodered.org/docs/user-guide/context).

Next I would have the air send it's msg to a seitch node which would check the flow context variable and only forward the msg to the rest of that flow it the variable was 'on'. No AND gate needed.



Since the OP is a beginner, I believe you meant switch node instead of seitch node :slight_smile:

I found the contributed logic nodes to be bizarre at best and quickly gave up on them for a function node and flow variables when I was first starting out with node-red.
The root issue with the logic node is node-red only allows a single input to a node, this is a feature, not a bug.

If you really need traditional AND/OR/XOR login in your flow, as far as I can tell, it is best done inside a function node.



@wb666greene have you looked at node-red-contrib-simple-gate? It does make controlling the simple case of letting messages through or not very simple.



Is the node-red-contrib-simple-gate the easiest way for a beginner? or should I use function and swith or change nodes? :slight_smile:



Whichever makes more sense to you!

Some people don't like to use context values, others don't mind. In the end the one that you understand and remember is the one to use

1 Like


This is possibly the simplest way, using node-red-contrib-simple-gate. In fact switch and change nodes could be combined using some JSONata but that would be more difficult to see what is going on.

[{"id":"542edd66.f00d14","type":"gate","z":"d7ff0732.2f81d8","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":458.5,"y":336,"wires":[["cb0825d9.d63c6"]]},{"id":"c27d22ef.fd3fb","type":"inject","z":"d7ff0732.2f81d8","name":"Data messages come in here","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":175.5,"y":336,"wires":[["542edd66.f00d14"]]},{"id":"72868e46.f6c37","type":"inject","z":"d7ff0732.2f81d8","name":"On message from telegram","topic":"","payload":"/on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":157.5,"y":440,"wires":[["4cdd7e72.f22f18"]]},{"id":"e296973b.a98458","type":"inject","z":"d7ff0732.2f81d8","name":"Off message from telegram","topic":"","payload":"/off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":158.5,"y":475,"wires":[["4cdd7e72.f22f18"]]},{"id":"4cdd7e72.f22f18","type":"switch","z":"d7ff0732.2f81d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"/on","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":376.5,"y":458,"wires":[["b35af95f.50d0d8"],["32f79208.824d7e"]]},{"id":"cb0825d9.d63c6","type":"debug","z":"d7ff0732.2f81d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":609.5,"y":336,"wires":[]},{"id":"b35af95f.50d0d8","type":"change","z":"d7ff0732.2f81d8","name":"Open","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":508.5,"y":433,"wires":[["542edd66.f00d14"]]},{"id":"32f79208.824d7e","type":"change","z":"d7ff0732.2f81d8","name":"Open","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":508,"y":478,"wires":[["542edd66.f00d14"]]}]

[edit] Oops, the lower Change node should be called Close. It is only the name that is wrong though.

1 Like


Thank you very much, I will try this :slight_smile:



Just to show another simplest way :stuck_out_tongue_winking_eye:

1 Like


So do I just need to connect the "Put msg.payload into flow.switch" to the "pass only is the flow.twitch 'on' "? And I can just paste the siren on the place were the debug node is?



Nope. You will be putting the value (on/off??) comming from telegrapf right into the flow context variable. That is avaliable to all other nodes on this tab. Then the Pir will be sending data but it has to pass thru the swirch node which tests the flow context variable to see what value it has and it will only pass the msg forward it it matches the 'on' value sent from telegraph.

1 Like


Okay, thank you :slight_smile:



No I haven't looked at it, I tried the "boolean" nodes (I think that was what they were called) and when that wasn't working as I expected/desired I was very comfortable with using "flow variables" in a function node, while new to JavaScript, my programming goes back to 8080 & 6502 assembly language in the dark ages before the IBM PC :slight_smile:

I certainly will take a look at it next time I need that kind of functionality. There is virtue is staying pure nod-red, as there is in just soldiering-on and quickly making a workable solution



Hey, I have another question :slight_smile:

The turning on and off works now, but now I can't turn off the buzzer anymore.
My plan is to leave the siren on untill I use the /off command but this is not the case at the moment.
Any idea how I can fix this?

This is my flow.



That is a big flow - what part is the problem? In a case like this, I would make a copy of the flow on another tab and then remove everything but what causes the problem. That is what I would share with people so they will not have to spend a lot of their time helping me. I what to make it as easy as I can because I know I'm more apt to get help that way.

1 Like