"if item does not exist" within oh-repeater

I iterate over all my Shelly devices for two specific items (qualitiy of signal, operating temperature). Some of my shellies do not provide operating temperature, thus there is no item defined for that thing. The widget in general works fine.

What I try to work around is a missing function like “if exist item xyz”.

In the following yaml code you see that I have an oh-repeater within an oh-repeater and that the name of the item consists of two repeater names.

- component: oh-repeater
  config:
    for: rItem
    fragment: true
    in:
      - Signalstarke
      - Geratetemperatur
    sourceType: array
  slots:
    default:
      - component: f7-col
        config:
          style:
            font-weight: normal
            white-space: nowrap
            width: 20%
        slots:
          default:
            - component: Label
              config:
                text: "=(!!items[loop.rThing.name + '_' + loop.rItem]) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"

However, none of the following works:

"=(!!items[loop.rThing.name + '_' + loop.rItem]) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"
"=(!!items[loop.rThing.name + '_' + loop.rItem].state) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"
"=(items[loop.rThing.name + '_' + loop.rItem]) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"
"=(items[loop.rThing.name + '_' + loop.rItem].state) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"

Does anybody has a tip for me?

The items[] call returns - not a falsy value when it is called with an item name that doesn’t exist. So your condition in all of the examples is always returning true. In this case, because the loop object behaves in a more standard way, you can just test the loop object directly. Something like:

text: "=(loop.rThing.name && loop.rItem) ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"

Hi Justin,
thanks for your help.
loop.rThing.name (which is the item’s prefix) and loop.rItem (which is the item‘s suffix) do always exist in each loop. But one cannot know if the resulting item name really exists.
So I think at first we have to create the entire item name and then we need to check if that item exists.

just to add some infos:
the Label control within oh-repeater:

text: =items[loop.rThing.name + '_' + loop.rItem].state

obviously then creates lots of error messages such as

[WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: ShellyGartenWandlampe_Geratetemperatur

In that case, the only easy method is to test if the items dict returns - or not:

text: "=(items[loop.rThing.name + '_' +  loop.rItem].state != '-') ? items[loop.rThing.name + '_' + loop.rItem].state : 'x'"
1 Like