Fire alarm trigger and notification

Embarrassingly easy. Thanks a lot. Now it works!

I tried to adjust the rule to make an alarm system using my door contact sensors. Unfortunately it does not execute.
This is the rule:

rule "Einbruch!"
when
    Item gDoors changed to OPEN             // Trigger when a door is opened

then
	logInfo("FILE", "A door was opened")
    // If the alarm system is not activated, exit the rule
    if(AlarmSystem.state != OFF) return;
	
	logInfo("FILE", "And the alarmsystem was activated")
    val List<GenericItem> Break_In = newArrayList

    // Get all the Doors that have been opened
    gDoors.members.filter[d|d.state == OPEN].forEach[d|Break_In.add(d as GenericItem)]

    // Sort them to be in the order they were most recently changed, oldest alarm listed first
    val sorted = Break_In.sortBy[lastUpdate]

    val message = new StringBuilder()

    // Build up the message listing the oldest alarms first and most recent last.
    message.append("Einbruch in: ")

    sorted.forEach[i|
        if(i instanceof NumberItem) message.append(transform("MAP", "alarmanlage.map", i.name) + " = " + i.state.toString + ", ")
        else message.append(transform("MAP", "alarmanlage.map", i.name) + ", ")
    ]
 	message.delete(message.length - 2, message.length) // delete the extraneous ", "
    // send message
    sendTelegram("Jonas", message.toString)
    sendTelegram("Johanna", message.toString)
    // trigger alarm
  	gLights?.members.forEach[l|l.sendCommand(ON)Thread::sleep(500)]
end

And this the error message:

An error occurred during the script execution: Unhandled parameter types: [null, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@4e7cd7, org.eclipse.xtext.util.CancelIndicator$1@4560ce]

Any ideas what is wrong?

Add tons of logging statements to see what line it is failing on. From there review all the objects that are used on that line. I bet one of them is null.

All,

I have been following this thread and tried my best to adapt my rule as described. I´ll be using temperature sensors and would like to send an mail to a Pushover mail, if temperature >45 degrees Celsius is being measured.

For whatever reason its not accepting the rule:

Can you please advise?

Thanks,
db

rule "Feueralarm Waschküche"

when 

Item zwave_device_5d01b5c9_node12_temperature changed

then

if (zwave_device_5d01b5c9_node12_temperature.state > 23,0) and
sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")

end

It is improper syntax. Also, ALWAYS post the errors in the logs.

I recommend using the VS plugin or ESH Designer to write rules. It will tell you when you have syntax errors.

rule "Feueralarm Waschküche"
when 
    Item zwave_device_5d01b5c9_node12_temperature changed
then

    if (zwave_device_5d01b5c9_node12_temperature.state as Number > 23.0) {
        sendMail("XXX@pomail.net","**Feueralarm Test!**","**Feueralarm Test!**")
    }
end

Errors:

  • you probably have to cast the state of your Item to a Number to make the comparison to 23
  • you must use decimal notation, not comma notation for numbers
  • the “and” after the if statement is nonsensical, you use { } to define the code that gets executed when the condition is true

In addition to using ESH Designer I highly recommend reading the Rules page and review examples.

Thanks for this code. Now my alarm tells me in detail, which windows are still open - and not only “at least one door or window is open”

But isn’t it possible to get the label of the item, that it is not needed to do it with MAP-Transform? Or can you only get the state or value of an item (e.g. Open, closed, On/off, 24.6 °C, …)?

I don’t know if this still works on recent releases

Yes assuming you label already matches what you want to say in your alert message. In most cases, in my experience, the text I want as part of my UIs is different from the text I’d want to use in an alert.

1 Like

Dear Rich, I have made the move to Openhab 3.2 and my alarm rule is not working anymore. I am not a programmer but get around with my basic knowledge, adapting existing rules, and thanks to helpful people such as yourself.
I am quite intrigued by Blockly as this is also something that I could show my kids and they could design their own lighting, wakeup, etc. rules. Should be a good way to start.
So I am wondering if the rule below can be programmed in Blockly, specifically the list generation, filtering, and building the message? Thanks!

