How to get a value from influxDB in rules

Hi all, is there a way to get a value for an item persisted in an influxDB in rules? Thanks for the help.

Have a look into the REST API ( a usable UI can be installed via PaperUI Addons Misc as “REST Documentation”).
On this UI you’ll find under persistence the GET /persistence/items/{itemname}, putting in all needed data ( serviceId, in your case influxdb, itemname, start- and endtime ) will create a request by hitting the “Try it out” button. The correct http and curl syntax will be shown as well as the result of the request.

Hi opus, thanks for the answer, but I need to get the last persisted value with policy everyChange at the system startup and it’s a bit tricky to establish start time and end time for making the query…Do you have any suggest?

Before digging any deeper, what are you trying? If you just want to restore the last persisted value on startup, it is all there. Use the policy Restoreonstartup for all ( needed) items and openHAB does what you want. Mapdb is the database that is build exactly for such, storing only one (the last) value!

The persistence service views the “last persisted state from 3 minutes before startup” as the last stored state from before that three minutes ago. Even if it were stored weeks ago, the logic being that with no other states stored that must be the state at the target time.

<item>.historicState(AbstractInstant)
where AbstractInstant is essentially (now - 3 mins)

Maybe .previousState() is better? I checked the help at https://www.openhab.org/docs/configuration/persistence.html .

It depends on what you are trying to do that you haven’t told us.
If you want the value from before rebooting, you might need to account for the value having been persisted since reboot, but before you are looking for it.

I want to take a value persisted in the influxDB and set the state of an item with it in a rule at the system startup. This is not possible with the directive “restoreOnStartup” in the persist file.

Then the advice about historicState() stands.
previousState() may give a result from a binding or some other rule or UI update since startup.

Perhaps an example;
Xxx.historicState(now.minusMinutes(5))
If the last entry in the database was an hour or a month ago, that will be returned.
If the last entry is two minutes ago, and the one before is an hour old, you get the hour old one.

So… no way to get simply the latest entry?

Yes, previousState gets the most recent entry. That may not come from before your reboot, which is what you said you wanted. Up to you, the advice is free.

I would call it only in the system startup rule so I think the goal is nearly achieved with previousState: if the rule fires not at startup like in the case of the rule file modified, the problem you’re talking about is still present. I think I’ll use previousState. Thanks.

Raffaello Bonvino
Real-time embedded software engineer