Detecting offline Things

[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!

3 Likes

Hello!

You could also use OpenHAB REST API to get things in JSON format, and then parse it to find offline things. This approach doesn’t require any external tool, since OH has HTTP binding and JSON transformation.

Best regards,
Davor

That would be much neater, but way too sophisticated for me to put together!

Hello!

It looks much more complicated to do it with an application I’m not familiar with. I haven’t tried to do this via OH rules, but I can try to test it when I find some free time, and post that rule here afterwards.

Best regards,
Davor

That would be fantastic

I was stumbling around until I found this post. Same issue as you. I’ve used Expect many years ago and completely forgot about it. Thanks

you’re very welcome!

Looks like with OH2.1 there is a proper way of detecting offline Things. Haven’t tried it yet as am insufficiently brave to use a snapshot!

Details: