Hi, thanks !
The thing is that I am not “in love” with Java.
The code is from blockly, I can read and understand, but write…
I google a long time, and found a lot of examples, but every script gives me an error
@htttito I don’t quite understand the maths / what you’re trying to calculate, but here’s your code translated to jruby
java_import java.time.ZonedDateTime # not needed in openhab 3.3
java_import java.time.temporal.ChronoUnit # not needed in openhab 3.3
midnight = ZonedDateTime.now.truncated_to(ChronoUnit::DAYS)
verbrauch_1minute = Shelly3EMSHEM3_KumulierterVerbrauch.average_since(1.minute)
if verbrauch_1minute.negative?
Minute_Tageseinspeisung.update(Minute_Einspeisung.sum_since(midnight) / 60_000)
elsif verbrauch_1minute.positive?
Minute_Tagesverbrauch1.update(Minute_Verbrauch.sum_since(midnight) / 60_000)
end
To get ZonedDateTime for the midnight of that day you can use what @rossko57 suggested, or ZonedDateTime.now.truncateTo(ChronoUnit::DAYS), it’s just a matter of preference.
My code is finished, and there are no errors.
I don’t find a possibility to write it with Blockly.
I want to answere yesterday but I have Problems with the mysql time…
So here it is:
var dtf = Java.type("java.time.format.DateTimeFormatter");
var LocalTime = Java.type("java.time.LocalTime");
var zdt = Java.type("java.time.ZonedDateTime");
var persistence = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');
function getZonedDateTime (datetime) {
return zdt.parse(datetime + ' 00:00:00 +00:00', dtf.ofPattern('yyyy-MM-dd HH:mm:ss z'))
}
if ((persistence.averageSince(itemRegistry.getItem('Shelly3EMSHEM3_KumulierterVerbrauch'), zdt.now().minusMinutes(1))) < '0') {
events.postUpdate('Minute_Tageseinspeisung', ((persistence.sumSince(itemRegistry.getItem('Minute_Einspeisung'), zdt.now().with(LocalTime.MIDNIGHT))) / 60000));}
else if ((persistence.averageSince(itemRegistry.getItem('Shelly3EMSHEM3_KumulierterVerbrauch'), zdt.now().minusMinutes(1))) > '0') {
events.postUpdate('Minute_Tagesverbrauch1', ((persistence.sumSince(itemRegistry.getItem('Minute_Verbrauch'), zdt.now().with(LocalTime.MIDNIGHT))) / 60000));}