Assistance with converting old scripts to OH3 rules

Hello, I used to have this script that updated the status of my door lock from ‘Alarmraw’ event.

I am aware that the function ‘triggeringItem’ has changed now. I do not know how to convert this to the new script standard.

import org.openhab.core.model.script.ScriptServiceUtil

rule "Lock: Update lock states after alarm_raw event"
when
    Item EntryLock_AlarmRaw received update
then
    val actionItem = ItemRegistry.getItem(triggeringItem.name.toString.replace("_AlarmRaw",""))
    logInfo("Rules", "Lock: Alarm events: {}=[{}]",actionItem.name,triggeringItem.state.toString)
    val StringBuilder message = new StringBuilder(actionItem.name)
    switch (transform("JSONPATH","$.type",triggeringItem.state.toString)) {
        case "ACCESS_CONTROL" : {
            switch (transform("JSONPATH", "$.event", triggeringItem.state.toString)) {
                case "1": {
                    actionItem.postUpdate(ON)
                    message.append(" updated to ON (locked)")
                }
                case "2": {
                    actionItem.postUpdate(OFF)
                    message.append(" updated to OFF (unlocked)")
                }
                case "11" : {
                    actionItem.postUpdate(OFF)// jammed could mean unlocked, so I do this for safety... you may want to send an alert here too
                    message.append(" is jammed, so setting lock to OFF (unlocked)")
                }
                default : {
                    message.append(" unknown door lock Event, ").append(triggeringItem.state.toString)
                }
            }
            if (transform("JSONPATH", "$.event", triggeringItem.previousState(true).state.toString) == "11" && transform("JSONPATH", "$.event", triggeringItem.state.toString) != "11") {
                message.append(" and is no longer jammed")
            }
        }
        case "BURGLAR" : {
            message.append(" has detected an intruder")
        }
        case "POWER_MANAGEMENT" : {
            message.append(" received a Power Management alarm, ").append(triggeringItem.state.toString)
        }
        default : {
            message.append(" received and unknown Type in alarmRawParser, ").append(triggeringItem.state.toString)
        }
    }
    logInfo("Rules", "Lock: Alarm events: {}", message.toString)
end

Edit: I am converting this to OH3 DSL Script. The example below shows only one item updates but I am keeping it general since I have multiple items that uses the same script. Currently, i get this error in OH3: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null

    val actionItem = triggeringItem.name.replace("_AlarmRaw","")

Do I change everything that has triggeringItem.name.toString to triggeringItem.name.?

Your rule can only be triggered by one Item. You can use that directly.as
EntryLock_AlarmRaw.name

or even just do
val actionItem = ItemRegistry.getItem("EntryLock")

or even just use
EntryLock
directly instead of actionItem e.g.
EntryLock.postUpdate(ON)

I have multiple locks thus I wanted to use a generic name. In the specific example I have just one triggering item.

Okay, you no longer get a triggeringItem implicit variable in OH3 DSL rules unless you have a Member of type trigger.
So for multiple lock Items you can put them in a Group and have one rule operating from Member of.

If your rule triggers from ordinary Item xxx, then you get triggeringItemName implicit variable. This is new and you might need OH3 Milestone1 or something.

How do I create virtual switches that are triggered by rules?

Create an item as you did with your other items and don’t link it to a channel.
Creating is possible via the blue + symbol in the lower rigth corner of the items page or via the subpage “Add items via textual definition” which takes the syntax from the .items files of OH2.

1 Like

Hallo,kann man diese virtuelle Switch auch im Modell verwenden, und wenn ja wie?

I created the virtual switches and can see them in the Items list.

How do I use the virtual switch in a Rule? I can’t add it to a Model so it doesn’t show up in the Rules “Item Event” and it doesn’t show up as a “Thing” in the Thing list.

It’s an Item, like any other Item. You can send commands or post updates from a rule.

A Thing is a Thing and an Item is an Item, expecting an Item in a Thing list is the failure!

So neither response actually answered the question.

I can see the switch in the ITEM list. When I go to add an ACTION under the Rules, I select ITEM Action and it is looking for ITEMS in the MODEL definition.

So, I go to the MODEL definitions, and I can only add THINGS.

Try to check the Show non-semenatic on the lower bound of the sub-window. If your virtual item is member of a group you need to open this group to see your item.

Little late on the update, I resolved the a while back. Putting the code as a reference to anyone who needs a example.

val actionItem = triggeringItem.name.replace("_AlarmRaw","")
    logInfo("Rules", "Lock: Alarm events: {}=[{}]",actionItem,triggeringItem.state.toString)
    val StringBuilder message = new StringBuilder(actionItem)
    switch (transform("JSONPATH","$.type",triggeringItem.state.toString)) {
        case "ACCESS_CONTROL" : {
            switch (transform("JSONPATH", "$.event", triggeringItem.state.toString)) {
                case "1": {
                    actionItem.postUpdate("ON")
                    message.append(" updated to ON (locked)")
                }
                case "2": {
                    actionItem.postUpdate("OFF")
                    message.append(" updated to OFF (unlocked)")
                }
                case "11" : {
                    actionItem.postUpdate("OFF")// jammed could mean unlocked, so I do this for safety... you may want to send an alert here too
                    message.append(" is jammed, so setting lock to OFF (unlocked)")
                }
                default : {
                    message.append(" unknown door lock Event, ").append(triggeringItem.state.toString)
                }
            }
            if (transform("JSONPATH", "$.event", triggeringItem.previousState(true).state.toString) == "11" && transform("JSONPATH", "$.event", triggeringItem.state.toString) != "11") {
                message.append(" and is no longer jammed")
            }
        }
        case "BURGLAR" : {
            message.append(" has detected an intruder")
        }
        case "POWER_MANAGEMENT" : {
            message.append(" received a Power Management alarm, ").append(triggeringItem.state.toString)
        }
        default : {
            message.append(" received and unknown Type in alarmRawParser, ").append(triggeringItem.state.toString)
        }
    }
    logInfo("Rules", "Lock: Alarm events: {}", message.toString)