HowTo: Node Red – Shut down and restart Raspberry Pi

Another practical thing in Node Red is that you can also trigger common Linux commands conveniently via NodeRed node.

For example, your Raspberry Pi can be easily shut down or restarted. Examples and information can be found in the following article.


Requirements

Helpful Articles:
So that you can install new nodes, NodeRed should of course already be installed. How to prepare a RaspberryPi and then install NodeRed on it is described in the following articles.

The following three articles describe what to do to prepare the RaspberryPi:
RaspberryPi – Einrichten für Nerdiys!
RaspberryPi – Die Erste Konfiguration!
RaspberryPi – Den RaspberryPi über SSH steuern

NodeRed – Installation von NodeRed auf dem RaspberryPi

Required tools:
-none-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in to the NodeRed configuration interface

Before you can edit your NodeRed configuration you must - if activated - first log in to the NodeRed configuration interface.

View of the login screen. Here you have to enter the login details that you entered during the configuration of the login. Information about this can be found in the article NodeRed - User Login Setup.

Simply shut down at the push of a button

To simply shut down your RaspberryPi, two simple nodes are sufficient.

On the left you can see the structure of two nodes that are already sufficient to shut down your RaspberryPi from the NodeRed environment.

You can find the code here:

[{"id":"7c9a271.bf32bd8","type":"inject","z":"2dcf0cc0.aeb894","name":"shutdown!","topic":"","payload":"","repeat":"","crontab":"","once":false,"x":1560,"y":380,"wires":[["e58b5ea3.57e91"]]},{"id":"e58b5ea3.57e91","type":"exec","z":"2dcf0cc0.aeb894","command":"sudo poweroff #","append":"","useSpawn":"","name":"turn off Pi","x":1734,"y":379,"wires":[[],[],[]]}]

Shutdown with security question in the dashboard

A slightly more luxurious flow is included in the code below. Sometimes you click on the wrong button. And even though you haven’t saved everything yet and didn’t want to shut down the RaspberryPi, you sometimes accidentally click on the “Shutdown” button.

To prevent such a thing, a simple question is sufficient whether you are really sure that you want to shut down the RaspberryPi.

On the left you can see the flow described. Only after you have confirmed the shutdown command it will be executed.

You can find the code here:

[{"id":"1702b798.ee9108","type":"exec","z":"2dcf0cc0.aeb894","command":"sudo shutdown -h now","addpay":false,"append":"","useSpawn":"","timer":"","name":"Shutdown","x":600,"y":440,"wires":[[],[],[]]},{"id":"7fcdbec0.5807","type":"function","z":"2dcf0cc0.aeb894","name":"","func":"if (msg.payload==\"Abbrechen\")\n{\n    msg.payload=false;\n} else if (msg.payload==\"OK\")\n{\n    msg.payload=true;\n    return msg;\n}\n","outputs":1,"noerr":0,"x":470,"y":440,"wires":[["1702b798.ee9108"]]},{"id":"25c2c1c0.47385e","type":"ui_toast","z":"2dcf0cc0.aeb894","position":"dialog","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":false,"topic":"Really?","name":"Really?","x":340,"y":440,"wires":[["7fcdbec0.5807"]]},{"id":"3ff7e395.fd757c","type":"ui_button","z":"2dcf0cc0.aeb894","name":"","group":"c50fec45.1c008","order":4,"width":0,"height":0,"passthru":false,"label":"Server Powerdown","tooltip":"","color":"","bgcolor":"red","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":170,"y":440,"wires":[["25c2c1c0.47385e"]]},{"id":"c50fec45.1c008","type":"ui_group","z":"","name":"Server","tab":"b128bba3.f99e38","order":3,"disp":true,"width":"6","collapse":false},{"id":"b128bba3.f99e38","type":"ui_tab","z":"","name":"Einstellungen","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

Restart the RaspberryPi

What works with the shutdown, of course also works with the restart. So here is the flow to restart your RaspberryPi from the dashboard or the NodeRed interface.

Links seht Ihr den Flow mit dem Euer RaspberryPi neu gestartet werden kann.

You can find the code here:

[{"id":"9bb1d464.175b18","type":"exec","z":"2dcf0cc0.aeb894","command":"sudo reboot","addpay":false,"append":"","useSpawn":"","timer":"","name":"Reboot","x":580,"y":340,"wires":[[],[],[]]},{"id":"fe864b5a.cda608","type":"function","z":"2dcf0cc0.aeb894","name":"","func":"if (msg.payload==\"Abbrechen\")\n{\n    msg.payload=false;\n} else if (msg.payload==\"OK\")\n{\n    msg.payload=true;\n    return msg;\n}\n","outputs":1,"noerr":0,"x":430,"y":320,"wires":[["9bb1d464.175b18"]]},{"id":"515814bf.e5f2ac","type":"inject","z":"2dcf0cc0.aeb894","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":360,"wires":[["9bb1d464.175b18"]]},{"id":"4451448.4acdabc","type":"ui_toast","z":"2dcf0cc0.aeb894","position":"dialog","displayTime":"3","highlight":"","sendall":true,"outputs":1,"ok":"OK","cancel":"Cancel","raw":false,"topic":"Really?","name":"Really","x":310,"y":320,"wires":[["fe864b5a.cda608"]]},{"id":"6124e9cf.3fdf68","type":"ui_button","z":"2dcf0cc0.aeb894","name":"","group":"c50fec45.1c008","order":3,"width":0,"height":0,"passthru":false,"label":"Server Reboot","tooltip":"","color":"","bgcolor":"","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":160,"y":320,"wires":[["4451448.4acdabc"]]},{"id":"c50fec45.1c008","type":"ui_group","z":"","name":"Server","tab":"b128bba3.f99e38","order":3,"disp":true,"width":"6","collapse":false},{"id":"b128bba3.f99e38","type":"ui_tab","z":"","name":"Einstellungen","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

Fab

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *