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.

Concept

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

9 Likes

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.
Johannes

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