[SOLVED] Shutting down Raspberry Pi which runs OpenHAB 2

I need to define a rule that calls a “shutdown 0” command via a OpenHAB rule, to turn the RaspberryPi (which runs OpenHAB) off, e.g. by using a switch. How can I do this? Of course the idea for OpenHAB is to run it as 24/7 server, but I think it’s not needed to run e.g. when I’m going on holiday.

Well, if you really want openHAB to shutdown itself, you have to grant openHAB to execute a sudo shutdown -h now.

Hi,

one more idea although I never tried it on a Pi.

But if you don’t want to give OH sudo rights you can use knockd.

You have to install and run the daemon and define a knocking sequence in the config file of knockd. And then you can run a script with the exec binding to do the knocking.

To be honestly I never tried to run everything on the same device but I think it should work. I use this to remote shutdown my Synologys running the daemon and the OH Pi doing the knocking.

Greetings
Sebastian

I know about that point, but the problem is, that i can’t find a way to do this command via item / rule or anything else… It seems to make problems because of missing user rights to run the shutdown.

with exec1 Binding I would try this:

Switch piShutdown {exec="/usr/bin/sudo /bin/shutdown -h now"}

or with exec2 Binding:
.things:

Thing exec:command:TpiShutdown "Pi shutdown" [command="/usr/bin/sudo /bin/shutdown -h now",autorun=false]

and an item corresponding to that thing:

Switch piShutdown {channel="exec:command:TpiShutdown:run"}

Switching the Switch to ON should execute the command. You will have to add the user openhab (given that this is the user which started openHAB) to the file /etc/sudoers like that:

# User privilege specification
root    ALL=(ALL:ALL) ALL
openhab    ALL = NOPASSWD; /bin/shutdown

Please use visudo for changing /etc/sudoers, as otherwise you can brick sudo…

1 Like

always read the last line :slight_smile: ii messed up

sudo blocked

Should be no real problem. Please try

su 

to login as root (after logging in as the normal user.)

BUT BE CAREFULL!

after logging in as root, you can destroy your os in seconds.

Then repair the damage, by using

visudo

again. (You have to know what is the fault in /etc/sudoers to correct it. visudo will only prevent you from saving a damaged configuration)

Thanks Udo

I have made a fresh install

Will retry without forgetting visudo:)

Well, even if your pi won’t start anymore (let’s say, because you bricked one of the essential files), there is no need to start from scratch. You could use another GNU/Linux system (life CD, vm, another computer…), then mount the filesystem (very easy, because it’s on a SDcard :slight_smile: ) and repair the damage. Of course, you have to know, what you’ve damaged… :wink:

Thanks Udo, i have made a backup of my SD.

I’m really struggling to get this working :frowning: Please can someone nudge me in the right direction?

I’ve read the docs regarding the fact that the user “openhab” needs to be able to run the shutdown comamand in order for this to work. I feel this is where I’m going wrong. As the following does not work:

sudo -u openhab reboot

Here are the steps I have taken which have been unsuccessful:

  1. Log into my Pi and add the following to /etc/sudoers.d/common_users
openhab ALL = NOPASSWD: /sbin/reboot, /sbin/shutdown
  1. Add the Exec Binding Add RegEx Transformation Service

  2. Added Thing in Paper UI:

2018-04-05%2014_03_51-Paper%20UI

.items

Switch Reboot "Reboot" { channel="exec:command:6dba3517:run" }

.sitemap

Switch item=Reboot mappings=[OFF="Off", ON="On"]

Any help would be much appreciated thanks.

Yes, this will not work, as it’s incorrect. :slight_smile:
You have to use

sudo -u openhab sudo reboot

This is: "execute the following commmand as user openhab: sudo reboot"
When using sudo the first time (as user openhab), this will result in a message, to respect the privacy of all users. as an alternative, you can change to a bash with login openhab:

sudo su openhab -s /bin/bash

As result you are logged in as user openhab, even if there is no password set and no login shell defined.
No you can perform

sudo reboot

and you will get the message about privacy:

sudo ls

Wir gehen davon aus, dass der lokale Systemadministrator Ihnen die
Regeln erklärt hat.  Normalerweise läuft es auf drei Regeln hinaus:

    #1) Respektieren Sie die Privatsphäre anderer.
    #2) Denken Sie nach, bevor Sie tippen.
    #3) Mit großer Macht kommt große Verantwortung.

[sudo] Passwort für openhab:

(sorry, only localized message here…)

Be aware that the path to reboot is /sbin/reboot, not /bin/reboot

1 Like

Woohoo, it is working now. I noticed as I left it at a 60 second interval by mistake and my Pi kept restarting. :grin:

Thanks for your help :+1:

Thanks for this. Working like a charm, sortoff. Problem is that after I used it, it keeps on shutting down.

From what I could see it seems the switch is initialized from Null to On thus triggering the script again.

I’ve added autoupdate=“false” to the switch item but this doesn’t seem to have helped.

Will initializing the switch within the startup rules prevent this?

Did you persist the item?

I haven’t added anything in a persistence file.

Added the entry in things, items and sitemap as per your suggestion. Cut and pasted them. Just changed the bin to sbin

Are you using exec1 or exec2?

Looks like

binding-exec - 2.4.0
Exec Binding
This is the binding to execute arbitrary shell commands
Author: Karel Goderis

Hi Curtis,
I have the same issues with restarting loop.
How you changed the interval?

@Udo_Hartmann i have made the same mistake again

openhabian is not anymore a sudoer

su root do not work because i do not have the password. blank password do not work

Please help me

i am so upset. twice the same error. last time