OH3: jsondb -- extracting item names and state

  • openHABian 3.4.3 on rPi4 with 4GB

This is a generic question, hence, no further detail about the specific hardware/software config.

I am trying to:

  1. extract all item names from the org.openhab.core.items.Item.json file.
    Solved: jq -r 'keys[]' org.openhab.core.items.Item.json
  2. ideally get a list of item names and their state

What I am really interested in, is more specific: find all items with state = UNDEF.
However, listing items names is generically useful for me; so is any state. I should be able to apply relevant filters.

I tried jq, but could not make it work, as the first property name is the item, not the property value.

Any hints appreciated.

You can get all items from OH‘s rest API

http://openhab:8080/rest/items

and depending on what you want to achieve, parse the resulting json string.

1 Like

Thanks… this and jq will work (for my json file).

Solution:

  1. Open: http://openhabian:8080/rest/items
  2. Save this file as rest_items.json
  3. Run jq 'map(select(.state | contains("UNDEF")))' rest_items.json | grep -i link
  4. Which will produce this:
    "link": "http://openhabian:8080/rest/items/Driver40Troffer03_ColourTemperature",
    "link": "http://openhabian:8080/rest/items/fbox_outgoing_call",
    "link": "http://openhabian:8080/rest/items/fbox_incoming_call",
    "link": "http://openhabian:8080/rest/items/fbox_active_call",
    "link": "http://openhabian:8080/rest/items/Shed_KDL_ColourTemperature",
    "link": "http://openhabian:8080/rest/items/gTeslaM3_tire_pressure",
    "link": "http://openhabian:8080/rest/items/fbox_resolved_name",

Of course variations are possible; e.g., use curl to get the actual data passed into jq.

You can also limit the output to the desired fields:

http://oh:8080/rest/items?fields=link,state

would have been nice if we could add &state=NULL to the get request.

1 Like

This often leads to XY Problems as questions like this exclude other possiblities which might be a better approach over all.

As @Oliver2 indicates, you’ll only be able to get access to the Item’s states through the REST API. But once you have that you’ll be able to use jq to parse out what you need.

You could also do this in a quick rule if all you are after are the names of the UNDEF Items. In JS it would be:

items.filter( i => i.state == "UNDEF" ).forEach( i => console.log(i.name + " is UNDEF") );

In Rules DSL it would be something like:

org.eclipse.smarthome.model.script.ScriptServiceUtil.getItemRegistry().getItems().filter[ i | i.state == UNDEF].forEach[ i | logInfo('UNDEF Items', i.name + ' is UNDEF') ]

Oh, and I forgot to mention that the Developer Sidebar can search Item states. So searching for NULL there will show all the Items that are NULL.

However, I’m not sure the sidebar search does that in OH 3.

1 Like