The openwebnet binding/gateway sometimes goes OFFLINE… grrr!! . It’s a rare event but has a happened to me a few times for unknown reasons.
So, I set out to fix the sporadic offline gateway problem automatically and below is how to do it.
My system Raspberry Pi4 openhabian OH2.5.2
The fix is in two parts.
- Part 1 Detection of offline Gateway Thing. I have a rule to check every 30mins if the binding is online or not. This sends me alerts if the binding is not online.
- Part2 If the binding is not online the rule runs a script to bring it back online with a binding restart.
Part1 Detection
Expire binding is needed to create timers.
.items
//Limit email notifications to every 60mins
Switch GatewayOfflineNotification_Timer "Gateway offline notifications paused 60mins" <time> {expire="60m,command=OFF"}
Rule to check if gateway is online and if it’s not restart it
rule "Check BUS gateway status"
when
Time cron "0 0/30 * * * ?" //Every 30mins
then
val thingStatusInfo = getThingStatusInfo("openwebnet:bus_gateway:gateway")
val mailActions = getActions("mail","mail:smtp:emailserver")
if ((thingStatusInfo !== null) && (thingStatusInfo.getStatus().toString() == "ONLINE")) {
logInfo("Thing_Status", "BUS gateway is online")
}
else {
logError("Thing_Status", "BUS gateway is OFFLINE") //or doesn't exist
sendNotification(''email address','BUS gateway went OFFLINE at '+ now.toString("HH:mm dd-MM-yyyy"))
val results = executeCommandLine("/etc/openhab2/scripts/restart_openwebnet.sh",10000)
if (results == '') {
logInfo("Thing_Status", "Restart openwebnet binding script successful")
sendNotification(''email address','BUS gateway restarted at '+ now.toString("HH:mm dd-MM-yyyy"))
if ( GatewayOfflineNotification_Timer.state !== ON) { //Limit emails to one every 30mins
GatewayOfflineNotification_Timer.sendCommand(ON)
mailActions.sendMail("'email address", "BUS gateway went offline", "BUS gateway restarted at " + now.toString("HH:mm dd-MM-yyyy") )
}
}
else {
logInfo("Thing_Status", "Restart openwebnet script failed. Result = " + results)
}
}
end
Part2. Restart binding with a script
As it is the above rule will not execute the command without some additional work. To restart the binding the karaf console is used and this requires a password to log in >> habopen. That’s a problem!
To get around this requirement I followed the recommended method to use rsa keys. These are generated as follows:
Create a directory to hold the key files. I think it needs full permisions
eg
/home/openhab/karaf_keys/
To generate the keys execute the following command while in the karaf_keys directory
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
Do not enter a passphrase when asked!!
Now we need to copy the keys, located in the .pub file, into a new line in this file… /srv/openhab2-userdata/etc/keys.properties
This is done as follows:
cat /home/openhabian/karaf_keys/openhab.id_rsa.pub
then copy the ouput so it’s in the buffer … just highlight it. You only need the long key part and not the rest.
Open the file
nano /srv/openhab2-userdata/etc/keys.properties
Add a new line as follows but replace the xxxx part with the copied key:
openhab= xxxxxxxxx,_g_\:admingroup
Save file
This is the script I created to restart the binding:
#!/bin/bash
ssh -p 8101 -i /home/openhabian/karaf_keys/openhab.id_rsa openhab@localhost bundle:restart org.openhab.binding.openwebnet
I put the script here
/etc/openhab2/scripts/restart_openwebnet.sh
That’s it. The rule and script should all work now.
To test the rule and that karaf can be accessed without password try the following to stop the binding. You will still need to enter the sudo password in the console for this test, but not the karaf password, habopen
sudo -u openhab ssh -p 8101 -i /home/openhabian/karaf_keys/openhab.id_rsa openhab@localhost bundle:stop org.openhab.binding.openwebnet
To test the script try this:
sudo /etc/openhab2/scripts/restart_openwebnet.sh
No passwords should be needed.
Or if you wait 30mins after stopping the binding the rule should detect that the binding is not online and automatically restart it.
M