[SOLVED] How to compare a value/variable against a bunch of numbers?

Hi, i have the following problem:

I want to compare a value/variable inside a rule to many numbers. If the value is the same like one of the numbers, then the rule should do something. Is this possible with only a variable - without a number-item?

With items i know the command “if xxx = member of group-name”, but with variables i don´t have any groups…

I have a rfid-reader and many rfid-chips. Now i want to do something inside a rule, when a known rfid-chip is recognized. How would you solve this?

Should i put all known rfid-numbers into number-items?

I actually wrote a DP for this exact use case. See Approach 3 in Design Pattern: Encoding and Accessing Values in Rules.

1 Like

Thanks! Works great in my first test.

@rlkoshak
Another small question to your DP:

I know this:

item.state shows the state of the item
item.name shows the name of the item

How can i get the description of the item?

String item-name "description of the item"

I want to make an output of the description in a logInfo inside my rule.

item.label

I think that’s one of those undocumented-but-works features

1 Like

Here is the part of my rule: What is the right syntax for using item.label in my variable-declaration?

I get the number of my rfid-chip.
My rfid-chips are stored as items like this

String RFID_12345 "rfid chip yellow"

And this is inside my rule:

	val itemName = "RFID_"+receivedCommand
	val itemLabel = ???

This gives me an error:

val itemLabel = itemName.label

Is it because i only can use items for this and no variables?

Your Item is named RFID_12345. So you’d access it’s label as RFID_12345.label

What your trying to do is indirection. You can’t just have a string “rhubarb” and attempt say “rhubarb”.state, expecting it to act like an Item.

There’s a least two ways to get hold of an actual Item object if you have it’s name string.

You can put possible Items in a group, and search the group by name. I prefer that method as it only uses standard rules features.

The other way is to use the REST API to fetch the named Item as though the rule were acting like a BasicUI.

Both methods are example here and in following posts

I already do this like rlkoshaks design pattern method #3

This works great, here is my whole rule:

rule "RFID Haustuer triggered"
when
    Item RFID_uid_haustuer received command
then
    val itemName = "RFID_"+receivedCommand
    //val itemLabel = itemName.label                             // This is the problem in this rule...
    val authorized = gRFID_Authorized.members.findFirst[ id | id.name == itemName ]
    val unauthorized = gRFID_Unauthorized.members.findFirst[ id | id.name == itemName ]
    
    // Purposefully putting unauthorized first so if an ID is in both Groups the unauthorized Group takes precedence
    if(unauthorized != null) {
			// alert attempted unauthorized access
			logInfo("RFID", "RFID Haustür UNAUTHORIZED ---> RFID_ID --> " + receivedCommand + " : ")
    }
    else if(authorized != null) {
        // unlock the door
			logInfo("RFID", "RFID Haustür AUTHORIZED ---> RFID_ID --> " + receivedCommand + " : " + itemName.label)
			if (Kontakt_EG_Haustuer.state == CLOSED) {
							Haustuer_Dummy.sendCommand(1)
			}
    }
    else {
			// alert attempted unkown access
			// do some stuff
    }
end

I want to display the label of the item in my logInfo --> So that i can see the real name of my rfid-chip and not only the item-name.

1 Like

Well, you’ve used findFirst to get hold of two Items (if the search worked) and you’ve chosen to call those Items authorized and unauthorized.

1 Like

Thanks! With authorized.label it works.

I didn´t try this, because my test with itemName.label failed…