# Oh3 blockly: logic block compare item state with number

**URL:** <https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517>\
**Category:** Scripts & Rules\
**Tags:** oh3, blockly\
**Created:** [February 8, 2021, 10:03pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517 "2021-02-08T22:03:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![johannesbonn](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@johannesbonn](https://community.openhab.org/u/johannesbonn)\
**Post date:** [February 8, 2021, 10:03pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/1 "2021-02-08T22:03:25Z")

</div>

Hello,  
I am using oh 3.1 m1 and make my first steps with blockly. I try to compare the item state (number item) with a number from the math block in an if statement, but I can not link the Number block to the inequality block (see first picture). A text block is no problem (2. picture) but doesn’t work. If I set a variable to the number value and than link the variable in the inequality block everything is running like I expect (3. Picture).

Can somebody help me to link the number block like it should…

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/d/f/df962f5c915d513ac26a045773113b088da34f57.jpeg)

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/5/f/5fed621b5b1e0a610f3d4d2ad393d63c2b09aea8.jpeg)

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/f/6fc2155cf5edcf82f8cec564c3e60126f753c05f.jpeg)

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 8, 2021, 10:45pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/2 "2021-02-08T22:45:03Z")

</div>

It’s not clear why, but the only way to compare an Item’s state to a number in Blockly right now is to put one of them into a variable first.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/c/2/c2de5512802d5b70364b5238da5dc1f5ffd93c9e.png)

---

<div class="post-metadata">

**Author:** ![johannesbonn](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@johannesbonn](https://community.openhab.org/u/johannesbonn)\
**Post date:** [February 9, 2021, 7:47am UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/3 "2021-02-09T07:47:38Z")

</div>

Thank you Rick! Are there any plans to rework this problem?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 9, 2021, 3:34pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/4 "2021-02-09T15:34:50Z")

</div>

I don’t know if an issue was opened for this or not but there is work being done on Blockly to make it more complete.

---

<div class="post-metadata">

**Author:** ![johannesbonn](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@johannesbonn](https://community.openhab.org/u/johannesbonn)\
**Post date:** [February 9, 2021, 3:58pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/5 "2021-02-09T15:58:30Z")

</div>

That sounds very good. Do you know when the new changes are going into a snapshot?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 9, 2021, 4:01pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/6 "2021-02-09T16:01:11Z")

</div>

When they are done. It’s all volunteer effort. there is no schedule. It might be tomorrow, it might be next week, it might be next year. In the next couple of weeks is the most likely.

---

<div class="post-metadata">

