Shutting down Pi from NR

Hi.

I was wondering if there's a way to issue system commands from NR (Raspbian).

Basically, I need to shut it down based on certain condition (reading an MQTT message, GPIO state, etc.)

TIA

Here is an example flow that will give you 5 seconds to confirm the shutdown, You should be able to modify it to your needs.

[{"id":"2c018f5e.11313","type":"tab","label":"shutdown","disabled":false,"info":""},{"id":"2519ee4a.ab1332","type":"exec","z":"2c018f5e.11313","command":"sudo shutdown -h now","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Shutdown the RPi","x":750,"y":380,"wires":[[],[],[]]},{"id":"baec1071.bec978","type":"inject","z":"2c018f5e.11313","name":"Shutdown this Pi","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":220,"wires":[["606c992e.6d6eb8","aa08b69c.ef7b08"]]},{"id":"eb8c8ce9.9e431","type":"inject","z":"2c018f5e.11313","name":"Confirm shutdown","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":380,"wires":[["604071e0.f181f"]]},{"id":"e94145e4.703b58","type":"function","z":"2c018f5e.11313","name":"","func":"if (msg.payload == 1)\n    {\n        flow.set(\"shutdown\", \"yes\")\n        node.status({text:\"Shutdown = yes\"});\n    }\nelse if (msg.payload === 0)\n    {\n        flow.set(\"shutdown\", \"no\")\n        node.status({text:\"Shutdown = no\"});\n    }","outputs":0,"noerr":0,"x":570,"y":220,"wires":[]},{"id":"606c992e.6d6eb8","type":"trigger","z":"2c018f5e.11313","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":360,"y":220,"wires":[["e94145e4.703b58"]]},{"id":"604071e0.f181f","type":"function","z":"2c018f5e.11313","name":"","func":"var shutdown = flow.get(\"shutdown\") || \"no\";\n\nif ( (msg.payload == 1) && (shutdown == \"yes\") )\n    {\n        node.status({text:\"Shutting down RPi\"});\n        return msg;\n    }\nelse\n    {\n    node.status({text:\"Nothing doing\"});\n        return null;\n    }","outputs":1,"noerr":0,"x":570,"y":380,"wires":[["f4eead.4206915","2519ee4a.ab1332"]]},{"id":"aa08b69c.ef7b08","type":"function","z":"2c018f5e.11313","name":"Inverter","func":"if (msg.payload == 1)\n{\n    msg.payload = 0;\n}\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":300,"wires":[["604071e0.f181f"]]},{"id":"c29f4af1.c246d","type":"comment","z":"2c018f5e.11313","name":"Routine to shutdown this Pi","info":"","x":180,"y":100,"wires":[]},{"id":"deb040ea.60319","type":"comment","z":"2c018f5e.11313","name":"Press the Confirm button within 5 seconds if you do really want to shutdown","info":"","x":330,"y":140,"wires":[]},{"id":"f4eead.4206915","type":"debug","z":"2c018f5e.11313","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":300,"wires":[]}]
6 Likes

Thank you @zenofmud! I wish I had known there was an Exec node :face_with_hand_over_mouth:

I've been working with @zenofmud on a couple of projects.
Here's the shutdown routine extended to shutdown a group of RPi(es) at my IoT club.

I'm using the Pi at IP address 192.168.1.142 as the master.
It can issue a global shutdown to all the Pi(es) or selectively to just one.
I use these flows at the end of each session of the club so all the Pi(es) are shut down correctly - rather than some students trying to be helpful by pulling the power leads out !!!!!!

The flow for the master uses 'node-red-configurable-ping' to ping all the Pi(es) at the club and report the status of each one. Obviously you can remove this part if you're not interested in it.

I've attached two flows: one for the master and one for a slave (e.g. 192.168.1.140).

Hope you find the flows useful.

global_shutdown.json (7.3 KB)

node_140.json (4.3 KB)

2 Likes

Thank you so much @dynamicdave!

I plan on using this on a custom made "HAT" with an UPS. Based on the state of an IO pin (external PSU will set a pin high). When no electricity, that pin will be low, then it will initialize the shut down sequence.

Hi Zen and Dave, I cant seem to import the flows. Copy and paste in import clipboard and import, but bank?

Hi all,
Sorry for that, red hearing, fingers trouble, all good.
Cheers,

1 Like