[SOLVED] Intelligent Rule for E-mail notification for online/offline devices

OH 2.4 on RasPI using Openhabian.

Hm - I hope that is the best title here.
So basically I want to get an E-Mail when the status of an item representing a device being online/offline.
The items for that are defined as follows:

//Online status of switches / plugs
Switch Plug_Garden_TerraceLeft_Ping				    "Steckdose Terrasse links [MAP(network.map):%s]"	<zwaveonline>	(gDeviceOnlineState)
Switch Illumination_GroundFloor_LivingRoom_Ping	    "Licht Wohnzimmer [MAP(network.map):%s]"			<zwaveonline>	(gDeviceOnlineState)
Switch Valve_Garden_Left_Ping					    "Ventil Johannisbeeren [MAP(network.map):%s]"		<zwaveonline>	(gDeviceOnlineState)
Switch Illumination_Garden_Blind_Ping			    "Licht Markise [MAP(network.map):%s]"				<zwaveonline>	(gDeviceOnlineState)
Switch Pump_Basement_Circulation_Ping			    "Zirkulationspumpe [MAP(network.map):%s]"			<zwaveonline>	(gDeviceOnlineState)
Switch Door_GroundFloor_Terrace_Ping			    "Terrassentüre [MAP(network.map):%s]"				<zwaveonline>	(gDeviceOnlineState)
Switch Illumination_GroundFloor_Office_Ping         "Licht Arbeitszimmer [MAP(network.map):%s]"         <zwaveonline>   (gDeviceOnlineState)
Switch Illumination_Kitchen_GroundFloor_Ping        "Licht Küche [MAP(network.map):%s]"                 <zwaveonline>   (gDeviceOnlineState)
Switch Illumination_KitchenBoard_GroundFloor_Ping   "Licht Küchenzeile [MAP(network.map):%s]"           <zwaveonline>   (gDeviceOnlineState)
Switch Thermostat_Dining_GroundFloor_Ping           "Thermostat Esszimmer [MAP(network.map):%s]"        <zwaveonline>   (gDeviceOnlineState)

//Online status of smoke detectors
Switch SmokeDetector_GroundFloor_LivingRoom_Ping	"Rauchmelder Wohnzimmer [MAP(network.map):%s]"			<zwaveonline>	(gDeviceOnlineState)
Switch SmokeDetector_GroundFloor_Entrance_Ping		"Rauchmelder Flur (EG) [MAP(network.map):%s]"			<zwaveonline>	(gDeviceOnlineState)
Switch SmokeDetector_2ndFloor_SleepingRoom_Ping		"Rauchmelder Schlafzimmer (DG) [MAP(network.map):%s]"	<zwaveonline>	(gDeviceOnlineState)
Switch SmokeDetector_2ndFloor_Entrance_Ping			"Rauchmelder Vorzimmer (DG) [MAP(network.map):%s]"		<zwaveonline>	(gDeviceOnlineState)
Switch SmokeDetector_2ndFloor_KidsRoom_Ping			"Rauchmelder Kinderzimmer (DG) [MAP(network.map):%s]"	<zwaveonline>	(gDeviceOnlineState)

Along with the group gDeviceOnlineState:

Group:Switch:OR(OFF,ON)	        gDeviceOnlineState	"Geräte offline [%d]"			<network>		(gRestoreOnStartup)

My current rule is, to send an E-Mail when this group gets updated:

rule "Device online state changed"
when
    Member of gDeviceOnlineState changed
then
    val StringBuilder ReportString = new StringBuilder
    val OnlineDevices = gDeviceOnlineState.members.filter[ i | i.state==ON] 
    val OfflineDevices = gDeviceOnlineState.members.filter[ i | i.state==OFF] 

    if(OnlineDevices !== null && OfflineDevices !== null)
    {

        ReportString.append("The state of the item " + triggeringItem.name + " has changed to " + triggeringItem.state + "\n\r")

        ReportString.append("The following devices are online:\n\r")
        OnlineDevices.forEach[i|
            ReportString.append(i.name + "\n")
        ]

        ReportString.append("\n\r\n\rThe following devices are offline:\n\r")
        OfflineDevices.forEach[i|
            ReportString.append(i.name + "\n")
        ]

        sendMail("some e-mail adress", "openhab device connectivity changed", ReportString.toString)
        logInfo("device state", ReportString.toString)
    }
end

So I am enumerating Online and Offline devices in different lists and create a string that then is sent via E-Mail.
This string looks like:

The state of the item Sonoff4CH_Garden_RaisedBed_Ping has changed to OFF

The following devices are online:

Plug_Garden_TerraceLeft_Ping
Illumination_GroundFloor_LivingRoom_Ping
Valve_Garden_Left_Ping
Illumination_Garden_Blind_Ping
Pump_Basement_Circulation_Ping
Door_GroundFloor_Terrace_Ping
Illumination_GroundFloor_Office_Ping
Illumination_Kitchen_GroundFloor_Ping
Illumination_KitchenBoard_GroundFloor_Ping
Thermostat_Dining_GroundFloor_Ping
SmokeDetector_GroundFloor_LivingRoom_Ping
SmokeDetector_GroundFloor_Entrance_Ping
SmokeDetector_2ndFloor_SleepingRoom_Ping
SmokeDetector_2ndFloor_Entrance_Ping
SmokeDetector_2ndFloor_KidsRoom_Ping

The following devices are offline:

MoistureRaisedBed_SensorState
Sensor_Gateway_Garden_Ping
Sonoff4CH_Garden_RaisedBed_Ping
Denon_Reciever_Ping

So everything is working, but my problem appears e.g. when Openhab boots and the devices change their states one after the other. My Inbox then looks like:

So after this long explanation i’d like to know if theres an elegant way to handle this?
I think of two possibilities:

  • Ignore this rule during startup (how can I achieve this)?
  • Have an algorithm that only sends an E-Mail when either a minimum amount of updates to that group has been fired or if the last E-Mail was more than x minutes ago.

Any suggestions are appreciated - sometimes i’m going to make things more complicated than needed :slight_smile:

Hi Sascha,

i haven’t worked with rules that much, but one possible solution that comes to mind is creating a timer
that checks if in a given time frame (see your logs for how long it takes after a restart untill all items have the correct state) more items change their status.
I think there are several examples of timers within rules

Regards
Raven

1 Like

I have done this in the past like this:
item

Switch StartUpSwitch

rule

rule "Start up switch"
when
    System started
then
    StartUpSwitch.postUpdate(ON)
    createTimer(now.plusMinutes(2), [ |
        StartUpSwitch.postUpdate(OFF)
    ])
end

The StartUpSwitch will be ON for 2 minutes after start up.
You can check you this in any rule

1 Like

@vzorglub
Thanks a lot Vincent. That’s a very simple approach and its working perfect
I did correct two typos :wink:

The trigger is

System started

And the Item should be called

Switch StartUpSwitch

Cool thanks, Sorry about that
Corrected above