**Author:** ![tophee](https://community.openhab.org/user_avatar/community.openhab.org/tophee/32/56734_2.png) [@tophee](https://community.openhab.org/u/tophee)\
**Post date:** [July 17, 2021, 4:34pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/7 "2021-07-17T16:34:58Z")

</div>

From what I can tell, this has not yet been fixed.

> [@rlkoshak](#):
>
> It’s not clear why, but the only way to compare an Item’s state to a number in Blockly right now is to put one of them into a variable first.

What a strange bug. Isn’t such a comparison (i.e. between an item state and a number) quite a common thing?

I suppose the reason why I find it strange, is that I don’t understand something, but the only explanation I can come up with is that item states are usually strings so that comparing them to text was the most important thing to implement since even numbers (i.e. number-strings) can be compared to strings. So it only becomes a problem when the comparison involves mathematical operations. Does that makes sense or is there a different explanation?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 19, 2021, 3:40am UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/8 "2021-07-19T03:40:26Z")

</div>

Blocky support was hurried and released as a minimum viable product (to parrot an Agile development term). Wiring with numbers on OH is actually quite complicated because of Units of Measurement so it’s not simple to implement.

---

<div class="post-metadata">

**Author:** ![Hoerli](https://community.openhab.org/user_avatar/community.openhab.org/hoerli/32/43102_2.png) [@Hoerli](https://community.openhab.org/u/Hoerli)\
**Post date:** [June 17, 2022, 6:05pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/9 "2022-06-17T18:05:56Z")

</div>

The hint with the variable is still up to date.  
To make a simple temperature control, where the object to be cooled should be lowered a few degrees, must still be achieved with variables.  
Example: Room temperature is set to 22°C, but the air conditioner should cool down to 21°C.  
So  
[Variable] = Item State  
ItemTemperature \< [Variable] - 1

A bit dumb, but it works.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [January 24, 2026, 4:36pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/10 "2026-01-24T16:36:40Z")

</div>

As of January 2026, this is still the case as I just discovered (thanks to this thread).

It’s still not possible to compare the quantity state of an item to a number directly, which would be intuitive.

---

<div class="post-metadata">

**Author:** ![Tschetan](https://community.openhab.org/user_avatar/community.openhab.org/tschetan/32/116608_2.png) [@Tschetan](https://community.openhab.org/u/Tschetan)\
**Post date:** [January 24, 2026, 5:06pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/11 "2026-01-24T17:06:00Z")

</div>

Hi,  
since openHAB 4.0 there is the [numericState](https://www.openhab.org/docs/configuration/blockly/rules-blockly-items-things.html#get-particular-attributes-of-an-item), math-Number block and  
[Quantity Comparison](https://www.openhab.org/docs/configuration/blockly/rules-blockly-uom.html#quantity-comparison)  
You just have to part with Nashorn.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 24, 2026, 5:12pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/12 "2026-01-24T17:12:29Z")

</div>

> [@Tron](#):
>
> It’s still not possible to compare the quantity state of an item to a number directly

And it’s not going to be possible due to limitations of JS. JS doesn’t allow operator overloading.

There’s also the issue about what units to use.

100 °C \> 5

5 what? °C? °F? K?

So if you want to do math with units, all the operands need to be Quantity. And because JS doesn’t allow operator overloading, we have to use the methods on Quantity instead of the built in math operators.

Or you can strip off the units and do math with plain numbers, but there you have to use the built in math operators and not the Quantity methods.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [January 24, 2026, 6:29pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/13 "2026-01-24T18:29:04Z")

</div>

Hi Rich,

> [@rlkoshak](#):
>
> Or you can strip off the units and do math with plain numbers, but there you have to use the built in math operators and not the Quantity methods.

That was my first idea. Use “get numeric state of item”, which should strip the unit and leave me with a pure number to do math with. You are correct that “quantity” isn’t the right way here.

@Tschetan comparing the numeric state of an item to a number doesn’t work, either. The only way I’ve got it working now is by doing the detour via a variable.

And since we’re on topic: is there any reason why (in pseudocode) “if state.of.item = (x or y)” doesn’t work? The way that works is: “if (state.of.item = x) or (state.of.item = y)”.

---

<div class="post-metadata">

**Author:** ![Tschetan](https://community.openhab.org/user_avatar/community.openhab.org/tschetan/32/116608_2.png) [@Tschetan](https://community.openhab.org/u/Tschetan)\
**Post date:** [January 24, 2026, 6:57pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/14 "2026-01-24T18:57:45Z")

</div>

Hi,  
please show me what doesn’t work for you.

> [@Tron](#):
>
> if state.of.item = (x or y)

Is that even possible in a programming language? If you have many variants to compare, you can also work with lists.

 ![Screenshot 2026-01-24 195428](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/9/4/9413694dec2d450c50d25b823d7a9671d95b558a.png)

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [January 24, 2026, 7:13pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/15 "2026-01-24T19:13:57Z")

</div>

> [@Tschetan](#):
>
> Is that even possible in a programming language? If you have many variants to compare, you can also work with lists.

Hmm, now that you mention it, I was pretty sure it worked in the DSL rules, but I can’t seem to find an example in my old rules, so you are very likely correct.

But yes, when I had a bunch of conditions to compare I didn’t use the list but there is a logic block that is “AND/OR for any of the following”. That worked, too, I replaced my old switch/case rules with that.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 24, 2026, 9:24pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/16 "2026-01-24T21:24:49Z")

</div>

> [@Tron](#):
>
> You are correct that “quantity” isn’t the right way here.

I’m actually saying the opposite. If you have a Quantity, your code will be more self explanatory and robust if you stay working with Quantity. You just have to use different blocks to do the math under the “Units of Measurement” category.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [January 24, 2026, 11:02pm UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/17 "2026-01-24T23:02:47Z")

</div>

Excellent point, I admit I’ve never looked into the Units of Measurement tab.

But yes, that totally makes sense, thanks!

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [January 25, 2026, 12:40am UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/18 "2026-01-25T00:40:25Z")

</div>

Hmm, I wonder why it’s not working with quantities.

The item in question is defined as “Number:Power” and shows up with a unit of “W” in karaf:

```php
Switch_Power_LR_TV1_Power (Type=NumberItem, State=132.117 W)

```

But this doesn’t work, the if-clause doesn’t get triggered. Anything I did wrong here?

![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/optimized/3X/b/6/b6bcbc733429205828538cac811dc8af66a8d562_2_690x48.png)

---

<div class="post-metadata">

**Author:** ![Larsen](https://community.openhab.org/user_avatar/community.openhab.org/larsen/32/95475_2.png) [@Larsen](https://community.openhab.org/u/Larsen)\
**Post date:** [January 25, 2026, 6:26am UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/19 "2026-01-25T06:26:09Z")

</div>

> [@Tron](#):
>
> Anything I did wrong here?

yes: If you use quantities then you have to use the quantity operator (notice the “Qty” below) which your find under “units of measurement”.  
If you don’t use quantities then the normal operator is sufficient. So both of these work:

 ![Screenshot_20260125_071930_Brave](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/b/6b4e4730426f70ba5e50c91eb9a41a85f0d6fec0.jpeg)

edit: the last one is wrong. See correct one below

---

<div class="post-metadata">

**Author:** ![Tschetan](https://community.openhab.org/user_avatar/community.openhab.org/tschetan/32/116608_2.png) [@Tschetan](https://community.openhab.org/u/Tschetan)\
**Post date:** [January 25, 2026, 8:44am UTC](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517/20 "2026-01-25T08:44:46Z")

</div>

Yup. But the 2nd example is very unfortunate. Numbers should never be used as a string when comparing. “10” is less than “2 W”  
because: The comparison of two strings is done character-by-character (i.e., according to the value of charCodeAt) from left to right. The extended equality rules of the Unicode standard do not apply, and Unicode normalization does not take place.

[Next page](https://community.openhab.org/t/oh3-blockly-logic-block-compare-item-state-with-number/116517.md?page=2)
