i try to get used to the new python script binding in openHAB5, but having some issues with the persistence class.
Goal is to cleanup a sql database which is connected via JDBC (set as default) via the following script:
from openhab import rule, Registry
from openhab.triggers import ItemStateUpdateTrigger, ItemCommandTrigger, when
from datetime import datetime, timedelta
@rule("Test", description="Test")
@when("Item TestTrigger received update")
def Test(module, input):
time_Delta = datetime.now() - timedelta(hours=24)
Registry.getItem("TestItem").getPersistence().removeAllStatesUntil(time_Delta.astimezone())
This throws an error in the logs:
2025-08-03 14:16:27.735 [WARN ] [nce.extensions.PersistenceExtensions] - Querying persistence service with open begin and/or end not allowed: begin null, end 2025-08-02T14:16:27.717+02:00, now 2025-08-03T14:16:27.735567812+02:00[Europe/Berlin]
Not sure why, tho.
According to the documentation it should be sufficient to pass one timestamp to the class, and the “end” timestamp in the log is exactly what i was expecting.
So what argument am i missing for the “begin” timestamp?
if ((begin == null && end == null) || (begin != null && end == null && begin.isAfter(now))
|| (begin == null && end != null && end.isBefore(now))) {
LoggerFactory.getLogger(PersistenceExtensions.class).warn(
"Querying persistence service with open begin and/or end not allowed: begin {}, end {}, now {}",
begin, end, now);
return;
}
the important part for your usecase is (begin == null && end != null && end.isBefore(now))
and the java doc says Removes from persistence the future items for a given <code>item</code> until a certain point in time.
For me it looks like, you can only delete states in the future.