Item list

This weekend I’ve been doing some maintenance on the devices connected to my openhab system, and realized how hard this can be!

Since I use HABPanel and not every item is directly mapped in the UI, getting the status of every item is a pain.

Does anyone know a simple way of listing all the items in a browser or on the command line, without having to configure sitemaps?

Thanks.

For me, the easiest way to show (multiple) items and their status during dev work is the new developer side bar (OH3).
Did you try this, maybe it is sufficient?

Thanks @chrismast . I should have mentioned that I’m still in the oh2.5 domain…

ah ok!
in OH2.5 I usually used VS Code and its plug-in as it also enables you to show (all) items and their current state. Also quite handy when you are writing rules.

http://ip-of-your-OH:8080/rest/items

It’s a bit messy on the browser screen but does get the items.

This will get all the items from the REST API and will format them to be human readable

curl -X GET "http://localhost:8080/rest/items/" 2>/dev/null | sed 's/,/\n/g' | grep name | sed 's/name//g' | sed 's/[":]//g'

(replace localhost if necessary obviously)


I use this function for a bash item autocompletion to get and set the status of items from the command line for debugging:

_item_completions(){
    items=$(curl -X GET "http://localhost:8080/rest/items/" 2>/dev/null | sed 's/,/\n/g' | grep name | grep -v Gruppe | sed 's/name//g' | sed 's/[":]//g')
    local anfang="${COMP_WORDS[$COMP_CWORD]}"
    for item in $( echo $items | sed 's/ /\n/g' | grep -P "^$anfang"); do
        COMPREPLY=("${COMPREPLY[@]}" "$item")
    done
}

complete -F _item_completions item_get item_set

item_get(){
    [ -z ${1} ] && echo -e "\e[31mEs wurde kein Item angegeben\e[39m" && return 1
    for item in $@ ; do
        status=$(curl -X GET "http://localhost:8080/rest/items/$item/state" 2>/dev/null)
        [[ "$status" == *"does not exist!"* ]] && echo -e "\n\e[31mDas angegebene Item \"$item\" existiert nicht\e[39m" && return 1
        echo -e "\e[36mItem:\e[39m\t$item\n\e[36mStatus:\e[39m\t$status"
    done
}

item_set(){
    [ -z ${1} ] && echo -e "\e[31mEs wurde kein Item angegeben\e[39m"   && return 1
    [ -z ${2} ] && echo -e "\e[31mEs wurde kein Status angegeben\e[39m" && return 1
    [ $# -gt 2 ] && echo -e "\e[31mEs wurden zu viele Argumente angegeben\e[39m" && return 1
    alter_status=$(curl -X GET "http://localhost:8080/rest/items/${1}/state" 2>/dev/null)
    [[ "$alter_status" == *"does not exist!"* ]] && echo -e "\e[31mDas angegebene Item existiert nicht\e[39m" && return 1
    echo -e "\e[36mItem:\e[39m\t\t${1}"
    echo -e "\e[36mAlter Status:\e[39m\t$alter_status"
    neuer_status_setzen=$(curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "${2}" "http://localhost:8080/rest/items/${1}/state" 2>/dev/null)
    [[ "$neuer_status_setzen" == *"State could not be parsed"* ]] && echo -e "\e[31mDer neue Status wird nicht von dem Item akzeptiert\e[39m" && return 1
    echo -e "\e[36mNeuer Status:\e[39m\t${2}"
}


}

For the listing, I came up with a mix of the suggestions:

#!/usr/bin/env bash

tabs 48

curl --show-error --silent http://localhost:8080/rest/items/ | jq  -jr '.[] | .name, "\t", .state, "\n"' | sort