Detect Item state type

I’m trying to use a Rule as a global function but I need to be able to detect if the value of ItemName.state is a String or a Number (to act accordingly)


ItemName.sendCommand(“String Here”)


String ItemName

I’ve tried this but with no luck:

if (ItemName.state instanceof DecimalType) {…

1 Like

What exactly do you want to achieve here?
Can you post the whole rule as it is and explain your ultimate goal?

I’m trying to create a sort of Global Function (to broadcast notifications) that other rules can call, and I want to be able to send either Strings (ie “Presence Detected”) or Numbers (ie 1). So the rule needs to know if the state received is a Number in order to map it to the respective string with a transform.

In short, I was not able to figure-out a way to detect if a string state contains just a number.

As far as I understand I can not create functions inside the rules.

Create 2 items, one a String item and the other a Number item

Create a rule where the trigger is one of these 2 items received command

In the rule select the source with the implicit variable triggeringItem

String myString
Number myNumber
rule "send notification"
    Item myString received command or
    Item myNumber received command
    if ( == "myString") {
        // Send String notification
    } else {
        // Send Number notification

Why go through this extra step? Why but just set the Item with the Stein in the first place?

There is. They are called lambdas. But if you stay to head down that path you usually end up with long brittle code that doesn’t work that well.

What you are trying to do is actually written up as a design pattern. Design Pattern: Separation of Behaviors

An alternative approach to what @vzorglub recommends (note the typo, it’s missing the second.= In the if statement) is

if(receivedCommand instance of DecimalType) //do number stuff

Thanks for the ideas! For my particular use case I ended up breaking the logic into two Rules, one (master) that only deals with Strings, and another that transform the Number into a String before forwarding to the master rule.