Virtual Item - String - Default Value

Not generically, but my Scripted Automation implementation of the Time of Day Design Pattern implements something like this. [Scripted Automation] Multi-day Time of Day Library

I’ve been deciding whether to extract this and make it generic. I’m hesitant to do this though because I fear it will be used in less than ideal ways. NULL and UNDEF are states that should be embraced and used, not avoided at all costs. But I also recognize there are times when you need to boot strap the state of an Item when it’s first created.

EDIT:

I’ve not tested this but it’s simple enough that it should work.

from core.rules import rule
from core.triggers import when
from core.metadata import get_key_value,remove_metadata
from core.utils import postUpdate, post_update_if_different

@rule("Initialize configured Items",
      description="Updates Items with an initialization value",
      tags=["init"])
@when("System started")
def item_init(event):
    """
    Rule that triggers at System started and populates Items with an initial
    value. The initialization value is defined in metadata with three key/values.
        - value: value to send update the Item to
        - override: whether to update to this value even if the Item already
            has a value. It's an optional key and should be either absent,
            "true", or "false"
        - clear: whether to delete the metadata once the Item is updated. It's
            an optional key and should be either absent, "true", or "false"

    For example:
        - { init=""[value="ON", override="true", clear="true"] }: Initialize
            the Switch Item to ON whether or not it already has a value and then
            delete the metadata.
        - { init=""[value="123,45,67"] }: Initialize the Color Item to the value
            only if it is NULL or UNDEF.

    Limitations:
        - The clear option only works for Items not created through .items
            files. For Items defined in .items files, you must manually remove
            the metadata or else it will get reloaded next time the file is
            loaded.
    """

    for item_name in [i for i in items if get_key_value(i, "init", "value")]:
        value = get_key_value(i, "init", "value")

        if get_key_value(i, "init", "override") == "true":
            post_update_if_different(i, value)
        elif isinstance(items[i], UnDefType):
            postUpdate(i, value)

        # only works if Items are not defined in .items files
        if get_key_value(i, "init", "clear") == "true":
            remove_metadata(i, "init")

I’ll probably submit it to the helper library at some point. But I’ve already a bunch awaiting merge so I’m holding off submitting new ones right now.