OH3 from 2.5 Rule no longer seems to work?

Hi All

I used the following rule in OH2.5 for my alarm Pin Number input.

It appears that it no longer works and I cannot work out why for the life of me:

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

var registry = ScriptServiceUtil.getItemRegistry()

rule "Pin Input"
when
    Item Pin_Number_Input received update
then
    var update = Pin_Number_Input.state.toString()
    if (update == "") return;

    logDebug("Pin_Input", "{}: {}", Pin_Number_Input.name, update)

    var target = registry.get(Pin_Number_Input.label)

    if (target !== null) {
        var text = target.state.toString()
        if (text == "" || text == "NULL") text = "_"
        else if (!text.contains("_")) text += "_"

        if (update.length() == 1) {
                text = text.replaceFirst("_", update + "_")
         }
        else if (update == "DELETE") {
                text = text.replaceFirst("._", "_")
        }
        else if (update == "DONE") {
                text = text.replaceFirst("_", "")
                        createTimer(now.plusSeconds(10), [
                        Pin_Number.postUpdate("_")
//                      Pin_Number_Input.postUpdate("")
                        ])
        }

        //text = text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase().replace("  ", " ").split(" ").reduce([capitalized, word | capitalized + " " + word.substring(0, 1).toUpperCase() + word.substring(1)])

        if (text.contains("_")) target.postUpdate(text)
        else target.sendCommand(text.trim())
                    logInfo("", "EnteredPin " + Pin_Number.state  )
    }
//        createTimer(now.plusSeconds(10), [
//                            Pin_Number.postUpdate("")
//                            Pin_Number_Input.postUpdate("")
//        ])
 
end

Looks to me like the line:

 text = text.replaceFirst("_", update + "_")

May be the culprit as I am not seeing the Pin_Number_Input getting the concatenated input. It just stays on the single “key” pressed.

Any suggestions for a Non Coder?

Thanks
Mark

Logs? Errors? A description of what “no longer works” actually specifically means? What the rule is supposed to do?

Hi Rich

Sorry, I guess I could have been clearer.

I use the rule as the PIN Number input to arm/disarm my alarm.
The site map displays a keypad with numbers 0 to 9 as well as Done and Delete.

Each Number pressed goes to Pin_Number_Input and gets concatenated until done. Then updated to Pin_Number.

Not seeing any errors etc in the logs, but can see that the Pin_Number_Input is not getting the concatenated Pn Number. Each just gets each entry and then overwritten by the next (instead of concatenated)

Hope that makes more sense?

Mark

Add more logging and post the logs. Remember, we’re not computers either. We can’t “run” the code in our heads very well so we have to rely on you to provide as much information as you can. Log out all the variables at each step of the rule.

Thank you. Will do.

I have been using the Item States to see what I can see. So will just need to find some help with logging. Will get that done then post back.

Always appreciate your help.

So I have added a ton of logging and my rule now looks like this:

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

var registry = ScriptServiceUtil.getItemRegistry()

rule "Pin Input"
when
    Item Pin_Number_Input received update
then
    logWarn("Pin_Number", "Rule is running and registry variable is: {}",  registry)
    var update = Pin_Number_Input.state.toString()
        logWarn("Pin_Number", "Entered Pin Number update Variable is: {}",  update)
        logWarn("Pin_Number", "Entered Pin Number State is: "+  Pin_Number_Input.state.toString)

    if (update == "") return;

    logWarn("Pin_Number", "{}: {}", Pin_Number_Input.name, update)

    var target = registry.get(Pin_Number_Input.label)
    logWarn("Pin_Number", "target variable is: {}",  target)

    if (target !== null) {
        var text = target.state.toString()
        logWarn("Pin_Number", "text variable is: {}",  text)

        if (text == "" || text == "NULL") {text = "_"
        logWarn("Pin_Number", "text should be _ variable is: {}",  text)}
        else if (!text.contains("_")) {text += "_"
        logWarn("Pin_Number", "text 2 variable is: {}",  text)}

        if (update.length() == 1) {
                text = text.replaceFirst("_", update + "_")
         }
        else if (update == "DELETE") {
                text = text.replaceFirst("._", "_")
        }
        else if (update == "DONE") {
                text = text.replaceFirst("_", "")
                        createTimer(now.plusSeconds(10), [
                        Pin_Number.postUpdate("_")
//                      Pin_Number_Input.postUpdate("")
                        ])
        }

        //text = text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase().replace("  ", " ").split(" ").reduce([capitalized, word | capitalized + " " + word.substring(0, 1).toUpperCase() + word.substring(1)])

        if (text.contains("_")) target.postUpdate(text)
        else target.sendCommand(text.trim())
                    logWarn("Pin_Number", "EnteredPin " + Pin_Number.state  )
    }
//        createTimer(now.plusSeconds(10), [
//                            Pin_Number.postUpdate("")
//                            Pin_Number_Input.postUpdate("")
//        ])
 
end

And I get the following logs when I input the first number (1 in this case):

07:56:40.715 [WARN ] [.openhab.core.model.script.Pin_Number] - Rule is running and registry variable is: org.openhab.core.internal.items.ItemRegistryImpl@10ca435b
07:56:40.715 [WARN ] [.openhab.core.model.script.Pin_Number] - Entered Pin Number update Variable is: 1
07:56:40.730 [WARN ] [.openhab.core.model.script.Pin_Number] - Entered Pin Number State is: 1
07:56:40.730 [WARN ] [.openhab.core.model.script.Pin_Number] - Pin_Number_Input: 1
07:56:40.730 [WARN ] [.openhab.core.model.script.Pin_Number] - target variable is: {}

So looks like my issue is:

    var target = registry.get(Pin_Number_Input.label)
    logWarn("Pin_Number", "target variable is: {}",  target)

    if (target !== null) {

It seems target is null ? As rule seems to exit at this point?

Any ideas? This used to work on 2.5?
EDIT:

Found this article:

Which seems to indicate that I should be using:

val target = ScriptServiceUtil.getItemRegistry.getItem("Pin_Number_Input") //as GenericItem

Which gives me:

09:32:04.873 [WARN ] [.openhab.core.model.script.Pin_Number] - testItem variable is: Pin_Number_Input (Type=StringItem, State=1, Label=Pin Number Input, Category=, Groups=[Pin_Input])

However that causes the rules to get stuck in a loop.

Cheers
Mark

Managed to solve my problem.

Was using the Following as a basis:

And inadvertently changed the name of the group - which is being used to facilitate the rules operation. Pretty rookie move :frowning:

An item is used to receive keystrokes. This item must be in the “TextInput” group for the rule to use it. The target text item name is specified in the Label for this keypad item (not the best use of labels either)

But on the up side I learnt a whole lot about logging.

1 Like