Blockly detect text in string channel item

Dear all, i am trying to detect a text in a string channel with blockly. But i am getting some errors.
Item EchoBuro_LetzterSprachbefehl is set by a string channel of the AmazonEchoControl Binding.


I am getting this error:

2023-07-27 08:46:00.482 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'EchoBuro_LetzterSprachbefehl' changed from stopp to spiel eins live
2023-07-27 08:46:00.490 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '6cf0271241' failed: TypeError: Sprachbefehl.indexOf is not a function in <eval> at line number 7

Can someone help?
Thank you!

I’m not using Blocky often, but something seems to be odd here with the index:
You are asking for the index of the first occurrence of the text “spiel” in the value of variable “Sprachbefehl” and then assign it to a variable named “spiel”. Is this your intention? So basically you are testing for existence of “spiel” in the command given.
May try to test for the index being larger than 0 instead.

Could you post the code generated by Blockly? This would make it easier to see what is behind the line number 7.

Hello, thank you for your answer. In the meantime i tried a little bit more and found a solution:

In case there is “spiel” in the item state a get a answer >0 and if there is no “spiel” in the state than i get the answer 0. I think its strange because the state is already a string. Dont know why i can to create a text but ok, its working now :slight_smile:

No, this is the expected behaviour.
You first code just retrieves the position (better: starting index) of “spiel” within a string.
It does not retrieve the string itself (which is not required and is performing better), thats the way firstOccurrence is working.
You are free to retrieve the complete string afterwards, but what sense would it make as you already know it would be “spiel” or an empty string, depending on the index you received…

Please use the help function for any of the blocks:


which sends you in this case to Text · google/blockly Wiki · GitHub