Hi,
need bit of help with time … again (still quite confused)
So here is the thing, I’m aware of this topic, which helped in many cases
Now I’m trying to do lastMonth metering summary, which simply still complains about Dates.
So I need first day of last month and last day of last month
val ZonedDateTime zdt = ZonedDateTime.now()
val ZonedDateTime tst = zdt.toLocalDate().atStartOfDay(zdt.getOffset())
val startDate = tst.minusMonths(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0)
val endDate = tst.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).minusMinutes(1)
val startDate2 = now.minusMonths(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0)
val endDate2 = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).minusMinutes(1)
Both works, 2nd version is producing ugly fomat as
I guess historicState is same as minimumSince in terms of logic … maybe not
2022-01-11 14:42:50.494 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'x_system-13' failed: Cannot format given Object as a Date in x_system
No idea which datetype historicstate wants, as i was not able to find what’s wrong.
For example in other rule I’m using this
val lst = (myItem.historicState(now.withYear(now.getYear - 1)).state as Number).floatValue
which works just fine… so maybe I need to define start/end somehow differently
hmmmm do i need to change that to the:
val startTotal = (PVE_Total.historicState(startDate)).state as Number).floatValues
Well, the documents do actually show that you should be using a ZonedDateTime as the argument you pass to historicState and you get back a HistoricItem.
<item>.historicState(ZonedDateTime) Retrieves the State of an Item at a certain point in time (returns HistoricItem)
It goes on to state that
The most useful methods of the HistoricItem object returned by some queries, are .state and .getTimestamp
What needs to be adjusted here?
Assuming what you want is the state of PVE_Total at the startDate, the one that works without errors is the only documented way to get that. As rossko57 says HistoricState is a complex Object consisting of both a state and a timestamp. You can’t use it as if it were a number nor can you use it as if it were a state.
I can’t guess what that error means. There is no context and with the information supplied so far it’s not even certain that it is that line of code that is generating the error as opposed to some line you have later in the rule that tries to use startTotal in a String or to do some math with. It is an odd error but I see no reason why it should occur on that particular line by itself.
The timestamp I was talking about, belonging to the return object, is not the same as the one supplied by you and used to query.
Think about it, it’s really unlikely that any record exists for the exact millisecond that you requested. So the service returns you the next-oldest record with a datestamp of the actual record.
logInfo("test", "Querying " + startDate.toString)
val startRecord = PVE_Total.historicState(startDate)
logInfo("test", "My record state " + startRecord.state.toString)
logInfo("test", "My record stamp " + startRecord.getTimestamp.toString)
Stumbled upon this topic while I was searching for historicState with a given timestamp. Had the same error when used historicState(parse(“2021-12-01T00:00:00.000Z”)). I wanted to give a specific date/time to fetch some info from Influx.