Any way to access the data that the UI uses for the Analyze option?

Curious if there is a way to access the data that the Analyzer Widget uses to create the graphs?

Running openHAB 3.2.0.M4 using Jython scripting

For instance, how can I know (programatically) which motion sensor had the most recent activity? Seems that unless I create an Item and store the date/time for each sensor triggers, there is not another way.

Do I have that right?

It’s all Persistence | openHAB.

Be sure to look at the Persistence Actions, one of which is “lastUpdate” which returns the most recent value stored in the database. Take note of that last sentence, it’s really important.

By default OH 3 uses rrd4j configured to store every Item every minute and on every change. That’s what drives the charts. However, because it saves every value every minute it makes it unsuitable for your use case. The most recent value in the database is going to be no more than one minute ago regardless of when the Item last changed.

Therefore you’ll want to use a different persistence for this. In this case, assuming you can use rrd4j for other historic data and calculations (e.g. averageSince, minSince, etc.) I recommend using MapDB. This persistence engine only saves the most recent value. However, there could be a conflict between rrd4j and MapDB’s default persistence strategies. Both will save the states of all Items and restoreOnStartup which means that Items will be restoredOnStartup twice during a book and which one updates any given Item second is not defined.

Using the link above, create a mapdb.persist file to save only those Items you care about on every change. Alternatively, create a rrd4j.persist file to save all Items every minute and on every change but do not use restoreOnStartup and let MapDB handle that. MapDB is better for that anyway because it can handle Item types that rrd4j cannot like String Items.

1 Like

Ok … I currently have a MapDB.persist file that I use to restore various Items on startup:

Strategies {
  default = everyChange
}

Items {
   Default_Evening 		   	: strategy = everyChange, restoreOnStartup
   Default_Night   			: strategy = everyChange, restoreOnStartup
   Default_Sleep   			: strategy = everyChange, restoreOnStartup
   ThermostatCT101_DoorOpenMinutes 	: strategy = everyChange, restoreOnStartup
   ThermostatCT101_HeatOverrideMinutes 	: strategy = everyChange, restoreOnStartup
   . 
   .

If I add a line for a Motion Sensor Item like:

ComputerDeskMotion : strategy = everyUpdate

how would I then access the .lastUpdate … it’s not part of the ir.getItem(xyz) … I’m confused.

Also:

In the case of this Motion sensor, it doesn’t matter if the state is restored twice etc … since it’s a motion sensor, it will be updated on next activity … right?

If you are using Python I assume you are using the Helper Libraries so: Actions — openHAB Helper Libraries documentation

Assuming both rrd4j and MapDB agree on what the state of the Item should be.

1 Like

:roll_eyes: Sorry, I seem to have a mental block to look there first, thank you again

… ok so I can access the .lastUpdate … but it doesn’t seem to matter if I have the Item in the MapDB.persist file or not, the .lastUpdate is always every minute. The MapDB file is being reloaded after the change, but do I need to restart the system for it to take affect? (I doubt it)

Which of your services did you tell it to look at? It can guess by default, but might not be right.

I am using the following:

debug.log.info("Last Motion: " + str(PersistenceExtensions.lastUpdate(ir.getItem("HallBath_Motion"))))

So, it seems I need to somehow specify the MapDB persistence … I guess it defaults to rrd4j?

That’s it, yep.
To repeat the docs link already offered -

Did you supply “mapdb” as the second argument in the call to lastUpdate? You have to tell it which database you want to query or else it will select the default one which is and needs to remain rrd4j.

If you move it off of rrd4j the charting in MainUI will fail as it can only generate charts from the default database.

2 Likes

right … but when I try:

debug.log.info("Last Motion: " + str(PersistenceExtensions.lastUpdate(ir.getItem("HallBath_Motion"),"MapDB")))

I get:

2021-11-23 10:42:08.498 [WARN ] [nce.extensions.PersistenceExtensions] - There is no queryable persistence service registered with the id 'MapDB'

Obviously I am confused.

Well, like so many things it’s case sensitive. “mapdb”

1 Like

Thanks, that was it.

I was using MapDB, silly me :slight_smile:

This is important … yes i saw I could change the default … glad i did not

Ok just to wrap this up, (thanks @rlkoshak and @rossko57 ) …

But it I don’t need to add the Motion sensor to the MapDB in order to get the lastUpdate. Seems it works for all sensors regardless if they are in the MapDB.persist file.

So for someone else, I added the MapDB Persistence, then all I did was :

from core.actions import PersistenceExtensions
@rule("Testing Various crap", description="playing around")
@when("Item ComputerDeskMotion changed to ON")
def debug(event):

    debug.log.info("Last Motion: " + str(PersistenceExtensions.lastUpdate(ir.getItem("HallBath_Motion"),"mapdb")))

If that is literally how your file is named it means that MapDB is using the default strategies which is to save all Items on every change with restoreOnStartup. Your .persist file is being ignored. Here too case matters.

mapdb.persist

Hmm ok … but when originally installed the MapDB for the TimeOfDay defaults etc and for some other items, they seems to be working as I would expect (ie. restored correctly after reboot) … sounds like I need to go back and check that.

but, it must be correct that I dont need to specify the Motion sensors in a mapdb.persist file to get the .lastUpdate to work … since it’s working.

When there is no mapdb.persist file MapDB will use the default strategy which is to save all Items on every change and restoreOnStartup.