Rule with triggeringItem

Hey guys.

I have a lot of Items called “Shelly_Licht_”
I need to send a command to other items calles “HTTP_Nachtmodus_”

Currently i have 20 different rules to achieve my goal. But i tried to do it in a more elegant way. So i tried to split the Name from an item to get and tried to sent a command to HTTP_Nachtmodus_

Unfortunately nothing really works.

rule "Shelly: Nachtmodi reaktivieren"
when
    Member of gShelly_Leuchten changed to 0
then
        var newitem = "HTTP_Nachtmodus_" + triggeringItem.state.toString.split('_').get(2)
        newitem.sendCommand(10)

end

triggeringItem.state.toString.split(‘_’).get(2) should be “Wohnzimmer”, when it comes from “Shelly_Licht_Wohnzimmer”
Now i want to send a command to an Item which name should be “HTTP_Nachtmodus_Wohnzimmer”

But it is not working and i am trying for several hours now to get this to work.
How to use a variable as an Item to send commands to?

newitem.sendCommand() is obviously wrong.

In the console i am getting this error:
Script execution of rule with UID ‘Shelly-2’ failed: Index 2 out of bounds for length 1 in Shelly

To explain it a bit further:

I want my Shelly Lights to dim to 10% at night, after i turned them off. But if i am sending Shelly_Licht_.sendCommand(10) the light turns on again.
I wanted the lights to stay off, when telling them their new brightness state for the night. So i used HTTP Binding to send ONLY the brightness, without turning the light on.

So i have 2 different items: Shelly_Licht_ to turn on/off and HTTP_Licht_ to tell the light its new brightness state.
This does work flawless. But i do have a lot of Shelly lights in my apartment and currently i am using a single rule for every light to do this. And it looks as following:

rule "Shelly: Nachtmodus in <room> reaktivieren"
when
    Item Shelly_Licht_<roomname> changed to 0 
then
    HTTP_Nachtmodus_<roomname>.sendCommand(10)
    }
end

But now to get my rules a bit cleaner, i am trying to do all this with one rule. Which should be possible. But due to my two different Items i am running into some problems.

I can’t help with your specific question, but I would suggest an alternative that doesn’t require HTTP. If you only ever have the lights set to specific brightnesses and never change them manually, have your lights set the brightness every time they turn on.

When my kitchen lights turn on, they immediately check what mode my system is in. If it’s Bedtime or Sleep, openHAB immediately adjusts the lights to 35%. Otherwise, they go to 75%.

rule "Kitchen Brightness"
when
    Item Dimmer_Kitchen_State changed from OFF to ON
then
    if (System_Mode.state == "Bedtime" || System_Mode.state == "Sleep")
    {
        Dimmer_Kitchen_Brightness.sendCommand(35)
    }
    else
    {
        Dimmer_Kitchen_Brightness.sendCommand(75)
    }
end

Since the dimmer ramps up the brightness, the adjustment is made before I notice any flickering (that might not be the case with 10% brightness). And again, this won’t work well if you have a tendency to change the brightness manually. In my case, I always want my kitchen lights at 35% or 75%.

With triggeringItem, this would be:

rule "Shelly: Nachtmodus in <room> reaktivieren"
when
    Item Shelly_Licht_<room1> changed from 0 or
    Item Shelly_Licht_<room2> changed from 0 or
    Item Shelly_Licht_<room3> changed from 0
then
    if (CONDITION_ITEM.state == ???)
    {
        triggeringItem.sendCommand(10)
    }
    else
    {
        triggeringItem.sendCommand(??)
    }
end

If you want, you could also add conditions to check whether or not the triggeringItem is already in the correct state, so that you don’t send unnecessary commands.

if (triggeringItem.state != 10) { triggeringItem.sendCommand(10) }

My problem is, that i am using some Shelly Plus to send ON or OFF to other Shelly Dimmers. So its not possible, since the Shelly Binding is no able to work with Plus models. But after many tries i found a way to do so:

rule "Shelly: Nachtmodi reaktivieren"
when
    Member of gShelly_Leuchten changed to 0
then
        sendCommand("HTTP_Nachtmodus_" + triggeringItemName.split('_').get(2), "10")
end

Its crazy, but works flawless.

I’m not sure why that matters. You’re triggering on the item changing to 0, and I just changed it to trigger on the item changing from 0.

Whatever the case, I’m glad you got it working. Looking at it again, I would just change the item names so that you only need to prepend HTTP_Nachtmodus_ to the original item:

Shelly_Licht_<roomname>
HTTP_Nachtmodus_Shelly_Licht_<roomname>

Then you don’t need to worry about splitting it.

In your original code, newitem is a string. You can’t do string.sendcommand.