Monitoring Things (Status)

Hi Community, gladly I give something back to the great Openhab Community. Maybe beneficial to someone… :blush:

Challenge:

The smarter the home, the more sensors and actors are in place. Unfortunately, the higher the probability a sensor/actor is not reachable or out of order. It can take a while until you find out that something is wrong and meanwhile your home behaves mad!

There is a need to be immediately notified when something brakes:

  • Monitoring of things status with an overview
  • Dashboard on your mobile with each things status (Online/Offline)
  • Notification when a thing goes offline
  • Easy to add new things to monitor (with little code)

Overview:

Dashboard:

Solution

For each thing to monitor a switch item in the items file is necessary. They need to belong to the group “gMonitor”. Their tag needs to correspond to the thingUID of the thing to be monitored.

Group gMonitor                          "Monitoring not yet working"    <error>           (Haus)
     
//Monitoring Items
    Switch        mFernsehlampe              "Hue Fernsehlampe [%s]"                       <switch>                (gMonitor)          ["hue:0220:001788b29832:1"]
    Switch        mNetatmo_Cellar            "Netatmo Keller Sensor [%s]"                  <switch>                (gMonitor)          ["netatmo:NAModule4:home:inside"]
    Switch        mKomfortlueftung           "Komfortlüftung [%s]"                         <switch>                (gMonitor)          ["modbus:poller:localhostTCP:p1066"]
    Switch        mKNX                       "KNX Gateway [%s]"                            <switch>                (gMonitor)          ["knx:device:bridge:generic"]
    Switch        mSmartMeterHouse           "Stromzähler Haushalt [%s]"                   <switch>                (gMonitor)          ["smartmeter:meter:house"]

The first rule checks every minute the status of the things and sets the corresponding switch item to ON when “ONline” / OFF when “OFFline”. If a thing can not be found an error is logged and the corresponding switch item is set to NULL. It also counts the number of offline things for an overview.

/// Monitoring Rules //////////////////////////////////////////////
rule "Monitor Things Status"
when Time cron "0 0/1 * * * ?"  
then
	gMonitor.members.forEach[ i | 
       	{var String vThingUID = i.tags.toString() // Reads the thingUID out of the tag
		 vThingUID = vThingUID.replaceFirst("\\[","")  // Brackets "[]"  in the string need to be removed ("\\" is escape for the RegeX)
		 vThingUID = vThingUID.replaceFirst("\\]","")  // Brackets "[]"  in the string need to be removed ("\\" is escape for the RegeX)
		 //logInfo("ThingStatus", "vThingUID: "+vThingUID)
	     try 
			{ 	var vThingStatus = getThingStatusInfo(vThingUID).getStatus()
			  	if ((vThingStatus.toString())=="ONLINE") 
				  	{  if (i.state!=ON) i.sendCommand(ON)
					} 
					else
					{ 	if (i.state!=OFF) i.sendCommand(OFF)
					} 
			} 
		 catch(Throwable t) 
		 	{	logError("Monitoring Things Status", "Thing: " + vThingUID + " not found / null")
			 	i.postUpdate(NULL)
			}   
		 finally {}
        }] //end forEach
		
		var vNumberofErrors = gMonitor.members.filter[s|s.state == OFF].size
		if (vNumberofErrors==0) 
			{ gMonitor.setLabel("OK - All Things Online")
			  gMonitor.setCategory("smiley")
			}
			else
			{ gMonitor.setLabel("Error - "+vNumberofErrors+" Things Offline")
			  gMonitor.setCategory("error")
			}
end

The second rule notifies when a thing went offline with a push message to the Openhab mobile app.

   rule "Notify when any gMonitor Member switched to OFF=Thing Offline"
   when Member of gMonitor changed from ON to OFF	//when any gMonitor Member switched to OFF=Thing Offline
   then
	  logInfo("Thing Monitoring",triggeringItem.label+" error - thing "+triggeringItem.tags.toString()+"went from online to offline!")
	  sendBroadcastNotification(triggeringItem.label+" error: Thing went offline!","error","high")
   end

The sitemap needs only one simple entry to show the content of the group
Group item=gMonitor

16 Likes

Please note that there is a " ` " in line 19 of the first rule . This one needs to be deleted before the rule is working ;o)

Kind regards
Jan

Thx Jan, corrected!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.