Design Pattern: Unbound Item (aka Virtual Item)

Problem Statement

Sometimes one needs to create a flag, control, switch, or some other control that is not bound to a device or channel. For example, a Switch to override presence detection.


Create an Item that does not have a binding configuration.

Simple Example

Switch VirtualItem "label" <icon> (groups) // No binding config

See the Sensor Aggregation Design Pattern for a more complicated example. The Virtual Item is Override_Presence in that example.

Advantages and Limitations

Virtual Items are a good way to store state needed by rules and to provide a way to allow the user to modify the behavior of their rules by adjusting parameters stored in Virtual Items. Data stored in Virtual Items can also be persisted and restored on startup unlike global vars and vals.

Related Design Patterns

Help, I'm ready to give up
[SOLVED] HUE + HM-PB-2-FM + WebSwitch
Hints to solve some scenarios in openHab (coming from homee)
Config & documentation
Comprehensive Wunderground using HTTP Binding Example
Energy harvesting
"disable" switch item
Design Pattern: Human Readable Names in Messages
String output based on status of multiple items
Planning out automation project
Setting a master variable / switch
Do not execute a Rule
Help needed! Xiaomi button click > Call IFTTT webhook > Turn Sonoff ON
Design Pattern: Switch & Dimmer
Design Pattern: Encoding and Accessing Values in Rules
Design Pattern: Associated Items
Sonos NotificationVolume no longer adjustable
Is it possible to enable/disable rules created textually via Paper UI?
Using HABMin rules in HABPanel
Absolute value of a decimal
Can't get rid of duplicate item
How to create a macro with OpenHab?
How to store additional info in ITEM?
How do I create groups and items in runtime mode, display them in the user´s interface (basic UI perhaps), and control their events (also in runtime)? Is it possible?
[SOLVED] Persistence query in habpanel
Best way to make long time macro
Design Pattern: Cascading Timers
Suggestion for controlling my underfloor heating, Roth Touchline
Need a little help transforming DSL Rules to the new JSScript
Integration of I2C sensors (raspberrypi4/BMP085 or BMP180; openhabian snapshot, Lua 5.1, lua-periphery, Exec Binding/REST API/MQTT)
[Deprecated] Design Pattern: Time Of Day
A Deep Dive into the Semantic Model
OpenHAB 3.1 expire command does not always work
Keeping state in ECMAScript rules in OH3?
Map battery level to %
How I have automated my lights
Automation of venetian blinds
Patterns proposal: item states delay management and items dependencies (cascading)
Input channel numbers with Infrared
Rule higher than 40 degrees notification sent
[SOLVED] A switch to turn on/ off rules
Journey to JSR223 Python 1 of 9
Accessing HomeKit scene events from OH

Data stored in Virtual Items can also be persisted and restored on startup unlike global vars and vals.

You might want to add how to do that.

I have figured out, by adding it to a group that was persisted and that group had restoreOnStartup in the .persiste file.

Yet it took me a long time to find that.

Wow, wanted to bump this old DP up because I had never stumbled across it before and it is a great explanation for what I call ‘dummy’ items in my rule examples. Thanks for this Rich and I hope you don’t mind if I link to this thread in my example threads.

The DPs are there to be linked to. Most of there were written because I got tired of typing the same thing over and over again. Please do link to it and I’m happy you found it useful.

Does this still apply in OpenHAB2?

Because I have been trying to define a simple logical (unbound) Switch item to set a certain “alert” status to on or off manually (to then use further in rules) but the state of the switch (at least in Basic UI) does not seem to persist?

Perhaps you could show us what you do. An Item state stays the way it is until something happens - a new update, or system reboot etc.
“Persist” is generally taken to mean a means of surviving e.g. a reboot, but maybe that is not what you meant.

I have a very simple logical item in an .items file:

Switch alert_status "Alert Status"

And that is all. I have added this also to my home.sitemap:

sitemap home label="Home" {
    Frame label="Alert" {
    Switch item=alert_status icon="switch" label="Status"

If I open the Basic UI on for example my phone and desktop at the same time, I can toggle the switch from either device, but the state is not reflected on the other device.

Have you tried manually reloading the Sitemap on the other device. Because I’m not sure if you will always see the updated state on the second device otherwise. The best way to see if it works is to look at the event log and not at the ui.

This happens sometimes (every couple of weeks) to me too.
Restarting openHAB did always solve this.

Thanks guise!

I restarted OpenHAB and that did the trick! Tested by spamming (slowly*) about 5-10 times from each device. Works flawlessly now both ways, and also reflected in logs which I had open in another window.

Now, off to write some rules, incorporating my new button(s)! :slight_smile:

* I mean it is a big Java thing at the end of the day after all :grin: I kid because I love :heart:

1 Like