Tutorial: Restart Binding from rule

Tags: #<Tag:0x00007faee8077a08> #<Tag:0x00007faee80778a0> #<Tag:0x00007faee80775d0>

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 /srv/openhab2-userdata/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 either
/var/lib/openhab2
or
/usr/share/openhab2

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)

8 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)
1 Like