Smartapp in eventhandler comparing device name to input device name

in my preferences section i have this line:

input "motionKMS", "capability.motionSensor", title: "Kitchen Motion Sensor", required: true, multiple: false

i subscribe to motion active which is working fine. then in my event handler for that device i have this if check

if (event.device == motionKMS)

which is always false. but if i log.debug those 2 operands the string value logged seems to be the same.

what am i doing wrong?


They are not technically the same. Ive had the exact issue…you need to use a string comparison. So try:


And it should work.

Source: the internet:

To compare Strings for equality, don’t use ==. The == operator checks to see if two objects are exactly the same object. In Java there are many string comparisons:

String s = “something”, t = “maybe something else”;
if (s == t) // Legal, but usually WRONG.
if (s.equals(t)) // RIGHT
if (s > t) // ILLEGAL
if (s.compareTo(t) > 0) // also CORRECT>

1 Like

I think this also works:

if(event.device.displayName == motionKMS.displayName)

although since you can name a device anything (including an existing name) you should be comparing the device ID or network ID (both should be unique):

if( ==


if(event.device.networkId == motionKMS.networkId)

I believe that all of the above will be faster than the suggested use of equals() because they directly compare two strings without need to convert the entire device objects into strings.

YMMV :slight_smile:


thanks guys, that makes sense.

appreciate the tip about the IDs.

thanks again.

1 Like

Oh yeah…I didn’t even realize it was the device “object” when I wrote my reply. I’m so used to comparing actual literal strings in my devices (blahblah.equals(“Yes”)) that I didn’t notice. I mean it should work but probably isn’t the best way.

1 Like