Get value from 24h ago or 2 days ago with ZonedDateTime and Oh3

Hello, under OH 3.1 I try to find the value of an Item 24 hours ago with “historicState” and its persistence “influxdb”.

To do so, I created a “ZonedDateTime”.
That I converted to “DateTime”.
Then I remove 24h from my “DateTime”.
Its working, but isn’t there a cleaner way to remove a “ZonedDateTime” from 24h? or remove 2 days? like we did under OH2?

here my code:

rule "Test"
Item ProdSolaire received update

    // ZonedDateTime
    val ZonedDateTime Maintenant =
    logInfo("Maintenant", Maintenant.toString)

    // ZonedDateTime to DateTimeType
    val DateTimeType MaintenantToDateTimeType = new DateTimeType(Maintenant)
    logInfo("Maintenant_DateTime", MaintenantToDateTimeType.format("%1$td du %1$tm à %1$tHh%1$tM"))

    // Usage de ZonedDateTime dans la recherche dans l'historique il y as 24 h
    val conso1 = (ZWaveNode019ZW095EnergyConsToday.historicState(Maintenant.minusHours(24),"influxdb").state as Number ) //Production hier
    logInfo("conso24", "conso hier=" + conso1)


Thanks for your help

val conso1 = (ZwaveNode019ZW095EnergyConsToday.historicState(now.minusDays(1), "influxdb").state as Number

While some of the methods have changed names, how you use ZonedDateTime is pretty much exactly the same as you used Joda DateTime in OH 2.

thanks and for formating ZoneDateTime to string can you tell me how whe can log ? whithout convert to DateTime and use “.format” ? or a link where i can learn ?

For exemple how i can format for log these string with ZoneDateTime :

val ZonedDateTime Maintenant =
// like :
logInfo("Maintenant",Maintenant.format("%1$td du %1$tm à %1$tHh%1$tM"))

thanks in advance for your time spend to help me :- :+1:,java.lang.Object...)

1 Like

With your great links I was able to format a “ZoneDateTime” less 1 day and formatting as “String”

val ZonedDateTime Maintenant =
    logInfo("Maintenant",String.format("%1$td du %1$tm à %1$tHh%1$tM", Maintenant.minusDays(1)))

//return :  "01 du 07 à 16h59"

also I was able to use “historicState” less than 24 hours directly on a “ZoneDateTime”.

val conso1 = (ZWaveNode019ZW095EnergyConsToday.historicState(Maintenant.minusHours(24),"influxdb").state as Number ) //Production 1 jours
    logInfo("conso24", "conso hier=" + conso1)

//return : the good one

Thank you !