Deprecated "getZonedDateTime" since Update to 4.3.x

Hi, i updated my 4.1.1 to 4.3.3 today an now i get many errors in my rules because of deprecated things…

	if (now.isAfter((Rollos_up_time.state as DateTimeType).getZonedDateTime)) {
            if (Rollos_is_day.state != ON) {
					logWarn("Rollosteuerung", "...
			}
	}

Please can someone help my, what i have to change here?

In another rule i have an error because of this:

var Number stromverbrauch_sdm630_heute = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(0))

ZonedDateTime is also deprecated. What is the new command?

See Release openHAB 4.3.0 · openhab/openhab-distro · GitHub

There might also be some more details to be found in this thread: The method getZonedDateTime() from the type DateTimeType is deprecated

I don’t see any problem with this, nothing has been deprecated here.

Sorry, my fault… error-message:

cannot convert from state to number

But it worked with 4.1.1

For now, to get rid of the warning, you’ll have to call getInstant. But because now is a ZonedDateTime you’ll have to convert now to and Instant or the Instant to a ZonedDateTime.

if (now.toInstant.isAfter((Rollos_up_time.state as DateTimeType).getInstant)) {`

if(now.isAfter((Rollos_up_time.state as DateTimeType).getInstant.atZone(ZoneId.systemDefault()))) {

I can’t remember if it was 4.2 or 4.3, but all the persistence actions were updated to return values with units ins tead of just a number. As a result, you’ll have to cast the result and call floatValue or use the value with units.

This was mentioned in the Breaking Changes section in the release notes for 4.2.

How can i cast the result of my val to floatValue? What do i have to add to my line from the first post?


And i have seen this in 4.2 release notes:

averageSince, averageBetween
, deltaSince, deltaBetween, deviationSince, deviationBetween, sumSince, sumBetween, varianceSince, varianceBetween:
These methods now return a PersistedState instead of a number - use the numericState property to get the numeric state as before.

What do i have to change here? Is this the change you told me?

For completeness, as mentioned in the Breaking Notes, there is also a convenience method for getting a ZonedDateTime in the specified time-zone:

if (now.isAfter((Rollos_up_time.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault()))) {

Thanks, this worked, when i tried it yesterday.

But the following isn´t working:

var Number stromverbrauch_sdm630_heute = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(0))

I don´t know how i can do this with floatValue or do it with units included. For calculations i think floatValue without units would be easier to do…

var stromverbrauch_sdm630_heute = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(0)) as QuantityType<?>

var plus5watts  = stromverbrauch_sdm630 + | 5 W

if(stromverbrauch_sdm640 > | 5 W) {

It’s no harder to work with units than it is to work without.

If you don’t have a use for units, remove the UoM from your Item and no units will be involved. Instead of Number:Temperature use just Number and remove the unit metadata.

In a rule:

var stromverbrauch_sdm630_heute = (SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(0)) as QuantityType<?>).floatValue

Note how I removed the type from the variable. Do not try to force the type of variables in Rules DSL except where you have to. It causes problems otherwsie.

My items are numbers only, there is no one with UoM.

So why do i have to remove it then?

It is working now, thanks a lot.


I added the floatValue to this line, in vscode there is no error, but in the log i get errors:

var count_today = (HeatPump_Stromzaehler.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT)) as QuantityType<?>).floatValue / 2000
[ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'strom_heatpump-2' failed: Could not cast 11449.625 to org.openhab.core.library.types.QuantityType; line 42, column 22, length 97 in strom_heatpump

My last problems are only warnings, no errors:

stromzaehler0_hausverbrauch_vor6tagen.postUpdate(stromzaehler0_hausverbrauch_heute.historicState(now.minusDays(5).with(LocalTime.of(0,0,0,0)), "rrd4j").state)

warning:

The method historicState(Item, ZonedDateTime, String) from the type PersistenceExtensions is deprecated

When i change historicState to numericState, i get an error:

The method numericState(ZonedDateTime, String) is undefined for the type NumberItem

What is the right way to solve this? When i don´t change this, will this break my rule in a new openhab version in the future? I think now i can use old and new way and in future versions i have to use the new way only?

OK, if you aren’t using UoM at all then the result from persistence is going to be a DecimalType instead of a QuantityType<?> so you need to cast to that instead.

This was also in the breaking changed announcements. Because persistence now supports future timeseries (e.g. upcoming energy prices, weather forecasts) historicState was renamed persistedState.

Yes, in OH 5 most likely historicState will no longer be there.

Again, thanky you very much. One last warning and then i´m ready for oh 5…

rule "Update Xiaomi Temp time since"
when
    Time cron "0 * * * * ?"
then
	if(SystemStarting.state == OFF) {
		gXiaomiZeit.members.forEach[ DateTimeItem lastTime |
    		val timeSinceItemName = lastTime.name.replace("_last_connection", "_time_since")
    		if (lastTime.state instanceof DateTimeType) {
      			val mins = java.time.Duration.between((lastTime.state as DateTimeType).zonedDateTime, now).toMinutes
      			postUpdate(timeSinceItemName, mins.toString)
    		} else {
      			postUpdate(timeSinceItemName, "")
    		}
		]
	}
end

Warning:

The method getZonedDateTime() from the type DateTimeType is deprecated

Warning is because of the

val mins = ...

line.

This is the same issue as in your first post, just using a different syntax. Try this:

val mins = java.time.Duration.between((lastTime.state as DateTimeType).getZonedDateTime(ZoneId.systemDefault()), now).toMinutes