Hello,
I have just started to work with DateTimeType calculation and it’s really being a big deal for me: this page about rules warned me
https://docs.openhab.org/configuration/rules-dsl.html#datetime-item
and this post confirmed me
anyway I tried.
I use OpenHAB 2.3.0, installed from the repository, and it runs in a Raspberry 3 with Raspbian 8.
My aim is to create a chart (X axis-Time, Y-axis hour:minute of the day[0:00-23:59]) where to show at what time, everyday, a particular event happens (the event is the trigger channel of the Astro binding - thing sun, group rise, event START - but could be something else too).
I already created a chart with similar characteristics showing at which azimuth the sun rises every day (X axis-Time, Y-axis azimuth angle (0-359]) and it works as expected.
So, I created these items
DateTime RiseStartTime “sun rise start time: [%1$tH:%1$tM]” {channel=“astro:sun:home:rise#start”}
DateTime ProvaTime “prova time: [%1$tH:%1$tM]” (TimeChart)
I created this persitence line
ProvaTime : strategy = everyChange, restoreOnStartup
and, according to the rule page i linked before, I created this rule
rule “sun program”
when
Channel ‘astro:sun:home:rise#event’ triggered START
then
val time = (RiseStartTime.state as DateTimeType).calendar.get(Calendar::HOUR_OF_DAY)
ProvaTime.postUpdate(time)
end
(I know that with this function I would get just the “hour” and not “hour:minutes” anyway I started in this way to see what would happens)
The item doesn’t get updates and in openhab.log appear this rows
2018-07-18 16:03:14.108 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘house_piossasco.rules’, using it anyway:
The method getCalendar() from the type DateTimeType is deprecated
2018-07-18 16:04:00.008 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘sun program’: The name ‘Calendar’ cannot be resolved to an item or type; line 49, column 83, length 8
Then I thought that maybe the problem was the itemtype of ProvaTime and so I changed it to Number, but I kept on receiving the same error.
I searched for examples in internet and I understood that the old function about the DateTimeType are changing and the old form is now deprecated. Anyway the function reported in the rule page doesn’t work for me so I looked in this page
https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-
and I realized that probably the function that would allow me to reach in the easiest way my goal is the “set(int year, int month, int date)” function: everytime the rule is triggered I could take the RiseStartTime item, apply the set(0,0,0) function to give YEAR=0, MONTH=0 and DAY=0, postupdate ProvaTime with this value (with just the not null information about hours, minutes, seconds and eventually milliseconds) and persist this value once a day.
So I set again the items and the persitence line as I wrote before, and, according to the rule page i linked before, and I modify the rule in this way
rule “sun program”
when
Channel ‘astro:sun:home:rise#event’ triggered START
then
val time = (RiseStartTime.state as DateTimeType).zonedDateTime.toInstant.set(0,0,0)
ProvaTime.postUpdate(time)
end
but the item ProvaTime doesn’t get updated and I can see this error in openhab.log
2018-07-18 16:49:00.008 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘sun program’: The name ‘time’ cannot be resolved to an item or type; line 66, column 39, length 4
I tried again a lot of ways and functions but I don’t know how to write in a good way this DateTimeType functions. The only one that I managed to use and reach the expected result is the “.zonedDateTime.toInstant.toEpochMilli”, but in order to reach my aim I can’t use just this one; I have to manipulate the incoming event time data erasing the information about year, month and day, in order to compare in the chart just the moment of the day when everyday the event happens.
I hope to have explained everything in a good way, try to work with these DateTimeType calculations is not easy without a clear documentation. Has anybody any suggestion that could help me?