Rule return wrong item

Hi all,

I have been reading a lot and this is my first post so bare with me :slight_smile:

I am running OH2 stable release now for a while and getting into the more complex site of things. So here is my situation and problem:
I purchased two Fibaro door/window sensor 2. All is working fine. All it’s channels work and are displayed correctly on the sitemap. I now wanted to create a rule that sends me a Pushover message if the battery of this sensor is running low and needs replacement. Problem is, it keeps sending me the wrong Item name. Here is the (relevant) setup (sorry, half in Dutch)

// contact.items file
// Battery alarm
Switch deurVoor_batAlarm "Voordeur [MAP(]" <battery> (gHal, gBatteryAlarm, gHerstel)
Switch deurAchter_batAlarm "Achterdeur [MAP(]" <battery> (gWookamer, gBatteryAlarm, gHerstel)

// group.items file
Group gBatteryAlarm "Batterij vervangen" <battery>

// battery.rules
// Send pushover msg when battery receives low level
val logName = "pushover"

rule "Battery low level"


        Item deurVoor_batAlarm received command ON or
        Item deurAchter_batAlarm received command ON


        logInfo(logName, "Alarm received, checking devices...")
        val alarmItem = gBatteryAlarm.members.filter[s|s.lastUpdate("mapdb") !== NULL].sortBy[lastUpdate("mapdb")].last as SwitchItem
        logInfo(logName, "Device " + alarmItem + " received alarm. Creating pushover message...")

        val StringBuilder msg = new StringBuilder
        msg.append("De batterij van ")
        msg.append(" moet vervangen worden!")

        pushover("","", msg.toString, 1)
        logInfo(logName, "Pushover message sent")


// mapdb.persist
// Persists all items that need their value restored on startup
Strategies {
        default = everyUpdate

Items {
        // persist all items on every change and restore them from the db at startup
        gHerstel* : strategy = everyChange, restoreOnStartup

The rules is hacked together from this topic.

In the logs I can see:

2018-02-18 11:42:48.073 [INFO ] [org.eclipse.smarthome.model.script.pushover       ] - Alarm received, checking devices...
2018-02-18 11:42:48.203 [INFO ] [org.eclipse.smarthome.model.script.pushover       ] - Device deurAchter_batAlarm (Type=SwitchItem, State=NULL, Label=Achterdeur, Category=battery, Groups=[gWookamer, gBatteryAlarm, gHerstel]) received alarm. Creating pushover message...
2018-02-18 11:42:48.863 [INFO ] [org.eclipse.smarthome.model.script.pushover       ] - Pushover message sent

However, I manually tricked deurVoor_batAlarm and not deurAchter_batAlarm. So after sorting, it returns the wrong item. Any ideas where I go wrong? Any help appreciated!

Ok, it is solved by:


... **!= null** ...

As in the origional topic (That did throw an error all the time). and
Second part of the solution was to restart OH. Now it is all working.

I do have that a lot though, that I have to restart OH before anything takes effect or is working properly…

You can use 2 rules, one for each item and put the code into a lambda, with the item-name as argument.

Use the new triggeringItem implicit variable. It will get set to whatever Item caused the rule to trigger.

Replace the sleep and lastUpdate filter with:

val alarmItem = triggeringItem

This DP Design Pattern: Human Readable Names in Messages can be used to get nicer messages rather than using just the Item name.

Thanks Rich, works perfect! :smiley:

Hacking my own topic here. I want to update another switch with the name of the triggerItem. Say the name is ‘deurVoor’. I thought I can append that name with ‘_alarm’ as to update Item ‘deurVoor_alarm’ with a sendCommand. This doesn’t work however. I also tried:

switch {
    case deurVoor: deurVoor_alarm.sendCommand(ON)
    case ....

too but no luck. How can I achieve this?


sendCommand( "deurVoor_alarm", "ON")

sendCommand( + "_alarm", "ON")

Should work.

It does! Perfect :smiley:

I was so close myself with this but I had ON instead of “ON” (no quotes)…

Does the same work for

postUpdate( + "_alarm", new DateTimeType)

If my item would be a DateTime of course…

I’d like to add a timestamp to it as well

No only strings are allowed as parameter.
But you could get the toString of your DateTimeType. Some litaratur :smile: