Knowing when an Item was just added

Running openhabian on Pi4
openHAB 3.4.0 Build #3029
jython rules

Running 3.4 (yes still :slight_smile: ) … I use Meta data to track various information on an Item and I wanted to find a simple / elegant way to automatically populate the Meta data when a new Item is added. I know that I will get an “was updated” event when it is added, and I could query the Meta to see if it exists, then update it … but I was hoping there would be a way to know it was just added or newly added…

Any help would be appreciated!

I don’t know if this existed or was available in 3.4 and Jython. But if you can create a GenericEventTrigger, there is both an ItemAddedEvent and ItemRemovedEvent (see Event Bus | openHAB).

To do this you’ll probably have to look into the helper library and see how it creates triggers and create access to the GenericEventTrigger and figure out how to filter on the openhab/items/*/added eventTopic.

Note, every time you touch a .items file your rule will get pounded with ItemAddedEvents as all the Items in that file (and more) are deleted and recreated. It’s much less of a problem with a managed config.

Note, I would strongly consider whether it’s worth spending any more effort on Jython or not. Sadly Jython should be treated as deprecated. It might be a better use of your time to start to explore one of the other options. Why is it deprecated?

  • The upstream Jython library is all but abandonded.
  • It still only supports Python 2.7, four years after Python 2 reached end of life. Python 3 support is still nowhere to be found.
  • The Python Helper Library never became part of the openHAB project itself and it was basically abandoned. A fork of it was created with changes to make it compatible with OH 3 but it hasn’t been maintained since so for all intents and purposes the Helper Library is unmaintained.
  • The primary developer of the Jython add-on has abandoned it as well. The other maintainers are doing the bare minimum to keep it working but at some point it’s going to break and no one is going to fix it. From the upstream library down to the Helper Library it’s a story of abandonment.

Someday, maybe soon, maybe not, it’s going to break and when it does no one is going to be able to fix it. Any users of Jython are on borrowed time. Already in OH 4 there are new features Jython doesn’t support.

Spending this time to gradually migrate to something else (JS Scripting and jRuby are great built in choices, HABApp is a great Python option, and even Blockly is basically feature complete) rather than adding new Jython code would be a good idea. Better to do it gradually rather than when you are not under the gun than after everything breaks.

1 Like

Thanks as always Rich for your thoughtful response.

I’ll check it out.

Yes. Agreed. I got a 2nd Pi 4 which I have running OH 4 now … it barfed on a lot of things and haven’t spent anytime yet on it. I did see a migration guide so will look at that.

My plan would be to move the scripts over to JS and maybe HabApp as a first step. I will use my 2nd PI to play around. I going to be out of the country for 3 months and will rely on my current workhorse to watch the house for me :slight_smile:

Other than a small glitch here and there, 3.4 has been very stable (but I want to move up to OH4)

Wise words of caution. Thanks!

Jruby has an item_added trigger amongst others

1 Like

While JS Scripting doesn’t have a separate ItemAddedTrigger, it’s super easy to set up with the GenericEventTrigger. It’s fairly easy to user in the UI too.

I just don’t remember if Jython has it

1 Like