# Members.filter for DateTime items

**URL:** <https://community.openhab.org/t/members-filter-for-datetime-items/129749>\
**Category:** Off-Topic\
**Tags:** rules\
**Created:** [December 8, 2021, 12:39am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749 "2021-12-08T00:39:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwiseman](https://community.openhab.org/user_avatar/community.openhab.org/jwiseman/32/97501_2.png) [@jwiseman](https://community.openhab.org/u/jwiseman)\
**Post date:** [December 8, 2021, 12:39am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/1 "2021-12-08T00:39:00Z")

</div>

Hey @rlkoshak,

Looking for your expertise on this one. Getting 4 warnings that are tied to DateTime fields on OH 3.x, wonder how to resolve them?

```auto
There is no context to infer the closure's argument types from. Consider typing the arguments or put the closures into a typed context.

```

I’m using this syntax for each of them, I understand that Generic is not correct for DateTime field. What is the correct syntax?

```auto
			if (gDimmerLastChange.members.filter[GenericItem s | s.state == NULL || s.state === null || s.state.toString() != 'UNDEF'])	{ 
			
				s.postUpdate(new DateTimeType())
			}

```

Best, Jay

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 8, 2021, 12:56am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/2 "2021-12-08T00:56:31Z")

</div>

> [@jwiseman](#):
>
> `if (gDimmerLastChange.members.filter[... )`

That’s an unusual construction.  
The Group `filter` will return a list object.  
The `if()` will try to evaluate the list as true or false; if the list is empty (`null`) I expect that will be false, not sure if any non-zero list would be true?  
In either case, the list is then thrown away before executing the next line.

This might help if you really want to test list/no-list

> [@Using .members.filter](https://community.openhab.org/t/using-members-filter/12772/2):
>
> It really helps if you post your code using code fences so the forum property formats it: ``` your code here ``` Two things with filter. I think you have to use square brackets, not parens Filter returns a List object containing all the Items that match the filter So the line should be: if(Sensor.members.filter[s|s.name == mm].size \> 0) // there is at least one Item named mm

> [@jwiseman](#):
>
> `s.state === null`

This never happens and can be removed,

General guidance

> [@Design Pattern: Working with Groups in Rules](https://community.openhab.org/t/design-pattern-working-with-groups-in-rules/20512):
>
> Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous. Please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for how to read and use DPs. Problem Statement One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc. Concept T…

I would imagine you want to `forEach` through your filtered list

---

<div class="post-metadata">

**Author:** ![jwiseman](https://community.openhab.org/user_avatar/community.openhab.org/jwiseman/32/97501_2.png) [@jwiseman](https://community.openhab.org/u/jwiseman)\
**Post date:** [December 8, 2021, 3:07am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/3 "2021-12-08T03:07:11Z")

</div>

> [@rossko57](#):
>
> you want to `forEach` through your filtered list

This syntax is still throwing the same warnings.

gDimmerLastChange.members.forEach[GenericItem s | s.postUpdate(new DateTimeType())]

Best, Jay

---

<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:** [December 8, 2021, 4:23am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/4 "2021-12-08T04:23:12Z")

</div>

@rossko57 is absolutely right, the filter returns a List so you need to do a forEach on that filtered list assuming you want to do something with each member of the filtered list.

What happens if you leave out the “GenericItem” part? It’s not usually needed and it’s often better to not type things unless you really really have to. This isn’t a case where you usually have to.

However, if the warning is still there without the “GenericItem”, then I’m skeptical that this is the actual source of the warning.

It’s complaining that it can’t infer the type of the closure’s arguments. In this case `GenericItem s` _is_ the argument to the closure. And by providing GenericItem as the type, you _are_ typing the argument. In this context, for the filter, GenericItem is probably the right type to use. You don’t need to be more specific than that. But, as I said it’s not really needed because the `.members` returns a `List<Item>` so it already knows the type.

One note on the filter though. `s.state` will \*never be `null` so that test is redundant. And it’s off to compare the state to `NULL` but then the string `"UNDEF"`. Why not compare to `UNDEF` the same as you compare to `NULL`?

Or you can use `if(s.state instanceof UnDefType)` which covers both NULL and UNDEF.

---

<div class="post-metadata">

**Author:** ![maniac103](https://community.openhab.org/user_avatar/community.openhab.org/maniac103/32/73196_2.png) [@maniac103](https://community.openhab.org/u/maniac103)\
**Post date:** [December 8, 2021, 7:02am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/5 "2021-12-08T07:02:54Z")

</div>

> [@rossko57](#):
>
> That’s an unusual construction.  
> The Group `filter` will return a list object.  
> The `if()` will try to evaluate the list as true or false; if the list is empty (`null`) I expect that will be false, not sure if any non-zero list would be true?

For me it looks like he wants `anyMatch` or `allMatch` instead of `filter` … basically check if any entry or all entries in the group have an initialized state.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 8, 2021, 10:34am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/6 "2021-12-08T10:34:20Z")

</div>

> [@jwiseman](#):
>
> gDimmerLastChange.members.forEach[GenericItem s | s.postUpdate(new DateTimeType())]

This will “do stuff” to _every_ Group member. (which might be a good idea to get the basics working)  
How do know the error relates to this line of the mystery rule?

If/when you want to filter, then filter first, and forEach through the resulting list.  
`MyGroup.members.filter[i | <condition>].forEach [x | <do stuff>]`

---

<div class="post-metadata">

**Author:** ![jwiseman](https://community.openhab.org/user_avatar/community.openhab.org/jwiseman/32/97501_2.png) [@jwiseman](https://community.openhab.org/u/jwiseman)\
**Post date:** [December 8, 2021, 1:01pm UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/7 "2021-12-08T13:01:46Z")

</div>

I want to Thank everyone for their guidance. I used everybody’s suggestions and fixed my warnings and improved other code also with the feedback.

Best, Jay

---

<div class="post-metadata">

**Author:** ![system](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/6/6a52f2ac5d00e4c7b50471a354b3a9221188be90.png) [@system](https://community.openhab.org/u/system)\
**Post date:** [January 19, 2022, 5:02am UTC](https://community.openhab.org/t/members-filter-for-datetime-items/129749/8 "2022-01-19T05:02:05Z")

</div>

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.
