I’d like to be able to convert a dateTime item, such as dusk, to the minute of the day for use in a rule.
I’ve tried breaking out the hour and minute but I’m in Java number types hell, and figure there must be an easier way, but haven’t been able to get the right combination of search terms working to find an answer…
Soo aggravating… I’ve spent literally 3h going down different paths trying to get what should be a simple number to get.
I can get the hour and minute as numbers, but why can’t I multiply them?
rule "test rule 1"
when
Item TestSwitchX changed
then
val number astro_hour = (LocalSun_Set_StartTime.state as DateTimeType).format("%1$tH")
val number astro_minute = (LocalSun_Set_StartTime.state as DateTimeType).format("%1$tM")
val number dusk_minute = (astro_hour * 60) + astro_minute
logInfo("test", dusk_minute)
end
returns this error:
Rule 'test rule 1': Unknown variable or command '*'; line 73, column 31, length 15
There are several issues in your rule. What are you trying to achieve? It looks like you are trying to get the number of minutes between midnight and sunset?
// add imports before global variables and rules
import java.time.temporal.ChronoUnit
rule "test rule 1"
when
System started
then
val sunset_zdt = civilDuskStart.getStateAs(DateTimeType).zonedDateTime
val midnight_zdt = sunset_zdt.toLocalDate.atStartOfDay(sunset_zdt.getZone)
val minutes_from_midnight = midnight_zdt.until(sunset_zdt, ChronoUnit.MINUTES)
logInfo("Rules", "minutes between midnight and sunset: {}", minutes_from_midnight)
end