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
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
Can this code be made any shorter, one line like I had before?
as now I have to go through many rules and add all these variables…
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.