I am struggling with a bit of code in a OH2.1 rule to show me what the time is in “now + XX minutes” ; I have a formula calculating the minutes into the future I need - but I cannot figure out how to use the .plusMinutes to get it working.
Basically, I want to show what the time is when I add the number of minutes I have calculated - to the current time of day.
Here is my draft (which keeps failing):
var Number min_to_comfort = 142.45 // This is just a test - normally there will be a formula behind this
var SimpleDateFormat df = new SimpleDateFormat( "HH:mm" )
var String Timestamp = df.format( new Date() )
var String Time2 = df.plusMinutes(min_to_comfort).format( new Date() )
logInfo("POOL", "Pool heating has been initated at " + Timestamp + ". Comfort temp estimated reached at " + Time2)
Anyone who have done this - or can see what I am doing wrong ?
Always post the errors you are getting or the incorrect behavior you are seeing.
Benjamin is correct, you cannot pass a floating point to plusMinutes, you should use plusSeconds instead or string them together:
now.plusMinutes(142).plusSeconds(27)
It makes no sense to print the TimeStamp because every log statement is already printed with a timestamp.
You can use the standard String Formatters features you have to format DateTime in Item labels so you should be able to do something like:
logInfo("POOL", "Pool heating has been initiated at %1$tH:%1$tM. Comfort temp estimated reached at %2$tH:%2$tM.", now, now.plusMinutes(142).plusSeconds(27))
OK - sorry for not being precise enough. My error went away after using plusSeconds instead.
I now have this rule-snippet working:
var Number tub_min_to_comfort = 148.12212 // This is just a test value. The real value will come from a formula when I know what I am doing ;-)
var Number tub_sec_to_comfort = tub_min_to_comfort.intValue() * 60
logInfo("POOL", "tub_min_to_comfort: " + tub_min_to_comfort.intValue()) // Rounding to full minutes (no decimals)
logInfo("POOL", "tub_sec_to_comfort: " + tub_sec_to_comfort)
var SimpleDateFormat df1 = new SimpleDateFormat( "HH:mm" )
var String TimeNow = df1.format( new Date() )
var DateTimeType TimeComfort = now.plusSeconds(tub_sec_to_comfort)
logInfo("POOL","TimeComfort: " + TimeComfort) // shows: TimeComfort: 2017-12-12T01:07:09.494Z
logInfo("POOL", "Pool heating has been initiated at " + TimeNow + ". Comfort temp estimated reached at " + TimeComfort)
I still struggle with the formatting of the contents of “TimeComfort”. I cannot get this formatted as HH:mm - just like the current time. I have been googling like mad to find examples - but no luck. @rlkoshak I tried your logInfo example - but somehow the %1$tH:%1$tM in the log-text does not get converted to time values?
I would prefer simply to get my calculated timestamp in TimeComfort changed into HH:mm in a string?
var Number tub_min_to_comfort = 148.12212 // This is just a test value. The real value will come from a formula when I know what I am doing ;-)
var Number tub_sec_to_comfort = tub_min_to_comfort.intValue() * 60
var SimpleDateFormat df1 = new SimpleDateFormat( "HH:mm" )
var String TimeNow = df1.format( new Date() )
var String TimeComfortString = (String::format("%02d:%02d", now.plusSeconds(tub_sec_to_comfort).getHourOfDay(), now.plusSeconds(tub_sec_to_comfort).getMinuteOfHour()))
logInfo("POOL", "Pool heating has been initiated at " + TimeNow + ". Comfort temp estimated reached at " + TimeComfortString)
Perhaps there are other ways of doing this - but it solves my need right now. Thanks for sparring with me and leading me in the right direction guys!