diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java index 257b18c14b6..e979b5a8b43 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java @@ -41,6 +41,7 @@ import org.openhab.core.persistence.QueryablePersistenceService; import org.openhab.core.types.State; import org.openhab.core.types.TimeSeries; +import org.openhab.core.types.TypeParser; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -147,6 +148,42 @@ private static void internalPersist(Item item, ZonedDateTime timestamp, State st .warn("There is no modifiable persistence service registered with the id '{}'", effectiveServiceId); } + /** + * Persists a state at a given timestamp of an item through the default + * persistence service. + * + * @param item the item to store + * @param timestamp the date for the item state to be stored + * @param stateString the state to be stored + */ + public static void persist(Item item, ZonedDateTime timestamp, String stateString) { + internalPersist(item, timestamp, stateString, null); + } + + /** + * Persists a state at a given timestamp of an item through a + * {@link PersistenceService} identified by the serviceId. + * + * @param item the item + * @param timestamp the date for the item state to be stored + * @param stateString the state to be stored + * @param serviceId the name of the {@link PersistenceService} to use + */ + public static void persist(Item item, ZonedDateTime timestamp, String stateString, @Nullable String serviceId) { + internalPersist(item, timestamp, stateString, serviceId); + } + + private static void internalPersist(Item item, ZonedDateTime timestamp, String stateString, + @Nullable String serviceId) { + State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString); + if (state != null) { + internalPersist(item, timestamp, state, serviceId); + } else { + LoggerFactory.getLogger(PersistenceExtensions.class).warn("State '{}' cannot be parsed for item '{}'.", + stateString, item.getName()); + } + } + /** * Persists a timeSeries of an item through the default persistence service. *