OH3 Widget hide component if item does not exist

Hi All

I am setting up a generic widget using props to populate the display.

As a result there may be times when a component refers to a non-existent Item - which I would prefer to hide.

          - component: oh-label-item
            config:
              action: group
              actionGroupPopupItem: =(props.partitionPrefix) + '_Doors'
              item: =(props.partitionPrefix) + '_Doors'
              title: =(props.partitionPrefix) + ' Doors'
              icon: oh:motion
              iconUseState: true
          - component: oh-label-item

The above is an example of a component I am using. I know I can hide the component based on the state of the item, however cannot find out how I can hide the component if the item does not exist?

          - component: oh-label-item
            config:
              visible: '=(items[props.partitionPrefix + "_Doors"].state === "ON")
              action: group
              actionGroupPopupItem: =(props.partitionPrefix) + '_Doors'
              item: =(props.partitionPrefix) + '_Doors'
              title: =(props.partitionPrefix) + ' Doors'
              icon: oh:motion
              iconUseState: true
          - component: oh-label-item

Any suggestions or advise would be appreciated.

Thanks as always
Mark

I don’t know for sure but maybe =items[props.partitionPrefix+"_Doors"] === undefined might work.

1 Like

Thanks @rlkoshak . Always appreciate your help.

I need to hide to the component if the item does NOT exist. The syntax hides the component, but also a valid component.

Tried =items[props.partitionPrefix+"_Doors"] !== undefined with no luck.

Querying REST API gives:

{
  "error": {
    "message": "Item Partition3_Doors does not exist!",
    "http-code": 404
  }
}

So I don’t think, but not sure that undefined would work.

I made an additional component to display the title of =items[props.partitionPrefix+"_Doors"].state

          - component: oh-label-item
            config:
              action: group
              actionGroupPopupItem: =(props.partitionPrefix) + '_Doors'
              item: =(props.partitionPrefix) + '_Doors'
              title: =items[props.partitionPrefix+"_Doors"].state
              icon: oh:motion
              iconUseState: true 

Which gives:

widget 1

So I can get INVAILD componets to display using

visible: =items[props.partitionPrefix+"_Doors"].state === "-"

Now just now sure how to invert that so they display if the state is NOT “-”?

Tried !==, != etc. My coding is just not up to this.
EDIT…
Sorted:
visible: =items[props.partitionPrefix+"_Doors"].state != "-"
Redraw issue!

Just a caution that you can get valid Items displaying that state too under various circumstances e.g. NULL because not yet initialized or UNDEF due to equipment failure. You may or may not care.

1 Like

Thanks for the “Heads Up”. I had only briefly considered the implications. Though in my case I don’t think it is a critical concern. If the component does not display then there is an issue with the config (or the item doe snot exist because it is not required.

I am sure there are many use cases for a “Not Exist” condition for visibility.

Always interested to hear better /other solutions?

I don’t know about UNDEF, but the items object in widgets does report "Null" for NULL states.