Using PersistenceService in Binding

Hi,

Wondering how to get “PersistenceServiceRegistry” service component in order to interact with Persistence binding like InfluxDB, RRD, etc. I tried the following code but not able to get the service:


    @Reference
    PersistenceServiceRegistry persistenceServiceRegistry;

In what context? Given this category is seems you are talking about in a binding but it’s not absolutely clear.

My understanding, at least for now, Bindings are not allowed access to the PersistenceService. And if you are creating a binding that depends upon that, consider the fact that it is not required to have any persistence service at all. What does your binding do in that case?

Technically you can use constructor injection in the thing handler factory and then pass it to a thing handler:

@Component
public class FooHandlerFactory extends BaseThingHandlerFactory {

    private final PersistenceServiceRegistry persistenceServiceRegisty;

    @Activate
    public FooHandlerFactory(@Reference PersistenceServiceRegistry psr) {
        this.persistenceServiceRegistry = psr; 
   }

   ...
}

But as @rlkoshak said: Bindings are not allowed to directly interact with persistence services. I don’t think that something like that has a chance of getting merged.

It seems that I can access the persistence service through @Activate function as mention by @J-N-K . The reason I need to access persistence service so that I can inject historical data into items.

Still, it is not allowed to do this. Bindings shall never work with historical data.

Bindings don’t have access to items either.

1 Like

There is itemlinkedregistry component that we can access them.

Technically anything can be accessed from a binding. But the OH architecture is built around layers of abstraction and interfaces. Just because it’s technically feasible for a binding to access Items and Persistence does not mean that any binding that does so would be accepted by the OH project.

So if you’ve no plan to release this binding as an official add-on to the openHAB project, you can do what ever you want. But if you do have such plans, your binding must follow the restrictions and policies of the openHAB project which dictate that Items and Persistence are not to be accessed from add-ons.

1 Like

There is an issue for adding support of historic state updates from bindings, see:

Also partially related PR: [influxdb] Partial ModifiablePersistenceService support by seime · Pull Request #12935 · openhab/openhab-addons · GitHub (still open) which mentions changing startlevel to 79 to start the persistence service before the bindings.

By also changing the startlevel to 79 (before bindings), it allows the persistence service to be called from bindings to persist previous (ie offline sensor time series being reported when sensor is offline) or future time series data such as electricity prices or weather forecasts.