[edit: looks like there is now a way to detect offline Things properly - see last post in this thread; further edit - code corrected, as with OH2.2 snapshot we need “smarthome:things list”]
openHAB 2 currently doesn’t provide any way of detecting if any Things have gone offline. I understand this is something the devs are working on, but have put together something quick and dirty that does the job for now, and notify me if anything goes offline.
This uses an Expect script file to access the Karaf console, count the number of Things showing as offline, and then pass that back to openHAB. I then have a daily cron rule calling the expect script, logging the result, and notifying me if necessary.
If you don’t already have Expect then simply sudo apt-get install expect. This is of course only for Linux - I understand there is Expect for WIndows, but I know nothing about it.
Expect script as follows:
#!/usr/bin/expect
set timeout 10
set countoffline 0
spawn ssh -p 8101 openhab@localhost
expect "Password: "
send "habopen\r";
expect "openhab>"
send "smarthome:things list\r";
expect {
"Status=OFFLINE" {incr countoffline; exp_continue}
"openhab>" {
exec echo offline count &;
exec echo $countoffline &;
send "logout\r";
exec wget http://localhost:8080/classicui/CMD?OfflineThings=$countoffline -O /tmp/offline ;
}
}
Simple openHAB items file:
Number OfflineThings "Things offline: [%d]" <energy>
And then all that remains is a simple cron rule to run the script (which I have in /etc/openhab2/utils) and process the result:
// Daily system check
rule "Daily check non-criticals - 11.06am so doesn't clash with other functions"
when
Time cron "0 6 11 ? * *"
then
// I have a bunch of other checks here
// Check for any offline Things
logInfo("DAILY-CHECK", "Checking for any offline Things - will then pause for four seconds")
executeCommandLine("/etc/openhab2/utils/offline-check.exp")
Thread::sleep(4000)
if (OfflineThings.state > 0) {
logInfo ("DAILY-CHECK","Offline things: {} - sending email alert", OfflineThings.state)
sendMail("[my email]", "Automation: Things offline", "Have detected " + OfflineThings.state + "T hings offline. Please access PaperUI for further details")
} else {
logInfo ("DAILY-CHECK", "No offline things")
}
end
This is really quite basic, but probably sufficient for my purposes. It should be easy to expand - one could even create an openHAB item for the online/offline status of each Thing, and then check each one off separately in the Expect script.
Any other improvements/suggestions gratefully received!