Blockly - How to get a Number from a Dimmer item

Lately I have been making some small changes for my things and the model, to make use of more OH3 ui features. So for my heating, I wanted to make the little badge appear in the rooms, signaling that heating in that room is on.

After googling for the conditions of those badges, I saw that I need an item that is a “state” to be on or off. My homematic valve thing doesn’t have that, but it has an “opening level” as a dimmer item.

So, easy solution, create an item that gets set to “ON” whenever “opening level” is higher than zero.

Just for fun, I wanted to try to create this really simple rule in blockly, and here we are:

image

At first it didn’t even have the “else if” part, I just added it for debugging, because … whenever I run the rule, I end up in the “else” part.

==> /var/log/openhab/openhab.log <==
2021-02-25 18:05:24.901 [INFO ] [org.openhab.rule.7fe90d98e4         ] - 91

So my guess is, blockly isn’t treating this dimmer items as a number. I changed the item type to number, but besides the logfile output (0.91) nothing changed - probably because the channel is a dimmer as well.

Usually you would use item.State as Number to fix this. But how do I do this in blockly?

I couldn’t find a function for it. I tried to create a variable and set this to the item … same result and no other options for the variable operator.

I mean … I know that blockly is limited, I’m just a bit confused that it already fails me at this level?

Note: I tried to use the “getItemState” block first in the IF condition, but this puzzle doesn’t dock there …

For reference, this is the javascript code from the rule above:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


if ('HeizungKuche_Level' > 0) {
  events.postUpdate('HeizungKuche_Status', 'ON');
} else if ('HeizungKuche_Level' == 0) {
  events.postUpdate('HeizungKuche_Status', 'OFF');
} else {
  logger.info(itemRegistry.getItem('HeizungKuche_Level').getState());
}

Oh dear … just after posting I figured this out when I was trying to figure out why it won’t let me dock the getItemState puzzle into the if-condition. Reason is, that “getItemState” expects as String to be on the other side of the comparison operator.

But no way, that using the “bigger than” operator on a String “0” works?

Wrong … it hurts my eyes, but it works.

image

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


if (itemRegistry.getItem('HeizungKuche_Level').getState() > '0') {
  events.postUpdate('HeizungKuche_Status', 'ON');
} else if (itemRegistry.getItem('HeizungKuche_Level').getState() == '0') {
  events.postUpdate('HeizungKuche_Status', 'OFF');
} else {
  logger.info(itemRegistry.getItem('HeizungKuche_Level').getState());
}

Ouch … I’ll leave this thread here for future reference :exploding_head:

It does an alphabetical > comparison and since 1 comes after 0 alphabetically 1 > 0. It probably won’t work for negative numbers though, you’ll have to look into the ASCII/UTF-8 codes to see which one has the higher number. It also wouldn’t work with double digit numbers. For example > "20" would return false for "100".

There is a bug/limitation? in Blockly right now where it won’t let you put a get item state and a number value in the if. But if you first assign the get item state to a variable, you can use the variable and the number in the comparison.

Damn, second bug that I run into :sweat_smile: and I was close to the workaround, but stopped when I noticed that I don’t have other functions for the variable, either.

So this works, and hopefully works better than getting the ascii code :wink:

image

Edit: just saw there is already an issue for it as well