Blockly - error for getting position of a text in item state

  • Platform information:
    • Hardware: Pi4
    • OS: Raspbian
    • openHAB version: 3.2.0
  • Issue of the topic: I’m trying to check if a specific text occurs in an item state. The item is defined as String and can also easily be logged out. But when calling the blockly version of indexOf an error is thrown.
    I’m first writing the item state into a variable and also logging it to see that this worked:
set activeDeviceName to get state of item SpotifyPlayerBridge_ActiveDeviceName
log error activeDeviceName

Then I would like to get the occurence of the text “Küche Heos” inb this string:

if in text activeDeviceName find first occurence of "Küche Heos" > 0
  • If logs where generated please post these here using code fences:
13:52:37.167 [ERROR] [org.openhab.rule.71ff468eac          ] - Bad Heos
13:52:37.183 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '71ff468eac' failed: TypeError: activeDeviceName.indexOf is not a function in <eval> at line number 8

After you have assigned your Item to your variable, and before you do your substring check, use the ‘append’ block from the Text section to append a zero length string to the variable.

This will force the variable into a string type.

See this example:

2 Likes

Thanks this worked, as I’m curious how can I find out what type if was before?

It will be a state type object. In DSL rules, we’d do a .toString to get a proper string, I don’t know a Blockly way apart from the kludge of appending zero-length string to force the same.

1 Like

Would it make sense to add something like get State-String to Blockly to simplify things?

My vote would be that Blockly returns the state itself by default, and not the object. I assume most beginners would also assume that the “get state” blocks actually return the state, not the object.

Is it possible behind the scenes for Blockly to interrogate the Item Type, and return the the state in the appropriate Blockly (JavaScript) type?

The Item state is an object - sometimes it is a string, sometimes it is a number, sometimes a quantity, an HSV value, or a grid reference. There are no “javascript types” for all those.

You know what I mean! :grinning:

We’d ideally get the expected value using the “state getting” blocks, without having to perform a kludge!

I think you run into a problem that lurks around in DSL rule editors; it is not possible to guess ahead of time exactly what sort of object myItem.state is going to be, there is a choice of dozens.

You could perhaps examine the current Item type, and accept that it will break if you later change things.

But even then, there are snags. A simple Switch type state could return an OnOffType, or an UndefType. How best to represent that in the javascript? Always a string, maybe.

So then doing the same with a Color type state … is “32, 56, 99” the best way to handle HSV values in javascript? Doubt it, I expect you’d have to start parsing and analyzing the simple string to do anything useful.
It might be nice to extract the components separately, say with getBrightness etc. method, … but then you need an object …

I fully agree there should be simple methods here, but do not see how to achieve that. Just dealing with quantities in Blockly seems fraught at this ime.

My idea was actually to provide an additional entry like “state string” here

and not some magic, where the user could intentionally chose to return the “toString()” version.

1 Like