A topic that has been mentioned by occasion in this forum is how to ‘time offset’ item states as they are persisted in the database.
My use case is similar:
- One Item (bound to my Homewizard P1 energy meter Thing) contains my current power usage (in W) of my home
- A second Item contains the amount of energy (in Wh) used in the last 15-minute block. This value is calculated by a rule that triggers every 15 minutes (using a third Item that records absolute energy usage from the same Thing).
(Why is this useful? Well, my grid operator charges me partially based on the highest energy usage in such a time block)
But this means that, say I have an peak in my power usage between 10:05 and 10:10.
The rules triggers at 10:15, calculating and storing the peak in energy usage at 10:15 (and until the next calculation at 10:30), so in the time block after the one where the peak actually occured.
This makes it quite hard to work with, or to accurately display on a graph (together with my first power usage item).
As far as I can tell, there is currently no way to store an Item value with any other timestamp than the current time.
Looking at how the RRD4jPersistenceService is currently implemented, I made the following modification:
replace both occurances of:
long now = System.currentTimeMillis() / 1000;
with a call to a new method:
private long getTimestamp(String name) {
Instant timestamp = Instant.now();
Metadata metadata = metadataRegistry.get(new MetadataKey(SERVICE_ID, name));
if (metadata != null) {
try {
String offset = (String) metadata.getConfiguration().get("timestamp-offset");
if (offset != null) {
timestamp = timestamp.plus(Duration.parse(offset));
}
} catch (Exception e) {
logger.warn("Could not parse timestamp-offset value '{}': {}", metadata.getValue(), e.getMessage());
}
}
return timestamp.getEpochSecond();
}
Then, by adding a ‘rrd4j’ namespace to the relevant item with configuration (the offset is in ISO-8601 duration notation),
timestamp-offset : -PT15M
the Item state seems to be correctly stored with the provided time offset.
Is this something worth pursuing? If so, I can create a PR for this.