In case you are struggling with the “Minutes running” parameter I can show you how I solved this:
-
I created a Runtime Item
-
I set this Item to ‘1’ when the washing starts in the script that determines the washing machine state:
var threshold = new QuantityType("5 W")
var zero = new QuantityType("0.1 W")
if (newState <= zero) {
events.sendCommand('washing_machine_state', 'Aus')
} else if (newState >= threshold && oldState < threshold) {
events.sendCommand('washing_machine_state', 'Waschvorgang')
events.sendCommand('washing_machine_runtime', 1)
} else if (newState < threshold && oldState >= threshold) {
events.sendCommand('washing_machine_state', 'Waschvorgang beendet')
}
Why do I not set it to 0? This is because I determine the state from the average consumption over the last five minutes so my “Washing” state is detected too late anyway and I’m compensating a bit for that. However it is really not important to be super precise in that case (at least in my opinion). If you have a real smart device you may be able to use a better solution, I only use a Shelly Plug S to make my dumb machine “smart”.
- Update the minute item with a cron rule for every minute as long as the machine state is “RUNNING” (you need mapdb or another persistance provider for that):
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("scripts.washing.runtime");
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var Duration = Java.type("java.time.Duration");
var lastUpdate = PersistenceExtensions.lastUpdate(ir.getItem('washing_machine_state'), "mapdb");
logger.info('Washing machine state last changed at ' + lastUpdate);
var minutesSinceLastUpdate = Duration.between(lastUpdate, ZonedDateTime.now()).toMinutes();
logger.info('Washing machine state changed ' + minutesSinceLastUpdate + ' minutes ago');
events.postUpdate('washing_machine_runtime', minutesSinceLastUpdate);
Two questions from my side:
- Does anyone know how the condition handling in rules is implemented? Is it just like an if condition and the rule fires anyway? I’m asking because I don’t want to use rules that fire every single minute excessively (even though they don’t really do much then).
- Why does MapDb know when the item has lasst changed when I haven’t configured persistence for this item? Is this:
a) The default behaviour or
b) Am I persisting all items states by accident/wrong configuration?