Member of using Prefix and replace

Hello, I would like to apply a rule when a member receives a command, this rule will perform actions on items that always use the same “Prefix”, I tried to create a variable to do this of type String, in the log the rule works very well, on the other hand if I try to carry out a “.sendCommand” on it I have errors, my variable not being an object that stuck, how can I recover the variable “LastVol” which makes it the item “ChromecastBureau_LastVol” and make a “LastVol.sendCommand” the goal being to be able to create a variable
like “(prefix +” _LastVol “). sendCommand (ChromecastBureau_Vol as Number)”
what formatting to carry out to achieve this results?
Thanks for your help

Here the rule

rule "Group Google speech recieved bureau"
when
    Member of Ggh_Speech received command
then
var  prefix = ""
var  LastVol = ""
var triggertDevices = Ggh_Speech?.members
    
    triggertDevices.forEach [ i |
        prefix =  i.name.replace("_Speech", "")
        LastVol = i.name.replace("_Speech", "_LastVol")
        

    ] 

    logInfo("Prefix =", prefix)
    logInfo("Prefix =", LastVol)
    
       LastVol.sendCommand(ChromecastShannah_Volume.state as Number) 
end

En Anglais, s’il vous plait

This is an English-only user forum.

yes sorry i fix that quicly , thanks

1 Like

That’s just a string, it doesn’t have a sendCommand() method.

You seem to be re-inventing this -

There is an action
sendCommand(string, string)
that is simple to use

Thanks a lot, i try to do me best on it

I succeeded well with the “items” of type string, on the other hand if I change the “suffix” and the target is an “item” of type number no longer works, how to convert the original type " triggeringItem.name “which is a” String “to” triggeringItem.name.replace ("_ Speech", “_ Volume” "which is a Number? to be able to perform an action on it.

 rule "Group Google speech recieved bureau"
 when
     Member of Ggh_Speech received command
 then

    val name = triggeringItem.name
    logInfo("name =", name)    
    val Volume = triggeringItem.name.replace("_Speech","_Volume") //How to convert to Number ?
    logInfo("Volume =", Volume)
    // run ok ///
    //// creation d'une string avec la fonction "now.toLocalDateTime().toString()"////////
     val String DateBackupFinished = now.toLocalDateTime().toString()
     val DateTimeType MyDateTimeTypeFromString = DateTimeType.valueOf(DateBackupFinished)    
     postUpdate(triggeringItem.name.replace("_Speech","_Speech"), MyDateTimeTypeFromString.format("le %1$td du %1$tm à %1$tHh%1$tM"))
     postUpdate(triggeringItem.name+"_LastUpdate", MyDateTimeTypeFromString.format("le %1$td du %1$tm à %1$tHh%1$tM"))
    // Error bicause is a string ? ///      
    Volume.postUpdate(50)

 end


there is a way to do this ?
thanks again for your time

Volume is just a string, it has no postUpdate() method. We’ve been here already.

I don’t know what that means. Maybe it would help if you showed the error message.
postUpdate(stringname, stringstate)
doesn’t care what kind of Item you target, but the new state must be a string that can be parsed into something suitable for the Item type e.g."50"or “51.275” for a Number type.

ok now it work with this rule :slight_smile:

rule "Group Google speech recieved bureau"
 when
     Member of Ggh_Speech received command
 then

    val name = triggeringItem.name
    logInfo("name =", name)    
    val LastVol  = triggeringItem.name.replace("_Speech","_LastVol") 
    logInfo("Volume =", LastVol )
    
     
    // Work ok now ///      
    LastVol.postUpdate(ChromecastBureau_Volume.state.toString)

 end

The “ChromecastBureau_Volume.state.toString” is a string ? bicause i send it to “LastVol” and this one is a Dimmer, and it work

if i use “ChromecastBureau_Volume.state as Number” i have error “Type mismatch: cannot convert from Number to String”…but the “triggeringItem.name.replace(”_Speech","_LastVol" is a dimmer type

I can’t believe that rule works at all.

var x = "someItem" + "Name"
// x is just a string, "someItemName"

x.postUpdate("newState")
// is an error, strings don't have a postUpdate method

postUpdate(x, "newState")
// is okay, the Action wants two strings and will find the Item by name

postUpdate(x, 50)
// is an error, the Action wants two strings and 50 is not a string

postUpdate(x, "50")
// is okay, the Action wants two strings and "50" is a string

y = a + b * 2
postUpdate(x, y)
// is an error, the Action wants two strings and y is not a string

postUpdate(x, y.toString)
// is okay, the Action wants two strings

postUpdate(x, someOtherItem.state)
// is an error, the Action wants two strings and a state object is not (usually) a string

postUpdate(x, someOtherItem.state.toString)
// is okay, the Action wants two strings

1 Like

thanks for yourt help: so the rule work … i don’t know why but it is
here the frontail

2021-04-08 12:44:27.055 [INFO ] [org.openhab.core.model.script.name =] - ChromecastBureau_Speech

2021-04-08 12:44:27.057 [INFO ] [g.openhab.core.model.script.Volume =] - ChromecastBureau_LastVol

2021-04-08 12:44:27.060 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule 'ChromeCast-3' is executed.

2021-04-08 12:44:27.063 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ChromecastBureau_LastVol' changed from 35 to 15