UPDATE: Revised to now also work with openHAB 3.
Hi!
Since I answered this question a couple of times now in private messages I decided to put it here for everyone.
The question that pops up e.g. in this thread about the Miele cloud binding is how to calculate the remaining time and end time of running programmes e.g. of a washing machine. Here’s how.
Items:
Number:Time WEGProgramRemainingTime "Restzeit [%.0f s]" {
channel="mielecloud:washing_machine:0815:program_remaining_time"
}
// Calculated
String WEGProgramRemainingTimeDisplay "Restzeit"
String WEGProgramFinishTime "Endzeit"
And the .rules file doing the “calculation”. Snippet contains both openHAB 2 and openHAB 3 versions. Remove comment marks accordingly.
// OH3
// import java.time.format.DateTimeFormatter
rule "WEGProgramFinishTime"
when
Item WEGProgramRemainingTime received update
then
if (WEGProgramRemainingTime.state == NULL || WEGProgramRemainingTime.state == UNDEF) {
/*
* Some Miele items have an UNDEF state once the machine is off.
*/
WEGProgramRemainingTimeDisplay.postUpdate("--:--")
WEGProgramFinishTime.postUpdate("--:--")
} else {
val duration = WEGProgramRemainingTime.state as Number
val seconds = duration.intValue
val hours = seconds / 3600
val mins = seconds / 60 % 60
val remainingTimeDisplay = String.format("%02d:%02d", hours, mins)
WEGProgramRemainingTimeDisplay.postUpdate(remainingTimeDisplay)
val finishTime = now.plusSeconds(seconds)
// OH2
// val finishTimeDisplay = finishTime.toString("HH:mm")
// OH3
// val formatter = DateTimeFormatter.ofPattern("HH:mm")
// val finishTimeDisplay = finishTime.toLocalDateTime().format(formatter)
WEGProgramFinishTime.postUpdate(finishTimeDisplay)
}
end