Problems getting historic state from item in JS Ruling


I want to get the latest value out of my DB (MariaDB) which was saved the day before at 23:59:59 or shortly before (really the latest sate from DB).

Here is my try:

var local_timestamp_yesterday = new Date(new Date().getTime() - (((new Date().getHours())) * 60 * 60 * 1000))-((new Date().getMinutes()) * 60 * 1000)-((new Date().getSeconds()+1)  * 1000);
var local_number_wallplug3dailytotalenergy = items.getItem("Z_way_number_WallPlug3_totalpower").history.historicState(local_timestamp_yesterday)

But I get the Error

TypeError: invokeMember (historicState) on org.openhab.core.persistence.extensions.PersistenceExtensions failed due to: no applicable overload found (overloads: [Method[public static org.openhab.core.persistence.HistoricItem org.openhab.core.persistence.extensions.PersistenceExtensions.historicState(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String)], Method[public static org.openhab.core.persistence.HistoricItem org.openhab.core.persistence.extensions.PersistenceExtensions.historicState(org.openhab.core.items.Item,java.time.ZonedDateTime)]], arguments: [JavaObject[Z_way_number_WallPlug3_totalpower (Type=NumberItem, State=77.64 kWh, Label=xxxx, Category=energyplug, Tags=[Measurement, Energy], Groups=[group_xxxx, group_xxxx]) (org.openhab.core.library.items.NumberItem)] (HostObject), 1.673823599909E12 (Double)])

Can someone help me?

If you break down the error it becomes apparent what the problem is.

TypeError:: Something is not of a type that histroicState understands. That means something you are passing to historicState isn’t of a type that is understood. Since the only thing you are passing to historicState is local_timestamp_yesterday, that is not of an understandable type.

So what types are acceptable? Reading on we see a list of “overloads” which is the list of different ways you can call historicState:

  • historicState(org.openhab.core.items.Item,java.time.ZonedDateTime,java.lang.String)
  • historicState(org.openhab.core.items.Item,java.time.ZonedDateTime)

In this case the error is coming from below the JS Scripting helper library level but the clue is still there. It wants a ZonedDateTime. In truth everything in openHAB wants a ZonedDateTime. There is basically no reason to use Date, epoch, or any of the myriad of other ways to represent date times because you will have to convert it to a ZonedDateTime eventually.

And even if a ZonedDateTime were not required, that one line of code is really complicated. Here are some much easier to read and understand options:

var local_timestamp_yesterday = time.toZDT().minusDays(1); // yesterday, same time as now
var local_timestamp_yesterday = time.toZDT('23:59:59').minusDays(1); // yesteday at one second before midnight
var local_timestamp_yesterday = time.toZDT().minusDays(1).withHour(23).withMinute(59).withSecond(59).withNano(0); // yesteday at one second before midnight 

Ok I guess the documentation is a liitle bit missleading…But nevertheless it’s running now…

Further question:

Why is


displaying undefined according to here Global - Documentation it should display a number or null?

What documentation did you find misleading?

Do you have the latest version of the openhab library installed? openhab-js has it’s own release schedule independent of OH’s release cycle. What you find in the docs, and especially in the reference docs is the latest which may include things not yet available in the library that ships with the add-on.