Creating Item to Thing Channel Links via rule/script

  • Platform information:
    • Hardware: _arch, pi3+,1GB,

    • OS: Raspbian 11

    • Java Runtime Environment: OpenJDK Runtime Environment Zulu17.42+19-CA (build 17.0.7+7)
      OpenJDK Server VM Zulu17.42+19-CA (build 17.0.7+7, mixed mode)

    • openHAB version: openHAB 4.0.0 Release Build

  • Issue of the topic: certain things upon restart do not reestablish their links from items to channels
  • Please post configurations (if applicable):
    *Thing:tplinksmarthome:hs103:CE373C
    channels: All, but specifically Power Switch (Switch)
    Items: JoeOffice1_Power
    Link: JoeOffice1_Power to tplinksmarthome:hs103:switch
  • If logs where generated please post these here using code fences: I wish it generated any errors to log, but it doesn’t

Upon restart of OpenHAB this and a similar item->thing:channel link disappear. I would like to run a script after restart completes to check for the existence of the links and if absent restore/recreate them automatically, as I have to restart the service every 4-6 days or the OpenHAB jvm chokes out and hangs.

Better to find the root of the problem rather than a bandaid. Frankly, getting to the bottom of the problem is likely going to be less work over all really. There really isn’t easy access to the Link registry in the rules languages as linking Items to Channels really isn’t something expected to be done in Rule.

It’s important to understand that Links are a separate entity in OH from the Channels and the Items. They exist independently and are even stored in a completely different JSONDB. You can have orphaned Links if the Thing or Channel went away without deleting the Link or if the Item went away without deleting the Link. Links don’t just disappear.

Is it always the same Links?

Do these Links exist in the JSONDB file before restarting and not after?

Do you see anything interesting in the logs particularly around startup or shutdown?

Anything else disappearing on a restart?

openHABian or have you configured ZRAM?

How are you shutting down/restarting?

The first suspect I have is OH crashes before it can save out to the JSONDB or crashes while writing it out and not everything gets saved. Or you yank the power on the device which doesn’t give zram a chance to flush all the changes since the last start to the SD card, losing everything.

I agree you need to find the root cause of why the links disappeared. It should never have happened.

Did you create the link through the UI and not in an .items file?

In any case there’s a PR in JRuby to make linking an item to a channel easier.

To check if an item is currently linked to a thing:

if MyItem.thing
  logger.info("MyItem is linked to #{MyItem.thing}")
else
  logger.warn("MyItem is not linked to a thing")
end

You can also get a list is all the links and their corresponding config (if applicable) using MyItem.links

Yes, always the same links.

If I am looking at the right file: ${OPENHAB_ROOT}/jsondb/org.openhab.core.thing.link.ItemChannelLink.json
Then no, even after I re-add them and wait an hour or so, they never appear in the file.

I did discover several others missing and more that required correction (due to a zigbee controller change which resulted in all new zigbee things so is unrelated)
raspbian with an openHab 4.0.0 pkgs from JFrog stable InRelease

Restart via systemctl restart openhab NOT rebooting the device, and certainly not via power yank… 30 years as a Linux./UNIX admin… power kills are bad mojo and I am fresh out of chickens to sacrifice… plus it’s so messy

I wonder if my jsondb is corrupted?
I’ll search the logs for json errors.

Please excuse me while I take myself out back and shoot me… the Link file in jsondb had suffered a permission change at some point and was unwriteable by openhab… that seems to have been that problem… now just to clear up the internal persistence spew I found in the logs
seems all my outlets with power meters are throwing gibberish at the wall in regards to persistence.

sudo openhab-cli reset-ownership works wonders sometimes. :grinning: