Tutorial: Restart Binding from rule

All,

I currently struggle with my Netatmo binding. After a few hours it stops getting updates from the Netatmo station.
So I implemented a solution to restart the binding from within a rule.
I hope this helps someone for solving similar issues with specific bindings:

First generate the key (as an alternative to sshpass) to enable you to start karaf without beeing asked for your password

sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa (no passphrase)

Next, put the result (key only of course) from openhab.id_rsa.pub into /var/lib/openhab2/etc/keys.properties:

openhab=AAAB3....xyz,_g_:admingroup

And connect to karaf by.
sudo -u openhab ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost

This should work to connect to karaf without a password.
You just need to confirm to connect to the host for the first time:
“Warning: Permanently added ‘[localhost]:8101’ (RSA) to the list of known hosts.”

Obviously there is a difference if I switch “really” to openhab user and run from there:
sudo su (to switch to root)
su openhab
This time run of course:
ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost

and again confirm:
Warning: Permanently added ‘[localhost]:8101’ (RSA) to the list of known hosts.

After that you should be able to run from any binding restart:
executeCommandLine("/etc/openhab2/scripts/restartNetatmo.sh", 7000)

Whereas the script contains:

#!/bin/bash
/usr/bin/ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost 'bundle:restart org.openhab.binding.netatmo'

If you need the names for the different bundles you would like to restart, please check in Karaf console:
list -s

In case you would like to restart the binding from within the rule (and without a script), just use this syntax:

executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.netatmo", 6000)

EDIT (OH3 related):
Just a quick modification if you are using OH3:
you need to use
openhab=
instead of
karaf=
in the
/var/lib/openhab/etc/keys.properties

18 Likes

Can you please try last netatmo PR. Mine is working since days without issues. Are you working with v1.1 or v2 of the binding ?

Thanks for your response.
Actually I played around and did some basic research. While doing that I rebooted my raspberry since several weeks for the first time. I currently update the system with new rules almost every day - so that might have caused troubles.

So currently Netatmo (2.1 stable) is running perfectly, but thanks for your support.
Great Binding!

NCO
 just a short message to say that your time writing this post has been very helpful for me.
Thanks a lot :slight_smile: )

1 Like

Thanks for your post
 I am also struggeling with Netatmo
 so I try to work with your information but I failed.
If I try:
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
I got the error:

[17:41:12] openhabian@openHABianPi:~$ sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
[sudo] password for openhabian:
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Saving key “openhab.id_rsa” failed: Permission denied

Can somebody help me?

it seems that you are in a local directory where you’re not allowed to store the generated key.
how about changing first into your home by
“cd”
I have generated the key into:
/home/openhab/karaf_keys/openhab.id_rsa

thanks a lot for fast response 
 now it works

and I’m able to connect to karaf by:

sudo -u openhab ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost

without password.
also I’m able to execute the command:

executeCommandLine("/etc/openhab2/scripts/restartNetatmo.sh", 7000)

but if I try to start the script by:

/etc/openhab2/scripts/restartNetatmo.sh

the system requests a password


[19:46:19] openhabian@openHABianPi:~$ /etc/openhab2/scripts/restartNetatmo.sh Load key “/home/openhab/karaf_keys/openhab.id_rsa”: Permission denied
Password authentication
Password:

andybody knows what I made wrong?

only by using “sudo su” before it works


[19:46:19] openhabian@openHABianPi:~$ /etc/openhab2/scripts/restartNetatmo.sh   Load key "/home/openhab/karaf_keys/openhab.id_rsa": Permission denied
Password authentication
Password:

[19:49:33] openhabian@openHABianPi:~$ sudo su
[19:49:40] root@openHABianPi:/home/openhabian# /etc/openhab2/scripts/restartNetatmo.sh

I like to start the script once per day (cron) will it work by using a rule 
 can sombody help me and explain a little bit what I have to do

And sorry for my bad Englisch :wink:

So, I guess you got a working solution (from rule) so far?
However:
For me the error sounds like you don’t have the access rights to access the key itself.
So, please check the access rights of this file.

I personally would prefer to use a rule instead of a global cron anyway


Hi, thanks again for fast Feedback.
No until now I don’t have a working solution from rule.
Can you please give me an example how I can execute the restart command within a rule?
This would be very helpful for me.

How often do you restart the binding?
Is it possible to recognize if the communication from the binding to the netatmo is crashed and restart the binding?

