Possible doing math in HABpanel code?


I want to sum the values of 3 items (3phase energy meter) and display them in a custom widget.
Is this possible? I already tried this by rule and DSL script but couldn´t get that working.

Result of the below script: Script execution of rule with UID ‘PowerSumRule’ failed: rule “PowerSumRule”.

rule "PowerSumRule"
    Item Shelly3EMSHEM3_L1 changed or
    Item Shelly3EMSHEM3_L2 changed or
    Item Shelly3EMSHEM3_L3 changed
    PowerSum.postUpdate((itemValue('Shelly3EMSHEM3_L1') as Number) + ((itemValue('Shelly3EMSHEM3_L2') as Number) + ((itemValue('Shelly3EMSHEM3_L3') as Number))

That might be because you are trying to treat Quantity values (with units) as just numeric.
Item types are important here.

@rossko57 Yes that sounds like a problem. So this should be something like this?

    var l1 = Float::parseFloat(itemValue('Shelly3EMSHEM3_L1').split(" ").get(0))
    var l2 = Float::parseFloat(itemValue('Shelly3EMSHEM3_L2').split(" ").get(0))
    var l3 = Float::parseFloat(itemValue('Shelly3EMSHEM3_L3').split(" ").get(0))

I don’t know; your important Item types (and some maybe example states) are still secret.

Should they happen to be Number:Energy or similar, work with the quantity value system instead of against it.

Wouldn’t it be easier, to put those items in a group and let the group logic do everything for you?

1 Like

@Sascha_Billian anything that keeps me away from scripts will be better I assume :wink: problem is I don´t even know how to sum it there

@rossko57 thanks for the link, yes I should start with the basics, I can´t even get

rule "PowerSumRule"

to update my item :frowning: … at least it is not logging any error

Your Item type is still secret. Is it sensible to post a string it?

@rossko57 shelly:shellyem3:Shelly3EM:meter1#currentWatts (Number:Power)

@rpwong You know I came to openHAB with 3.0 so thanks for the hint, I expected there would be some arithmetic functions I just have no clue where to use these. In groups which whyever are also called model.

EDIT: okay I did it now, with the blocky scripting for idiots :wink:

var l1, sum, l2, l3;

l1 = itemRegistry.getItem('Shelly3EMSHEM3_L1').getState();
l2 = itemRegistry.getItem('Shelly3EMSHEM3_L2').getState();
l3 = itemRegistry.getItem('Shelly3EMSHEM3_L3').getState();

sum = l1 + l2 + l3;

events.postUpdate('PowerSum', sum);

I’m not sure what you mean. Group items have existed since long before OH3.

When you create a group item in MainUI and indicate that the members are numbers (with or without dimensions), you can set how the data is aggregated.

Hm that looks comfy but my group window doesn´t show any group settings. Anyway, I got a solution now. I´ll keep the group thing in mind. Might be helpful in other situations. Especially with the min/max. thanks

You have to set the Members Base Type first, since Aggregation Function only applies to numbers.