[SOLVED] How to convert State String to Number in Blockly

I am trying to write a rule in Blockly where I retrieve the energy of a plug thing which I want to compare against a number

even though the item is defined as follows which should be a number

image

blockly only allows me to compare it to a string which to me feels wrong, doesn’t it?

Does anyone know why the state is perceived as a string only and how I can convert it into a string in blockly (if at all) ?

Thanks in advance,
Stefan

Actually, that’s not a plain old number. That;s a QuantityType that carries a Unit of Measurement (UoM). So it’s not just 123, it’s 124 kWh.

I suspect that Blockly does not know anything about UoM yet so this type of comparison may not be possible.

Even if the Item were a Number, it might not be able to handle the Number. I don’t know that. Blockly is still very basic and needs lots of work to become wholly viable.

Yes, Rich, I was 2 minutes too late comment as just noticed that myself, too. You are right. I just changed it to Number ONLY. Number:something is the default when you create an item in OH3.

However, even if I change the item to become Number only the comparison still doesn’t work in Blockly. It seams it wants to compare to Strings only which still feels weird to me and kinda unclean. Maybe it will do some casting in the background then? I’ll try that out in a moment and come back if it works. I first wanted to write the rule in DSL, so I know if it works in general and now I try to replicate it with blocky.

It’s often driven by the Channel so if you create the Item from the Channel or from the Model, it will select the UoM defined by the Channel.

Of course you can add UoM yourself but it might not always work if the Channel doesn’t support it.

As I indicated, Blockly is really basic right now. I would not be surprised if comparing Numbers like that is simply not yet supported. I’ve not played with Blockly enough yet to find all of it’s limitations. But I just clicked around in it and if you assign the Item state to a variable it will let you compare the variable to a Number from the Math set of blocks.

The code generated is:

var foo;


foo = itemRegistry.getItem('MyItem').getState();
if (foo > 123) {
}

I think that may work.

1 Like

Yep, pretty clever idea of you!

Here is the comparison:

results into:

watt = itemRegistry.getItem(‘ShellySteckdose_Leistung2’).getState();
if (watt > 5) {
}
if (itemRegistry.getItem(watt).getState() > ‘’) {
}

solved - thanks, Rich!
Stefan

1 Like

You might file an issue on the openhab-uis repo on this. Functionally there is no difference between assigning it to a variable first and comparing it to the state directly so I would call this a bug.

I find this being “lucky” :wink: - just kidding :joy:

Ok, I’ll put this onto my list for tomorrow.

Issue has been submitted: Blockly handles comparison differently when taken from state directly or from intermediate variable · Issue #836 · openhab/openhab-webui · GitHub

1 Like

Just for clarification, does it means finally that there are no way to convert a quantity type including UoM into a number in blocky script in order, for example to do some simple calculation as sum or subtraction and create a new number item?

You can strip of the UoM for example by applying a regex like in the following example and then compare the value.

see Blockly Transformations

Or you can always use an Blockly inline script to manipulate data directly with ECMA Script:

Thanks for the clarification.
Blockly is gives a nice help to program that seems user friendly rule but debugging stay difficult because you need to add some program just to debug…
I hope that soon, it will be possible to see the values of the results , or variables closer in the graphical program like we use to have for years in the PLC programming world…

Guess what coincidentally I have programmed PLCs for many years, so I exactly know what you mean but unfortunately that doesn’t work. Blockly and in general Rules cannot be compared to PLC “programs” which run in a cyclic loop and you can gather the context information and display it like done in FUP (Siemens graphical PLC language) on the screen. Rules are sequential programs that run on the server and the only way to debug it unfortunately is to insert log statements :smiley:

Thanks for your feedback… with the appearance of open programming interface using web browser as interface, we could expect it to appear… Node red has already the possibility to displays some status attached to the node…and make the debug easier. so I will continue to use nodered for rule définition rather than blocky .

Fair enough. I would love to have that functionality to. You are invited to implement that functionality to OH Blockly, Pierre.

btw, I have never worked with node red, so I checked it out today. From my perspective it is a completely different concept (not better or worse than the openhab rules which includes blocklies): while OH rules are only trigger by some particular event and only then runs, node red to my seems to in fact similar to what you know from PLCs: if I am not wrong it gathers the inputs all the time and then applies the sequence of nodes which is probably why it is easier to provide this real-time view of information next to the nodes (I saw it now today what you were actually requesting).

Theoretically while in the editor of the blocklies we could gather the current state of the items used but I am not if this would help or even confuse but the rule is a sequence of blocks with conditions and the like and code like this is usually single stepped to debug it.

I am not sure if you know the developer sidebar?

It is actually used to pin items to access them easily. I could image that the current state of that item could be also displayed here like a watch window in a debug but you had to ask @ysc here.

Also another option for you could be to use the “event monitor” in the sidebar that can show you all events, that come in which is a lot. However, can filter them with that little icon:
image

In this case I added the following filter: “openhab/items/F2_Office_Desk_Light_Scene” (you can add multiple in a comma separated way):

This is added to the side of the blockly rule and maybe that could help you.

1 Like

If you pin an Item you will see its current state and even be able to interact with it (e.g. toggle the switch). You can also navigate to the Item’s configuration page where you can change it if needed. It should be updated and show the state even as it changes on the server side.

The event filter is another way, as you demonstrate, and it’s the best way to see when an Item received an update to the same state. But in all other cases, it’s easier to just pin the Item.

Interesting and thanks for mentioning it. I am surprised that I didn’t see this above in my example:

image

Once you are done pinning, you need to hit “cancel”. It’s a bit counter intuitive I know. You can go back and pin more or remove them but until you hit “cancel” you are kind of in “edit mode”. Once you hit “cancel” you’re in use mode and you’ll see the current states and control widgets.

I’ll think about it a bit but it’s probably worth changing that “cancel” to “done”.

Yep, that’s I good idea. I need to keep that in mind. This should be an easy thing to find in the code and change, I would stay

Good to know! This will definitely make the things easier.
Regarding Nodered, the rules that I programmed there are also triggered by a change of the values read from Openhab … so this can work somehow also in an event manner.
But there is also the possibility to trigger the rule based on a one shot time stamp or in a cyclic mode like in a PLC.
So I need to go deeper in blocky using the developper sidebar.
What I find really nice with nodered is the possibility to display complex object in the debug window with the possibility to understand and explore the structure of the object by simple click in the window.
Using log or print instruction to debug will force me to convert the object content to string just to be sure of its content…

For example, if I read an item with nodered I can see immediately with a debug node if I get a string or a number or an array or a json object.
If I do the same in blocky, how can see the result of my action without print or log messages that are necessarily string ?
Is there any way to create some temporary local to the script item that I can explore using the developper sidebar?
Thanks for your feedback that are interesting also for other users .

Comment; Blockly is intended to be simple for beginners. More bells and whistles are available in other scripting languages.