Iterate throught String characters and get single charater

Hi,
I have a string Item, containing the TV channel to send to my TV.
Format is 401 or 5001

I would like to use a structure like For Each on all single digit of the string, and then send command
Inside the For Each cycle i need to put the single digit into a string variable.

I’m looking around on the forum but I’m stuck on this from a couple of days.

Many Thanks

I wrote something similar to blink lights to send a message using morse code.

1 Like

hi @rlkoshak
as usual thanks for your precius help.
This is my code:

ule "Imposta Canale Sky"
when
	Item SALOTTO_IR_IFTTT received command
then
    val sState = SALOTTO_IR_IFTTT.state.toString
	logInfo("IR.SKY.Channel","Ricevuto Canale Sky "+sState)
    
    var len = sState.length
    logInfo("IR.SKY.Channel","Lunghezza Canale Sky "+len.toString)

    var String digit = ""
    var int charIndex = 0
    while(charIndex < sState.length){
        digit = sState.toString.substring(charIndex,charIndex+1)
        logInfo("IR.SKY.Channel","index Ciclo / Digit da Inviare :"+charIndex.toString+" / "+digit)
        SALOTTO_IR.sendCommand("IR_SKY_"+digit)
        Thread::sleep(500)
        charIndex = charIndex+1
    }

end

If you run into trouble with your Rules stopping working, you will need to adjust this Rule to use Timers instead of Thread::sleep. As it is, a four character long command will keep this Rule running for more than two seconds.

hi @rlkoshak
i will check your example too for timer. I’m testing now with IR blaster, and I think I can reduce sleep to 300 ms

marco