sendCommand and itemName from variable

This is working:


and this is not:

var String myItem = "myIphone"


Assuming that you defined an item “myIphone” somewhere in your .items file, in the first case you make reference to an object (items are specific objects that are defined in eclipse; these objects have other structures and methods attached to it) in the second to a string (which, granted, is an object too in java, but for all intends and purposes of this discussion, it is just a string of letters with nothing else to it).

Simply put:
an item myIphone “knows” (through methods) what a command is, how to send a command, and what it needs to do with such a command, etc; a string does not

Ok, but how do I get reference to item object based on the item name.

It is not really clear to me where you want to go with it, but if you want a way to make a more generic rule, you might be interested to read up on how to trigger behaviors over rules using groups:

1 Like

I have very simple rule that is monitoring list of bluetooth addresses. I’m trying to get this to work:

/ Detect bluetooth devices every minute
// chmod 4755 /usr/bin/l2ping

import java.util.HashMap

rule "Detect BT device"
        Time cron "0 0/1 * * * ?"
        var String output1

        val HashMap<String,String> devices = new HashMap<String, String>()
        devices.put("omadon_iphone5s", "1C:1A:C0:1D:46:9F")
        devices.put("j_phone",  "5C:2E:59:1F:C2:04")

        for (String key : devices.keySet() ) {
            output1 = executeCommandLine("/usr/bin/l2ping -c1 "+devices.get(key), 3000)
            if (output1.contains("received")) {
                logInfo("SYSTEM", "BT device "+devices.get(key)+": Found!!!")
            else {
                logInfo("SYSTEM", "BT device "+devices.get(key)+": Not found!!!")

And here sendCommand fails:

Error during the execution of rule Detect BT device: An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(java.lang.String,java.lang.String) on instance: null

Sory, will not be able to comment much, but still unclear what you want to achieve:
it appears the sendCommand is identical in both branches of the if statement, also not sure whether OPEN is supported, you may have to check your time definitions (depends on the item)
Unless you want to add many more items, your rule seems to have a lot of overhead for two devices.

Not that I would know how to code this, but I am sure there is a way to add your items to the hashmap and then you could get retrieve the instance of the item that sendCommand needs, rather than the string with the name

Last thought, on other posts on this forum, it is recommended to use rather:
item.sendCommand(Command) as it appears more reliable than the more generic method: sendCommand(item, Command)

Copy paste error. It is fixed now. The idea is to have hashmap containing item and mac address. Based on ping result I want to trigger switch item to OPEN or CLOSED. The problem is if I pass itemName as a string to the sendCommand this is not working because probably in this case he will try to set item named “key” and not the current value of “key”.

1 Like

I believe you are right, it looks to me that you need to find a way where you can actually pass a reference to the relevant instance of the object (item) to the line item.sendCommand(Command); a string is only a string and would not point to your object.
Even if it did work, it looks to me that the value of key is either “omadon_iphone5s” or “j_phone” to either you then want to send an open or closed command. You may need to doublecheck this, but I thought switches take ON and OFF as commands.

I used your advice about Working with groups in rules and rewrited my rule. Now it is working fine, but I would still like to know how to reference to item object without iterating over group members.


Group gMobiles
Group gBTdevices
Contact         omadon_iphone5s         "1C:1A:C0:1D:46:9F"     <apple>         (gMobiles, gBTdevices)
Contact         j_phone                 "5C:2E:59:1F:C2:04"     <android>       (gMobiles, gBTdevices)


// Detect bluetooth devices every minute
// chmod 4755 /usr/bin/l2ping
// log:set DEBUG org.eclipse.smarthome.model.script.BT

rule "Detect BT device"
        Time cron "0 0/1 * * * ?"
        var String output

        gBTdevices.members.forEach[ i | {
            output = executeCommandLine("/usr/bin/l2ping -c1 "+i.label, 3000)
            if (output.contains("received")) {
                logDebug("BT", "BT device "" ["+i.label+"]  Found.")
            else {
                logDebug("BT", "BT device "" ["+i.label+"]  Not found!!!")



Glad you got it to work,
As for your remaining question, maybe this post helps you a little further:

As a generalism, sending commands to Contact items makes little sense. You would normally use postUpdate.

You are right, fixed…