[SOLVED] Working with Strings (Replacing Parts)

  • Platform information:

    • Hardware: Raspberry Pi 3
    • OS: current Openhabian
    • openHAB version: current unstable (2.4.0~20180721171026-1 (Build #1314))
  • Hey there,
    i want to shadow my house.
    My Idea in general:
    There are global and local items for triggering, the state and the possibillity of enabling the rule, for example to disable at night, or exclude some rooms.
    Im now stubling at working with strings.
    What i got so far:

Group Shadow_Trigger
Switch global_Shadow_Trigger        "Haus Abschatten"           (Functions) ["Switchable"]
Switch GF_Kitchen_Shadow_Trigger    "Küche Abschatten"          (Functions) ["Switchable"]
Switch GF_Living_Shadow_Trigger     "Wohnzimmer Abschatten"     (Functions) ["Switchable"]
Switch FF_Living_Shadow_Trigger     "Balkonzimmer Abschatten"   (Functions) ["Switchable"]

Group Shadow_Enabled
Switch global_Shadow_Enabled        "Haus darf abgeschattet werden?"            (Functions, Shadow_Enabled)
Switch GF_Kitchen_Shadow_Enabled    "Küche darf abgeschattet werden?"           (Functions, Shadow_Enabled)
Switch GF_Living_Shadow_Enabled     "Küche darf abgeschattet werden?"           (Functions, Shadow_Enabled)
Switch FF_Living_Shadow_Enabled     "Balkonzimmer darf abgeschattet werden?"    (Functions, Shadow_Enabled)

Group Shadow_State
Switch global_Shadow_State      "Haus abgeschattet?"        (Status, Shadow_State)
Switch GF_Kitchen_Shadow_State  "Küche abgeschattet?"        (Status, Shadow_State)
Switch GF_Living_Shadow_State   "Wohnzimmer abgeschattet?"   (Status, Shadow_State)
Switch FF_Living_Shadow_State   "Balkonzimmer abgeschattet?" (Status, Shadow_State)
rule "Abschattung global"
        Item global_Shadow_Trigger changed to ON or
        Item global_Shadow_Enabled changed to ON
        if(global_Shadow_Trigger.state.equals(ON) && global_Shadow_Enabled.state.equals(ON))
            Shadow_Trigger.members.forEach[ i | i.sendCommand(ON) ]

rule "Abschattung lokal"
        Member of Shadow_Trigger changed to ON
        val StringBuilder sb = new StringBuilder
        Shadow_Trigger.members.filter[ i | i.state.equals(ON) ].forEach[ i | 
            // sb."replace("Trigger", "State")" This is my problem.
               sendCommand(sb, ON) ]


As one can see, i want to replace the part of the name “Trigger” with “State” to work on. But I dont get it. Is there any way to do this, or do i have to use an other approach?
Thanks in advance for any hint! :slight_smile:

Maybe this helps a little bit

val String groupIdentifier = triggeringItem.name.substring(0, triggeringItem.name.lastIndexOf('_'))

var StringItem plantNameItem = GRP_PlantName.members.filter[ i | i.name == groupIdentifier+"_Name" ].head as StringItem
var NumberItem plantMoistureItem = GRP_PlantMoisture.members.filter[ i | i.name == groupIdentifier+"_Moisture" ].head as NumberItem
var NumberItem plantFertilityItem = GRP_PlantFertility.members.filter[ i | i.name == groupIdentifier+"_Fertility" ].head as NumberItem
1 Like

@Dibbler42: Could you please give me also examples of the members of the triggeritems and an out of it generated plantNameItem?

No Problem. below is the strukture of my plant items. The rule is then triggered by a moisture or fertility item which are organsized in an group

Group MO_Plant02 "MiFlora Planzensensor 02" <scene_plant> (GRP_MO_PlantSensors)

    DateTime MO_Plant02_LastUpdated  "MiFlora Planzensensor 02: Letztes Update [%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS]" <time_clock>         (MO_Plant02, GRP_PlantLastUpdated) 

    Number   MO_Plant02_Fertility    "MiFlora Planzensensor 02: Düngung [%d µS/cm]"                                   <scene_plant>        (MO_Plant02, GRP_PlantFertility)   
    Number   MO_Plant02_Moisture     "MiFlora Planzensensor 02: Feuchte [%d %%]"                                      <scene_plant>        (MO_Plant02, GRP_PlantMoisture)
    Number   MO_Plant02_Sunlight     "MiFlora Planzensensor 02: Sonnenstrahlung [%d lux]"                             <scene_plant>        (MO_Plant02, GRP_PlantSunlight)
    Number   MO_Plant02_Temperature  "MiFlora Planzensensor 02: Temperatur [%.1f °C]"                                 <scene_plant>        (MO_Plant02, GRP_PlantTemperature)

    String   MO_Plant02_Name         "MiFlora Planzensensor 02: Name [%s]"                                            <scene_plant>        (MO_Plant02, GRP_PlantName)
    String   MO_Plant02_Location     "MiFlora Planzensensor 02: Ort [%s]"                                             <scene_plant>        (MO_Plant02, GRP_PlantLocation)
    Number   MO_Plant02_Status       "MiFlora Planzensensor 02: Status [MAP(number2plantstatus.map):%s]"             <scene_plant>        (MO_Plant02, GRP_PlantStatus)

1 Like

My setup is working now:

rule "Abschattung lokal"
        Member of Shadow_Trigger changed to ON
        val String Trigger = triggeringItem.name.substring(0, triggeringItem.name.lastIndexOf('_'))
        var SwitchItem State = Shadow_State.members.filter[ i| i.name == Trigger+"_State" ].head as SwitchItem
        var SwitchItem Enabled = Shadow_Enabled.members.filter[ i|i.name == Trigger+"_Enabled"].head as SwitchItem

            sendCommand(State, ON)

Thank you, @Dibbler42