# Use variable for item

**URL:** <https://community.openhab.org/t/use-variable-for-item/41721>\
**Category:** Scripts & Rules\
**Tags:** rules\
**Created:** [March 14, 2018, 11:37am UTC](https://community.openhab.org/t/use-variable-for-item/41721 "2018-03-14T11:37:28Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:** [March 14, 2018, 1:52pm UTC](https://community.openhab.org/t/use-variable-for-item/41721/7 "2018-03-14T13:52:20Z")

</div>

Wrong approach, there is no way to substitute a name string for an OH Item in the rules language.

There is a way to select Items by name from a Group, which is one of the possible ways to re-use code

> [@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…

There are also lambda “functions” which allow passing of Items to reusable code

> [@Reusable Functions: A simple lambda example with copious notes](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888):
>
> Major Edit: Rewrote with the cleaner OH 2.3 syntax. The example lambda on the OH 1 wiki is overly complex and as written will not work in OH 2. This quick example will show a super simple lambda example with copious notes. Here is the code without comments. It takes in an Item, logs it’s state and returns a slightly modified version of the String logged. val log= [GenericItem s | logInfo("lambda", s.state.toString) s.state.toString + " logged"] rule "Call Lambda" when // some t…

---

_[View the full topic](https://community.openhab.org/t/use-variable-for-item/41721)._
