Hey Everyone.
So the newest version of the Amazon Echo Controle Binding has a channel called “Next Timer”
When you set a timer on a given echo device, it creates a date and time stamp in openhab as a Date/Time item in the following format:
2019-10-20T19:58:36.026-0600
I am wondering if there any ways to convert this date and time variable into a countdown that I can show in habpanel. Essential a visual representation of the timer.
Perhaps taking that date and time, cross-referencing with the current time, and creating a variable timer for the difference.
This might be way out there ion a limb but I thought there are probably many people here that use echo timers and have habpanels.
Even some direction on a post about converting time and setting timers would be really helpful.
I created a rule to start by working out the time now, and the time of the timer:
rule "timer"
when
Item KitchenEchoNextTimer received command
then
val timerString = KitchenEchoNextTimer.state.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS")
val currentTime = String::format( "%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS", new java.util.Date )
logInfo("timer.rules", timerString)
logInfo("timer.rules", currentTime)
end
This ouputs the following is ISO 8601:
2019-10-20 20:22:54.404 [INFO ] [e.smarthome.model.script.timer.rules] - 2019-10-20T21:58:35
2019-10-20 20:22:54.404 [INFO ] [e.smarthome.model.script.timer.rules] - 2019-10-20T20:22:54
Now I just need help with showing the difference between these two strings. Or do I have to do the logic with the actual numbers…confusing
UPDATE
I figured out how to do it… with some stumbling around forum posts and online, and the results are amazing.
If anyone wants to help me clean up this rule (as I am sure it’s written horribly!) I would appreciate the help.
To clarify what this rule and template widget does:
- I set a timer on my kitchen echo
- my dashboard changes to a countdown dashboard that displays the time left on the timer in minutes (I like minimal design)
3. The timer counts down the minutes until it reaches 0
4. If I want to go back to the home dashboard at any time I just click the number, as it is a button with the timer as the text value
5. When the timer reaches 0 (and the echo is beeping) it switches the dashboard back to my home dashboard
Now you dont need to buy an echo show!
Here are the Items:
String CurrentTimer "Current Timer"
DateTime BasementEchoNextTimer "Basement Echo Next timer" {channel="amazonechocontrol:echo:xxxxxxxx:xxxxxxxxxxxxx:nextTimer"}
Here is the rule:
var Timer timer = null // global variable usually
rule "timer"
when
Item KitchenEchoNextTimer changed
then
timer = null
timer = createTimer(now, [ |
// Timer Date and time 2019-10-20T21:58:35.870-0600
val timerString = KitchenEchoNextTimer.state.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS")
// parse String to Date Time
var DateTime timestring = parse(timerString)
// find out time left in minutes
val timeleft = (((timestring.millis + 60000) - now.millis) /60000)
// null the timer if time left is 0
if(timeleft <= 0) timer = null
else {
// update the timer Item with time remaining
CurrentTimer.sendCommand(timeleft)
// Switch the Dashboard to the Timer Dashboard Holding the Timer Template Widget
Pin_String.postUpdate("Timer")
// reschedule the timer for 1 minute
timer.reschedule(now.plusMillis(60000))
}
])
end
rule "Update Display Timer Iterm"
when
Item CurrentTimer received command
then
// Give the item a half second to register the change in time left
Thread::sleep(500)
//Log the time left
logInfo("alexatimer", CurrentTimer.state.toString + " time left on the Echo Timer")
// create a second timer when the first timer has reached 1 minute left to switch the dashboard when the timmer reaches null
if( receivedCommand == "1" ) {
createTimer(now.plusMinutes(1), [ |
// Switch the Dashboard to the Home Dashboard
Pin_String.postUpdate("Home")
])
}
end
Here is the template widget:
<button class="timertime" style=""
ng-click="sendCmd('Pin_String', 'Home')">{{itemValue('CurrentTimer')}}
</button>
<style>
button.timertime.ng-binding {
background: transparent;
border: none;
color: #fff;
padding: 200px;
}
.timertime {
font-size: 400px;
line-height: 250px;
}
</style>