Monitor ChromeCast status in openHAB
I really like to monitor things, to catch issues early on. In our home we have a couple ChromeCasts, both Audio and Video. They are all connected to the openHAB system, Once in a while they stop working, and need to be restarted (unplugged and plugged in again). Unfortunately you usually only find that out when you want to stream something, and wonder why either the ChromeCast does not show up in the device list, or does show up but does not accept the media.
Therefore I decided to monitor the devices in openHAB.
That's quite easy, a rule can react on a status change of the Thing:
rule "ChromeCast Living Room status changed"
when
Thing "chromecast:chromecast:xyz123" changed
then
....
end
Inside of the rule any notification can be sent (email, Telegram, Pushover, ...).
However more often than not the ChromeCast will only go offline for a moment. The log looks like this:
2020-01-01 00:00:42.911 [WARN ] [su.litvak.chromecast.api.v2.Channel ] - Error while reading, caused by su.litvak.chromecast.api.v2.ChromeCastException: Remote socket closed
2020-01-01 00:00:42.915 [WARN ] [su.litvak.chromecast.api.v2.Channel ] - <-- null payload in message
2020-01-01 00:00:42.937 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'ChromeCast Living Room status changed': The name 'ChromeCastStatus' cannot be resolved to an item or type; line 97, column 9, length 16
There is a blib, and the rule is triggered and will send a notification. Then a couple seconds later the Binding reconnects to the ChromeCast and everything is fine again. It would be much better if the notification is only sent if the device stays offline for a certain time. This is possible by using a Timer and a Lambda function in the rule. It also requires two rules: one for triggering the timer when the device goes offline, one for canceling the timer when the device comes online.
// use a timer to avoid notifications for short outages
var Timer timerLivingRoom = null
rule "ChromeCast Living Room offline - timer"
when
Thing "chromecast:chromecast:xyz123" changed from ONLINE to OFFLINE
then
logInfo("ChromeCast Status", "ChromeCast Living Room offline")
if (timerLivingRoom === null) {
timerLivingRoom = createTimer(now.plusMinutes(1), [ |
// there was no cancel because the Thing never came online in the past minute
// ... send notification here
logInfo("ChromeCast Status", "ChromeCast Living Room is offline")
])
}
end
rule "ChromeCast Living Room online - timer"
when
Thing "chromecast:chromecast:xyz123" changed from OFFLINE to ONLINE
then
logInfo("ChromeCast Status", "ChromeCast Living Room is online")
if (timerLivingRoom === null) {
// recovered from longer downtime, > 1 minute
logInfo("ChromeCast Status", "Timer for chromecast:chromecast:xyz123 is already stopped")
} else {
logInfo("ChromeCast Status Debug", "Timer for chromecast:chromecast:xyz123 is running, stopping it")
timerLivingRoom.cancel()
timerLivingRoom = null
// no further notification required, was a short downtime
}
end
The "ChromeCast Living Room offline - timer" rule will create a timer and at the end of the timer execute the inline Lambda function. If the timer finishes and was not canceled before, the inline function will send the notification that the device is offline.
The "ChromeCast Living Room online - timer" has two jobs: if the timer is already expired, the downtime was for longer than a minute. Write this into the log. And if the timer is not yet expired, cancel the timer - because the device is online again and no notification is required.
Comments
Display comments as Linear | Threaded