# On/off switch start and stop Python script

**URL:** https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926
**Category:** Dashboard
**Created:** [24 February 2022 13:36 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926 "2022-02-24T13:36:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edsulst](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edsulst/32/57299_2.png) [@edsulst](https://discourse.nodered.org/u/edsulst)
#### Post date: [24 February 2022 13:36 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926/1 "2022-02-24T13:36:22Z")

</div>

I'm completely new with Node RED but so far I can make a button start a python script and another button stops the script.

But I would like to do this by using a on/off switch.  
I can't get it to work.

The switch has 2 parameters (On Payload / Off Payload) and I thought I add a Exec node tot start and another Exec node to stop, but off course this doesn't work.

Can anybody help me?  
What I want is to turn the switch to ON and the Python script mus start.  
If I set the switch to OFF the script to kill the running Python script must be run.

![node-red-python](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c1fd0ca1fd72f7c3dca33201435cf4409ddd581.png)

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [24 February 2022 13:45 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926/2 "2022-02-24T13:45:46Z")

</div>

A hint, use a switch node after the button

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

---

<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: [24 February 2022 13:50 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926/3 "2022-02-24T13:50:19Z")

</div>

Or with one ui-switch node and one exec node

```auto
[{"id":"3b8da533.1afcea","type":"ui_switch","z":"bf9e1e33.030598","name":"","label":"switch","tooltip":"","group":"2d4fe667.28f8ba","order":25,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"python/home/pi/test.py","onvalueType":"str","onicon":"","oncolor":"","offvalue":"fpkill -9 -f test.py","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":250,"y":2260,"wires":[["7700fa69.710624"]]},{"id":"7700fa69.710624","type":"exec","z":"bf9e1e33.030598","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":420,"y":2260,"wires":[[],[],[]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

 ![Screenshot_20220224-135046](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71ee54f1c353cfef7b6144190ef8e1e8d3ffad95.png)

---

<div class="post-metadata">

### Author: ![edsulst](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edsulst/32/57299_2.png) [@edsulst](https://discourse.nodered.org/u/edsulst)
#### Post date: [24 February 2022 13:58 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926/4 "2022-02-24T13:58:02Z")

</div>

Wow, that's easy.

---

<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: [26 March 2022 13:58 UTC](https://discourse.nodered.org/t/on-off-switch-start-and-stop-python-script/58926/5 "2022-03-26T13:58:52Z")

</div>

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