# State Filter (Range Filter) Profile

**URL:** <https://community.openhab.org/t/state-filter-range-filter-profile/158025>\
**Category:** Features\
**Tags:** transformations, filter\
**Created:** [August 25, 2024, 3:56pm UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025 "2024-08-25T15:56:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 25, 2024, 3:56pm UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/1 "2024-08-25T15:56:42Z")

</div>

Some time ago, I tried submitting a [range filter profile](https://github.com/openhab/openhab-core/pull/3534) to core, but it was rejected because such profiles should be added as an add-on.

I noticed that [Basic Profiles](https://www.openhab.org/addons/transformations/basicprofiles/) add-on was recently merged into the official openhab repo. This includes a `State Filter` profile.

I’ve been working on improvements to this profile so that it can be used to filter incoming data based on the incoming data itself.

With this PR it supports:

- Filtering (EQ and NEQ) based on the state of other items (the original feature of this profile)
- Expanded comparisons using greater/less than operators (`GT`, `GTE`, `LT`, `LTE`)
- Support comparisons using symbols too (`==`, `!=` (or `<>`), `>`, `>=`, `<`, `<=`)
- Filtering based on the incoming data (added in this PR)
- Compare one item’s state against another item’s state, e.g. `Item1 > Item2`
- Compare the input state against another items’s state, e.g. `> LowerThresholdItem, < UpperThresholdItem`

Some quick contrived examples:

Two conditions, checking two other items:

```java
Number:Temperature Aircon_Temp { 
  channel="xxxx" [
    profile="basic-profiles:state-filter",
    conditions="Aircon_Power==ON", "House_Occupied == ON",
    mismatchState="UNDEF"
  ]
}

```

Checking against the incoming state, making sure it’s within a “reasonable” range

```java
Number:Temperature BBQ_Temperature {
  channel="xxx" [
    profile="basic-profiles:state-filter",
    conditions=">= 0 °F", "< 1000 °F" 
  ]
}

```

[https://github.com/openhab/openhab-addons/pull/17323](https://github.com/openhab/openhab-addons/pull/17323)

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 1:16am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/2 "2024-08-27T01:16:05Z")

</div>

I could add a way to check for delta, average, stddev, median, in a separate pr later. Any other ideas?

---

<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:** [August 27, 2024, 5:18am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/3 "2024-08-27T05:18:12Z")

</div>

sorry, no ideas yet. But woohoo. That sneaked in somehow and I didn’t notice it. It sounds awesome.  
(I’m pretty sure that quite a few are fiddling around with proxy items and rules while most of it can be solved now just with those profiles).

Maybe one idea/question (but not for the state filter): Would the rollershutter-profile-addon fit into that package too? It emulates positions for rollershutters without this function. It currently lacks the ability to be configured within the UI, so this would be something that could be updated. Or is that too specific for the “basic”-package?

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 27, 2024, 5:27am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/4 "2024-08-27T05:27:48Z")

</div>

> [@jimtng](#):
>
> Any other ideas?

Would it be somehow possible to use another item state as input instead of a thing channel?  
E.g. some kind of virtual channel which links one item to another item?

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 6:16am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/5 "2024-08-27T06:16:39Z")

</div>

> [@Spaceman\_Spiff](#):
>
> Would it be somehow possible to use another item state as input instead of a thing channel?  
> E.g. some kind of virtual channel which links one item to another item?

Interesting idea. While it’s possible, however the profile is only “triggered” / invoked when the actual channel / binding is sending an update though.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 6:20am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/6 "2024-08-27T06:20:32Z")

</div>

> [@Larsen](#):
>
> Would the rollershutter-profile-addon fit into that package too?

I wish the addon had a different name, but I can’t think of a better name right now :D. The word “basic” implies there could be an “advanced” one… and that’d just be confusing: “Is this “median” filter in the basic profile addon, or in the advanced one?”

And then there’s a concern (not mine) that things could get out of hand and we ended up with far too many profiles.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 6:29am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/7 "2024-08-27T06:29:50Z")

</div>

Updated the OP with some examples

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 27, 2024, 7:31am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/8 "2024-08-27T07:31:03Z")

</div>

> [@jimtng](#):
>
> Interesting idea. While it’s possible, however the profile is only “triggered” / invoked when the actual channel / binding is sending an update though.

The idea is that the profile would be triggered when the source item state updates, otherwise it would not be very useful.That way simple rules could become an easy item configuration.

e.g. I do some calculations and update an item. When the item state is over 100 a light should turn on.  
Obviously I can write a simple rule that does exactly that but with a configuration it would be easier.

Also it would be nice to reference other item states as boundaries instead of fixed values.  
E.g.

```auto
Number:Temperature BBQ_Temperature {
  channel="xxx" [
    profile="basic-profiles:state-filter",
    conditions=">= $MyLowerBoundaryItem", "< $MyUpperBoundaryItem" 
  ]
}

```

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 7:42am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/9 "2024-08-27T07:42:36Z")

</div>

> [@Spaceman\_Spiff](#):
>
> The idea is that the profile would be triggered when the source item state updates, otherwise it would not be very useful.That way simple rules could become an easy item configuration.

That would only work if the source item is linked to a channel, and we’re “hijacking” that link. It won’t work for virtual items since profiles are only applicable to item channel links.

> [@Spaceman\_Spiff](#):
>
> Also it would be nice to reference other item states as boundaries instead of fixed values.  
> E.g.
> 
> ```auto
> Number:Temperature BBQ_Temperature {
> channel="xxx" [
> profile="basic-profiles:state-filter",
> conditions=">= $MyLowerBoundaryItem", "< $MyUpperBoundaryItem" 
> ]
> }
> 
> ```

This is a good idea

Perhaps we need to require string literals to always be quoted. This way we can treat bare words to be item names, thus avoiding the need to use special symbols like $ in this case.

I was thinking of using $ for special operators like $delta

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 12:29pm UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/10 "2024-08-27T12:29:11Z")

</div>

@Spaceman_Spiff I’ve added the feature to compare against another item as you suggested, thanks!

I’ll update the OP to show this use case.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 27, 2024, 12:46pm UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/11 "2024-08-27T12:46:10Z")

</div>

Note that DateTimeType currently cannot utilise inequality operators e.g. DateTimeItem1 \> DateTimeItem2. If this [PR](https://github.com/openhab/openhab-core/pull/4364) is merged, then we can have inequality comparisons for DateTimeType here.

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 27, 2024, 4:59pm UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/12 "2024-08-27T16:59:45Z")

</div>

Nice - you’re on fire!

Does it re-trigger evaluation when the referenced item changes?  
Will this work for all profiles?

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 28, 2024, 3:20am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/13 "2024-08-28T03:20:55Z")

