@dakoeli: This works for my setup very well! This is exactly what I needed in order to sum up my “uptime firing unit” for calculation of my energy consumption!
I still don’t get it to work. I have made a TestRule which I trigger by Hand:
//val DateTimeType Epoche = now.toInstant()
val DateTimeType Epoche = now.toInstant()
logInfo("TestTrigger", "Epoche 2: " + Epoche)
createTimer(now.plusSeconds(3 )) [|
val DateTimeType Spaeter = now.toInstant()
logInfo("TestTrigger", "Spaeter 4: " + Spaeter)
val DateTimeType Differenz = Duration.between (Spaeter, Epoche)
logInfo("TestTrigger", "Differenz 5: " + Differenz)
// Here I need to convert from "DateTimeType" to a kind of Number. I want the time Difference in Millisecond or seconds......
]
This is working so far. I can calculate timedifferences between 2 given times. I get as result:
Differenz 5: PT-3.003164S
which is fine. Now I need this as Number, e.g. “3000” .
So how can the DateTimeType be converted to a Number???
val Number millis_spaeter = Spaeter.getZonedDateTime.toInstant.toEpochMilli val Number millis_epoche = Epoche.getZonedDateTime.toInstant.toEpochMilli
For rule internal internal calculations I would recommend to stay with the Java 11 classes like “ZonedDateTime” or “LocalDateTime”. The DateTimeType I would only use if I want to populate an Openhab Item with it.
@ljsquare You are right, thanks. I looked up the code of DateTimeType, and it turns out it’s only a thin wrapper around ZonedDateTime. So there is an easy conversion between the two, without the need for any String parsing or formatting.
I’ve rewritten my rule as follows:
val morning_start = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(7) // 7:00
// Java time to DateTimeType:
vMorning_Time.postUpdate(new DateTimeType(morning_start))
// DateTimeType to Java time:
val day_start = (vSunrise_Time.state as DateTimeType).getZonedDateTime()
@anfaenger May I suggest to rewrite section #3 of your start post, as follows?
#3 Get DateTimeType from Java Time
DateTimeType has a conventient constructor method that accepts Java java.time.LocalDateTime objects. So getting a DateTimeType if you have a java.time.LocalDateTime is as easy as:
val LocalDateTime myLocalDateTime = new LocalDateTime();
val DateTimeType myDateTimeType = new DateTimeType(myLocalDateTime);
Sorry for “hijacking” that chat, but I do not come any further.
My Rules for measuring “working times per day” don’t work under OH3 any more. (And I spent a Day now not getting them to work)
I have A switch which can be On or Off. I need to store the time when the switch goes high, and when it went back to low again. Then subtrated it and transfered the result to an Integer (Seconds).
Seconds = MakeSeconds(TimeNow - TimeBefore)
Maybe you can tell me how to do that OH3 conform?
Thanks in advance,
Ingo
@bartkummel Thank you for this hint!
I was not yet familiar with the LocalDateTime class.
Can you have a look at my rewrite? I hope I didn’t mess things up.
I’m still on openHAB 2.4 with my system and therefor can’t test it.
I use a DateTime Item and switched to the MainUI website and configuration. For me it is not clear how to format the DateTime Code in the UI like this
DD.MM.YY, HH:MM
Is there some Metadata config needed or do I have to format it in the UI?
Ok, then I always have to use a second Item?
With the textual configuration I had the chance to directly format DateTime and illustrate it at the sitemap. Is that still possible?
I am just realizing that I have probably misunderstood.
You are probably switching from textual confiuration to another option.
Unfortunately, I can not help.