I want to log the daily run time (i.e. in minutes) of a warmer which I have connected to a switch.
The switch is simply defined in .items as “Switch Greenhouse_Warmer …” (it is coupled to a zwave flush switch) - and it is turned ON and OFF by a rule looking at temperature readings once every few minutes. That part all works.
How should I do this run-calculation most easily ? I am sure one of you guys have something like this and can share the code ?
I am on OH1 - but that is probably not relevant here…
OK - came up with a better solution, inspired by older examples and google. Although the example has too much logging etc. - it does works and without running crons every second/minute:
rule "Drivhus2_Warmer started"
Item Drivhus2_Warmer received command ON
logInfo("DrivTemp", "Warmer on")
postUpdate(Drivhus2_Warmer_LastOnState, new DateTimeType())
rule "Drivhus2_Warmer stopped"
Item Drivhus2_Warmer received command OFF
var DateTimeType prevOnState = Drivhus2_Warmer_LastOnState.state as DateTimeType
var Number execDuration = Seconds::secondsBetween(new DateTime(prevOnState.calendar.timeInMillis), now ).getSeconds()
logInfo("DrivTemp","The warmer is now off after runnung for " + execDuration + " secs.")
var Number dailyTotalSeconds = execDuration + Drivhus2_Warmer_Today_Runtime_Sec.state as Number
var Number dailyTotalMinutes = dailyTotalSeconds / 60
rule "Drivhus2_Warmer Daily Reset"
Time cron "0 0 0 * * ?"
logInfo("DrivTemp","Daily runtime counters reset to 0")