Math in Rules on OH3... I don't get it

Hi,

i had a rule in OH2.5 that worked fine:

rule "Aktueller Verbrauch Trigger 2"
when
	Item AC_CURRENT_POWER received update
then
	DSVerbrauchHausAktuell = (((AC_CURRENT_POWER.state as Number).floatValue) + ((DSWirkleistung.state as Number).floatValue))
    //logInfo("Aktueller Stromverbrauch",(DS_Verbrauch_Haus_Aktuell.state as DecimalType).floatValue)
end

Now with OH3, I am not able to figure out how to get this to work again
.state is no longer required, but then it creates an error, floatValue is not recognized.
Without floatValue, it cannot cast NumberItem to number.
Without Number it cannot recognize the item…

Err… at this point my brain stops working and trows an error… read though the docs and the tutorials, still cannot figure this out. Can somebody please help me back on track here?
Thanks a lot!

No there is nothing changed. You will definitely need .state to get the state of an Item. You will need to cast that state to Number to get the method .floatValue.

What is DSVerbrauchHausAktuell? Is it a global var?
If it’s an Item, you have to (and always had to!) use .postUpdate(value) instead of using =
So the rule should lok like that:

rule "Aktueller Verbrauch Trigger 2"
when
	Item AC_CURRENT_POWER received update
then
	DSVerbrauchHausAktuell.postUpdate((AC_CURRENT_POWER.state as Number).floatValue + (DSWirkleistung.state as Number).floatValue)
end

You will not need additional brackets around the values.
Please be aware that it is very likely not to get the new value, if logging the state right after updateing the state, as this is an asynchronous task. So better use a lokal var:

rule "Aktueller Verbrauch Trigger 2"
when
    Item AC_CURRENT_POWER received update
then
    var nVerbrauch = (AC_CURRENT_POWER.state as Number).floatValue + (DSWirkleistung.state as Number).floatValue
    DS_Verbrauch_Haus_Aktuell.postUpdate(nVerbrauch)
    logInfo("current","Aktueller Verbrauch (Strom): {} kWh",nVerbrauch)
end

It is a variable
var Number DSVerbrauchHausAktuell

Funny enough, that this is the only rule that does not work anymore…

As the Item is probably a Quantity type like Number:Power, you might want to review -

Work with the quantity, instead of struggling to strip it out.

(What’s changed from OH2 to OH3 is that a great many more binding channels supply quantities, instead of simple numerics)

I read that post of yours already :wink:
Due to the items being defined via textual config, there is no Quantity assigned, as far as I can tell.
With the help of Udo’s example, I got it to work again. Don’t know, why it is working again now, since it did not for two days, and Udo was only changing the way I am doing the logging.

What remains is the mystery, that this variable no longer gets updated within the sitemap… which it did in OH2 without problems…
It is defined as a simple Text Element:

Text  item=DSVerbrauchHausAktuell				label="Aktuelle Last"					icon="energy"

Find out. The thread does caution that both Number type Items and Number type variables may be loaded with a state with units. But you don’t get full quantity functionality, so e.g. doing maths breaks down,

Log your values out before trying to process them, and you may find out.

Changed the variable name to make it less complicated, and this version works (again)

var Number DSVerbrauchHausAktuell

rule "Aktueller Verbrauch"
when
    Item AC_CURRENT_POWER received update
then
    var nVerbrauch = (AC_CURRENT_POWER.state as Number).floatValue + (DSWirkleistung.state as Number).floatValue
    DSVerbrauchHausAktuell = nVerbrauch
    logInfo("current","Aktueller Verbrauch (Strom): {} kWh",nVerbrauch)
end

Any ideas why the corresponding item in de Sitemap doesn’t get updated?

Check again what Udo has written before

What is DSVerbrauchHausAktuell? Is it a global var?
If it’s an Item, you have to (and always had to!) use .postUpdate(value) instead of using =

As I wrote before, it is a varable and the math works again, thanks to Udo.
The value is calculated again and i can see it in the logs as well as in Influx/Grafana. It is just that the value is not updated in the sitemap for no obvious reason. So I don’t know where to look for that…

You have a variable and an item with the same name.
That’s what you have wrote before and where maybe the confusion is coming from:

From your rule:

var Number DSVerbrauchHausAktuell

From your posting regarding the sitemap:

Text  item=DSVerbrauchHausAktuell				label="Aktuelle Last"					icon="energy"

If this item is part of the sitemap, you need to issue an postUpdate or sendCommand in order to update the item. There is no automatic update or link between a variable and an item

1 Like

Ah, now I understand. Had to reboot my brain from yesterday :wink:

Thanks, now I got this fixed as well.

Only question for me is, why on earth did this work with OH2.5… or maybe it never did but I never realized that really… lol.

Thanks again guys for this excellent support and the awesome OH project.