Rule not working since the upgrade from 4.0 to 4.2

Hi,
any reason this:

kWh_Today.postUpdate ((kWh.deltaSince(now.with(LocalTime.of(0,0,0,0)))).floatValue)

does not work anymore?

2024-08-11 13:47:59.388 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'energy-1' failed: 'floatValue' is not a member of 'org.openhab.core.types.State'; line 5, column 27, length 60 in energy

i tried to fix it by using Udo’s repair from:
floatValue error after upgrade from 4.1 to 4.2 - Setup, Configuration and Use / Scripts & Rules - openHAB Community

this is my final rule

rule "Energy Usage Today" 
when
    Time cron "0 0/21 * * * ? *"
then
    val ZonedDateTime now = ZonedDateTime.now()
    var ZonedDateTime start = now.toLocalDate().atStartOfDay(now.getZone())
    kWh_Today.postUpdate ((kWh.deltaSince(start, "influxdb") as Number).floatValue)
    //kWh_Today.postUpdate ((kWh.deltaSince(now.with(LocalTime.of(0,0,0,0)))).floatValue)
end

what is ,influxdb ? I am not sure this will work, if I have defined the name of my influxdb and what is it’s name…

EDIT: OK it seems its working, the number is increasing.
So I guess now we need to mention the persistence which is used to retrieve data, as you see I was not using this in the code before, it’s something new to me :slight_smile:
Can this code be made any shorter, one line like I had before? :slight_smile:
as now I have to go through many rules and add all these variables…

also this is not working anymore

kWh_Month.postUpdate ((kWh.deltaSince(now.minusDays(31))).floatValue)

what would be the new format?

just add as Number :slight_smile:

kWh_Month.postUpdate((kWh.deltaSince(now.minusDays(31)) as Number).floatValue)

nice, thanks!
i found it in release 4.2 thread discussion, but over there they say you need to add “,influxdb” as well. Needed or not?

also the other one

old:

kWh_Today.postUpdate ((kWh.deltaSince(now.with(LocalTime.of(0,0,0,0)) as Number)).floatValue)

when I just add as Number it complains cannot cast ZonedDateTime as Number

new:

kWh_Today.postUpdate ((kWh.deltaSince(ZonedDateTime.now().toLocalDate().atStartOfDay(ZonedDateTime.now().getZone())) as Number).floatValue)

this monster works, is it all needed?

edit, i manage to shorten it to:

kWh_Today.postUpdate ((kWh.deltaSince(now.toLocalDate().atStartOfDay(now.getZone())) as Number).floatValue)

No you don’t (at least…)
The thing is: there is an optional parameter:

item.deltaSince(timestamp[,"<persistence>"])

where <persistence> is the name of the source to use for the delta.
Keep in mind, that you can use as many persistence services as you want, individually per Item.

There is a default persistence which can be set via Main UI->Administration->Settings->Configuration (left column)->Persistence->General Settings.
So you only need to set the optional parameter if it differs to the Standard Persistence Service.

The only change from 4.0 to 4.2 is, that now persistence will always use QuantityType for return values if available, so you have to cast to Number to get the method .floatValue.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.