</div>

> [@Spaceman\_Spiff](#):
>
> Does it re-trigger evaluation when the referenced item changes?

It reevaluates item states every time the incoming data is received.

> [@Spaceman\_Spiff](#):
>
> Will this work for all profiles?

Please elaborate this question

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 28, 2024, 3:48am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/14 "2024-08-28T03:48:14Z")

</div>

> [@jimtng](#):
>
> It reevaluates item states every time the incoming data is received.

So if I understand it correctly when the referenced item changes nothing happens.  
Only when a value through the channel is received the new boundary will be picked up.  
That’s a gotcha that needs to be explicitly made clear in the docs.

> [@jimtng](#):
>
> Please elaborate this question

Will referencing other items work for all profiles or just the “basic profiles” you are mentioning in the title?

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 28, 2024, 4:16am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/15 "2024-08-28T04:16:45Z")

</div>

> [@Spaceman\_Spiff](#):
>
> So if I understand it correctly when the referenced item changes nothing happens.  
> Only when a value through the channel is received the new boundary will be picked up.

This is correct.

Let’s say in a profile for item A, you have conditions that check for items X and Y.

```auto
Number X
Number Y
Number A { channel="xxx" [profile="basic-profiles:state-filter", conditions="> X, < Y"]}

```

What happens is that whenever the channel `xxx` sends an updated data to item A, the profile will be invoked and it then re-evaluates the conditions based on the _current_ states of item X and Y.

That’s all it does.

That profile does not get invoked independently when X or Y changes, because that’s not how profiles work.

If you want something to happen _whenever_ X or Y changed, you’d either add a rule that triggers when X or Y changed, or you’d add a profile on X or Y, but that has nothing to do with _this_ profile on item A.

> [@Spaceman\_Spiff](#):
>
> Will referencing other items work for all profiles or just the “basic profiles” you are mentioning in the title?

I know that the `basic-profiles` addon contains multiple profiles. I’m only making changes to the `basic-profiles:state-filter` profile. It does not affect/apply to any other profiles within the `basic-profiles` addon, nor any other profiles outside this addon.

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 28, 2024, 8:19am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/16 "2024-08-28T08:19:09Z")

</div>

> [@jimtng](#):
>
> If you want something to happen _whenever_ X or Y changed, you’d either add a rule that triggers when X or Y changed, or you’d add a profile on X or Y, but that has nothing to do with _this_ profile on item A.

So it works as I suspected. It’s clear to me why but I still think there should an explicit warning or info in the docs with a behavior example (like you posted above) because it’s probably not clear for everyone.

> [@jimtng](#):
>
> I know that the `basic-profiles` addon contains multiple profiles. I’m only making changes to the `basic-profiles:state-filter` profile. It does not affect/apply to any other profiles within the `basic-profiles` addon, nor any other profiles outside this addon.

I see. Do you think referencing item states should work across all profiles in the same manner?  
E.g. also for the `system:range`  
 ![grafik](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/7/e/7e3dba3165085088433c36820c020703b5c7e641.png)

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 28, 2024, 8:43am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/17 "2024-08-28T08:43:46Z")

</div>

> [@Spaceman\_Spiff](#):
>
> Do you think referencing item states should work across all profiles in the same manner?  
> E.g. also for the `system:range`

Maybe. It just needs to be implemented separately on a case by case basis, as there’s no one central place to change them all.

---

<div class="post-metadata">

**Author:** ![Spaceman\_Spiff](https://community.openhab.org/user_avatar/community.openhab.org/spaceman_spiff/32/56883_2.png) [@Spaceman\_Spiff](https://community.openhab.org/u/Spaceman_Spiff)\
**Post date:** [August 29, 2024, 4:42am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/18 "2024-08-29T04:42:47Z")

</div>

> [@jimtng](#):
>
> Maybe. It just needs to be implemented separately on a case by case basis, as there’s no one central place to change them all.

Hm, shame. It would have been really nice if there is a central place where this functionality could have been implemented. It’s really awkward, that referencing items will work for one profile but not for the other.

Maybe it would make sense to rework the whole profiles so that

- there is a central place that handles these things
- it’s possible to chain profiles together
- it’s possible to apply profiles on “virtual items”.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 29, 2024, 4:54am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/19 "2024-08-29T04:54:17Z")

</div>

> [@Spaceman\_Spiff](#):
>
> it’s possible to apply profiles on “virtual items”.

This one is easy. Introduce a “virtual binding” then you can have your profile as usual. But this is a very convoluted way of avoiding to write a rule.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [September 3, 2024, 9:13am UTC](https://community.openhab.org/t/state-filter-range-filter-profile/158025/20 "2024-09-03T09:13:30Z")

</div>

[https://github.com/openhab/openhab-addons/pull/17362](https://github.com/openhab/openhab-addons/pull/17362)

- Improve the conditions flexibility so that any type of operand can be on either side of the operator, e.g.: `ItemName > 10` and `10 < ItemName`
- Add support for functions:
  - `$DELTA` to represent the absolute difference between the incoming value and the previously accepted value.
  - `$AVERAGE`, or `$AVG` to represent the average of the previous incoming values, excluding the incoming value.
  - `$STDDEV` to represent the _population_ standard deviation of the previous unfiltered incoming values.
  - `$MEDIAN` to represent the median value of the previous incoming values.
  - `$MIN` to represent the minimum value of the previous incoming values.
  - `$MAX` to represent the maximum value of the previous incoming values.  
By default, 5 samples of the previous values are kept.  
This can be customized by specifying the “window size” or sample count applicable to the function, e.g. `$MEDIAN(10)` will return the median of the last 10 values.  
All the functions except `$DELTA` support a custom window size.

Example:

Filter out incoming data with very small difference from the previous one:

```java
Number:Power PowerUsage {
  channel="mybinding:mything:mychannel" [ 
    profile="basic-profiles:state-filter",
    conditions="$DELTA > 10 W"
  ]
}

```

[Next page](https://community.openhab.org/t/state-filter-range-filter-profile/158025.md?page=2)