If you use the /runtime/bin/client script, you can avoid the SSH.

Hi,

sorry, I thought, I have the latest approach in my tutorial above.
Actually nowadays I restart (whatever) binding by:

executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.netatmo", 6000)

That works well and actually since the milestone builds I actually don’t need it anymore.

Hey thanks a lot for your answer
 but unfortunatelly the command wont’t work. :frowning:

I have create the following rule:

rule "reset"
 
when
Item Reset_netatmo received command

then
executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.netatmo", 6000)
Thread::sleep(2000) 
postUpdate(Reset_netatmo, OFF)
end

The problem is that the command won’t work.
The log shows:

2018-12-19 17:33:39.234 [ome.event.ItemCommandEvent] - Item ‘Reset_netatmo’ received command ON
2018-12-19 17:33:39.244 [vent.ItemStateChangedEvent] - Reset_netatmo changed from OFF to ON

And the switch will not switch off by the rule.

If I uncommand the executeCommandLine with “//” the switch will switched of by the rule.
Is there some mistake in my rule?

Hmmm
 did you make sure that openhab (the owner the OH process is running on) has the appropriate permissions?.

please check the sudo permissions by:
sudo visudo

there should be a line like:

openhab ALL = NOPASSWD: /bin/rm, /bin/chmod, /bin/chown, /usr/bin/amixer, /usr/bin/l2ping, /usr/bin/ssh, /sbin/reboot, /sbin/iptables, /sbin/modprobe, /etc/init.d/openhab2, /etc/init.d/mosquitto, /etc/init.d/openvpn, ...

your line should at least contain ssh.

Maybe that’s it!?

I’d like to understand how I should be using this one. Would the following be correct?

rule "reset"
 
when
Item Reset_netatmo received command

then
executeCommandLine("/usr/share/openhab2/runtime/bin/client bundle:restart org.openhab.binding.netatmo", 6000)
Thread::sleep(2000) 
postUpdate(Reset_netatmo, OFF)
end

EDIT: from the command line, this works.

Note that on my environment, the valid path for accessing /runtime/bin/client is:

/usr/share/openhab2/runtime/bin/client

I’ve given up on using /runtime/bin/client within a rule, since I was getting inconsistent results. Sometimes it worked, but not always


from core.actions import Exec

Exec.executeCommandLine("/bin/sh@@-c@@/opt/openhab2/runtime/bin/client 'bundle:restart org.openhab.binding.upnpcontrol;'", 10000)

So, I went back to ssh, which works consistently. This is what I use in a rule to restart the DLNA binding after OH startup to get the subscriptions working


from core.actions import Exec

Exec.executeCommandLine("/bin/sh@@-c@@ssh -p 8101 -l openhab localhost 'bundle:restart org.openhab.binding.upnpcontrol;'", 10000)
2 Likes

Question: is this approach working with the latest openhab snapshot 2.5.7?

I actually created the pair of keys and inserted the public key into the keys.properties.
I copied the private key file into /home/openhabian.

when I try to connect to karaf using
sudo -u openhab ssh -i ~/openhab.id_rsa -p8101 openhab@localhost
I get the request to confirm the host 
 and then I get a password prompt. So somehow, it is not working for me.

Anybody using this approach successfully with a recent openhab version?

Yes, still works for me.

Yes, this is still working for me on 2.5.7
I just used it this morning.
Did you try to use the absolute / full path.
And did you check the permissions of the file?

After redoing the “keys.properties” file again correctly

I got the following command to work from the command line, wo entering a password:

sudo /usr/bin/ssh -p 8101 -i ~/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.ipcamera

I have to enter the openhabian password for the SUDO though.

When I use

executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /etc/openhab2/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.ipcamera")

in a rule, the command does not get executed, what I see in the log though is:

2020-08-13 16:03:15.269 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'sudo /usr/bin/ssh -p 8101 -i /etc/openhab2/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.ipcamera'

any suggestion?

I have a “/usr/bin/ssh” entry in my sudoers.d file for the user openhab

did you put /usr/bin/ssh using visudo for openhabian as well?
This (password for sudo when beeing openhabian) should then be solved.

executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /etc/openhab2/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.ipcamera")

you forgot the timeout at the end:

executeCommandLine("sudo /usr/bin/ssh -p 8101 -i /etc/openhab2/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.ipcamera", 5000)

But I am not sure, if a default value is used then.
It’s worth a try I guess