spacemanspiff2007 / HABApp

Easy home automation with MQTT and/or openHAB
Apache License 2.0
54 stars 23 forks source link

Most Efficient Way To Capture OH Item/Thing/Link Changes #403

Closed alfista2600 closed 10 months ago

alfista2600 commented 11 months ago

Hi @spacemanspiff2007 !

Not sure if this is the best place to ask this question...

I have a few scripts that import OH Items/Things/Links into a database that I can easily query/sort/update/export/link to foreign data/etc. to look for assorted issues as I hone my environment.

what is the best practice to capture all changes (through UI, API, HABapp) to these elements so I can update the database in real-time instead of periodic import?

spacemanspiff2007 commented 11 months ago

You get the change events in HABApp for items/things so updating a database should be easy because you an register an event listener in a rule. The only thing you have to do is to re-query openHAB for the link when you get an event but the item endpoint is available in the rule. And a one time sync on startup would probably also make sense ...

alfista2600 commented 11 months ago

Thanks for the reply. Do I have to iterate through every item/thing and create a listener for each or is there a way to do them all at once? I still don't really speak the language but suspect EventFilter plays a role. I do have Things being monitored this way, but it seems clumsy to do this for 1800 items, groups, etc. I guess I could do some more filtering so I am only monitoring certain items.

I have been doing lots of updates through the API and creating/updating items from text so I'm trying to find a quick way produce a history of changes.

spacemanspiff2007 commented 11 months ago

No - it's only one listener on openHAB.Items (I think). There you listen for the ItemAddedEvent, ItemRemovedEvent and ItemUpdatedEvent with the EventFilter (or you can just listen to all events and check the instances in your function). For the Things there is a different topic openHAB.Things (I think) and the events are named differently. You fill your db once initially and then only track the updates with the events. Do you see the topics/events?

alfista2600 commented 11 months ago

Ahh, great.

Yes, I think I see these under:

HABApp.openhab.events.item_events.ItemUpdatedEvent ?

Yes, I currently run a script that uploads everything periodically, but it takes a while and it seemed it would be more fun if it was possible to have HABApp involved. I'll give this a try later today!

spacemanspiff2007 commented 10 months ago

Does it work as expected?

alfista2600 commented 10 months ago

I believe so. I only had a few minutes to tinker before having to focus on other less-fun projects