HowTo: Node Red – Shutdown and restart Raspberry Pi

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

For example, your RaspberryPi can be easily shut down or restarted. You can find examples and information about this 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 needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPi – The first configuration!
RaspberryPi – Control the RaspberryPi via SSH

NodeRed – Installing NodeRed on the RaspberryPi

Required tool:
-no-

Required material:

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


Log into the NodeRed configuration interface

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

View of the login dialog. Here you have to enter the login data that you specified during the configuration of the login. You can find information about this in the article NodeRed - set up user login.

Simple shutdown at the push of a button

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

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

You can find the code here:

[ { "id": "7c9a271.bf32bd8", "type": "inject", "z": "9ccc4e725c3673aa", "name": "shutdown!", "repeat": "", "crontab": "" , "once": false, "topic": "", "payload": "", "x": 520, "y": 1760, "wires": [ [ "e58b5ea3.57e91" ] ] }, { " id": "e58b5ea3.57e91", "type": "exec", "z": "9ccc4e725c3673aa", "command": "sudo poweroff #", "append": "", "useSpawn": "", " name": "turn off Pi", "x": 694, "y": 1759, "wires": [ [], [], [] ] } ]

Shutdown with security question in dashboard

A slightly more luxurious shortcut is included in the code below. Sometimes you get confused. And even though you haven't saved everything yet and didn't want to shut down the Raspberry Pi, you sometimes accidentally click on the “Shutdown” button.

To prevent something like this from happening, simply ask whether you are really sure that you want to shut down the Raspberry Pi.

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

You can find the code here:

[
    {
        "id": "1702b798.ee9108",
        "type": "exec",
        "z": "9ccc4e725c3673aa",
        "command": "sudo shutdown -h now",
        "addpay": false,
        "append": "",
        "useSpawn": "",
        "timer": "",
        "name": "Shutdown",
        "x": 1020,
        "y": 2020,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "7fcdbec0.5807",
        "type": "function",
        "z": "9ccc4e725c3673aa",
        "name": "",
        "func": "if (msg.payload=='Abbrechen'){msg.payload=false;} else if (msg.payload=='OK'){ msg.payload=true;return msg;}",
        "outputs": 1,
        "noerr": 0,
        "x": 890,
        "y": 2020,
        "wires": [
            [
                "1702b798.ee9108"
            ]
        ]
    },
    {
        "id": "25c2c1c0.47385e",
        "type": "ui_toast",
        "z": "9ccc4e725c3673aa",
        "position": "dialog",
        "displayTime": "3",
        "highlight": "",
        "sendall": true,
        "outputs": 1,
        "ok": "OK",
        "cancel": "Cancel",
        "raw": false,
        "topic": "Really?",
        "name": "Really?",
        "x": 760,
        "y": 2020,
        "wires": [
            [
                "7fcdbec0.5807"
            ]
        ]
    },
    {
        "id": "3ff7e395.fd757c",
        "type": "ui_button",
        "z": "9ccc4e725c3673aa",
        "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": 590,
        "y": 2020,
        "wires": [
            [
                "25c2c1c0.47385e"
            ]
        ]
    }
]

Restart Raspberry Pi

What works with shutting down also works with restarting. So here is the flow to restart your RaspberryPi from the dashboard or the NodeRed interface.

On the left you can see the flow with which your RaspberryPi can be restarted.

You can find the code here:

[
    {
        "id": "df20eac13fdcba2e",
        "type": "exec",
        "z": "9ccc4e725c3673aa",
        "command": "sudo reboot",
        "addpay": false,
        "append": "",
        "useSpawn": "",
        "timer": "",
        "name": "Reboot",
        "x": 1140,
        "y": 2300,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "02e02a3df0b16d3d",
        "type": "function",
        "z": "9ccc4e725c3673aa",
        "name": "",
        "func": "if (msg.payload=='Abbrechen'){ msg.payload=false;} else if (msg.payload=='OK'){msg.payload=true; return msg;}",
        "outputs": 1,
        "noerr": 0,
        "x": 990,
        "y": 2280,
        "wires": [
            [
                "df20eac13fdcba2e"
            ]
        ]
    },
    {
        "id": "55940c40590b5819",
        "type": "inject",
        "z": "9ccc4e725c3673aa",
        "name": "",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 720,
        "y": 2320,
        "wires": [
            [
                "df20eac13fdcba2e"
            ]
        ]
    },
    {
        "id": "cd3f522e2a28e48a",
        "type": "ui_toast",
        "z": "9ccc4e725c3673aa",
        "position": "dialog",
        "displayTime": "3",
        "highlight": "",
        "sendall": true,
        "outputs": 1,
        "ok": "OK",
        "cancel": "Cancel",
        "raw": false,
        "topic": "Really?",
        "name": "Really",
        "x": 870,
        "y": 2280,
        "wires": [
            [
                "02e02a3df0b16d3d"
            ]
        ]
    },
    {
        "id": "38d0494f17305068",
        "type": "ui_button",
        "z": "9ccc4e725c3673aa",
        "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": 720,
        "y": 2280,
        "wires": [
            [
                "cd3f522e2a28e48a"
            ]
        ]
    }
]

Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at ko-fi.com       

4 comments

  1. Do you have a tip on how I shut down a Raspi in my network using Node-Red. So not the one running on Node-Red. Thanks! great blog thanks

  2. SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 290 of the JSON data

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.