rule	"Alert when doors left open"
when
	Item	AlarmSystem changed to ON
then
	if (gDoors_JoJo.state != OPEN) return;
	
	val List<GenericItem> OpenDoors = newArrayList

    // Get all the Doors that have been opened
    gDoors_JoJo.members.filter[d|d.state == OPEN].forEach[d|OpenDoors.add(d as GenericItem)]

    // Sort them to be in the order they were most recently changed, oldest alarm listed first
    val sorted = OpenDoors.sortBy[lastUpdate]

    val message = new StringBuilder()

    // Build up the message listing the oldest alarms first and most recent last.
    message.append("The following doors are still open: ")

    sorted.forEach[i|
        if(i instanceof NumberItem) message.append(transform("MAP", "alarmanlage.map", i.name) + " = " + i.state.toString + ", ")
        else message.append(transform("MAP", "alarmanlage.map", i.name) + ", ")
    ]
 	message.delete(message.length - 2, message.length) // delete the extraneous ", "
    // send message
    sendNotification("x@y.com", message.toString)

I believe everything you need us there in Blockly based on a quick scan of the rule. But if you run into something lacking we can maybe add it or provide an alternative. Give it a shot and see would be my suggestion.

Ok, this is more difficult than I thought:
I can create a list of all items in the group gDoors_JoJo and print the content in the log for testing.
Blockly list 1
However, I don’t see any option to filter and sort the list. There are only blocks to sort numerically and alphabetically or return an item from a defined position in the list if I am not mistaken. I tried building in that last option just to see how I would have to add an additional operation but that fails with

cannot read property "indexOf" from undefined in <eval> at line number 16

Something like this should work to filter the list.

After that “MyList” will contain only the members of AllLights that are ON.

Sorting is indeed going to be limited to numerical or alphabetical sorting. At some point a requirement is going to become too complex to handle in blockly. It’s probably not feasible to offer blocks for everything. However, over time more libraries will be published to the marketplace which might include sorting options like that.

Be sure to click on any option that has a little down triangle for additional options.

You don’t need to create a list with the members of the Group because that is already a list. By doing it the way you tried you end up with a List that has one element which is itself a list.

Thanks Rich, hand happy new year!
I built the rule based on your suggestion and now I get this java error for any items in the group I am using in the rule:

java.lang.RuntimeException: org.openhab.core.items.ItemNotFoundException: Item 'Shelly25LivingRoom_Relay1_Output (Type=SwitchItem, State=ON, Label=Light JoJo LivingRoom Sideboard, Category=light, Tags=[Switch, Light], Groups=[gGF_LivingRoom, gLights_JoJo_GF, gLights_JoJo_PresenceSim])' could not be found in the item registry

Any ideas?

That error usually occurs when using the"get item state" block with a full Item Object instead of just the Item’s name. I purposely did not use that block for that very reason.

Got it, that issue is indeed solved now. Is there a way to use transformation or something similar to make the list readable? Now the output is very verbose:

Shelly25LivingRoom_Relay1_Output (Type=SwitchItem, State=ON, Label=Light JoJo LivingRoom Sideboard, Category=light, Tags=[Switch, Light], Groups=[gGF_LivingRoom, gLights_JoJo_GF, gLights_JoJo_PresenceSim])

There is a block that says “get name of item” The"name" part can is a suggestion where you can select all the different parts of the Item which you can log individually.

Great, it is working now:


Ideally, I could also truncate the labels as they are inserted into the list but that is not crucial.
Thanks!

There are string manipulation blocks available for that.

Yes, I found the “in text … get substring from letter # … to letter # …” I guess this would have to be part of the loop (maybe in place of the “get label of item i” but there it does not fit.

Under Lists there is also “make list from text with delimiter” to split a string and then you can use the list operations to extract and rebuild the string you want.