Click here for system info
runtimeInfo:
version: 3.2.0
buildString: Release Build
locale: en-GB
systemInfo:
configFolder: /etc/openhab
userdataFolder: /var/lib/openhab
logFolder: /var/log/openhab
javaVersion: 11.0.13
javaVendor: Azul Systems, Inc.
javaVendorVersion: Zulu11.52+13-CA
osName: Linux
osVersion: 5.11.22-5-pve
osArchitecture: amd64
availableProcessors: 2
freeMemory: 61696984
totalMemory: 299892736
bindings:
- astro
- gpstracker
- http
- mqtt
- network
I have a simple Number Item in openHAB that stores the local outdoor temperature. I would like to compare it to a fixed number. I thought I’d be able to do the following:
But unfortunately the Maths block containing the “5” won’t fit into the space - it keeps popping out.
A workaround is to use a variable:
Or, to use a Text block, which feels wrong!
Presumably this is by design, and we’ll never allow a Maths number into that space?
Matze0211
(Matthias)
December 30, 2021, 10:11pm
2
The state of an item is always a string, therefore you can only compare string with string but not string with a number
Right, but it feels counterintuitive to perform mathematical comparisons on Strings.
And the example with the variable works, where the variable is a number, right?
(Note that the State of an Item in Blockly isn’t always a String …)
I’m trying to use Blockly as if I was an absolute beginner, which I think is who this is intended for (after the rule templates). It seems like a strange quirk that we’re exposing to these beginners.
This is far from being something new.
I opened up this issue quite a time ago when I wasn’t developing blockly myself and later when I joined the development I started to investigate my own filed issue because my intention was to solve the issue during the development of the new blocks - but as always things are not easy as they seem…
opened 04:25PM - 22 Jan 21 UTC
closed 05:16PM - 26 Dec 22 UTC
bug
main ui
## The problem
- when a state of type number (e.g a power item) is compared i… n blockly it is not treated as a number but a string instead
- when the item's state is assigned to a value and then the variable is compared it is treated as a number
See more details in thread https://community.openhab.org/t/solved-how-to-convert-state-string-to-number-in-blockly/113933/4
## Expected behavior
It is expected that a state value of type number can be directly compared against a number without intermediate assignment to a variable which to me even though working rather looks like a hack 😄
## Steps to reproduce
- Define a number item and then create the following rule in blockly

- in the above image the first comparison works against a number as it has been assigned to a variable before
- if the comparison is done line in the second if branch it only allows a string comparison (which does not make sense in this case)
## Your environment
Openhab 3.0.0
```yaml
generated javascript code from blockly:
var watt;
watt = itemRegistry.getItem('Shelly1Steckdose_Leistung').getState();
if (watt < 5) {
}
if (itemRegistry.getItem('Shelly1Steckdose_Leistung').getState() < '5') {
}
```
Please read what I learned in my own comment and also the reference I did there where Yannick also shared his insights.
PS: The topic was also mentioned here: Oh3 blockly: logic block compare item state with number - #6 by rlkoshak