ECMA Problem with UoM

Hi there fellow Openhabians :slight_smile:

I’m seeing trouble with my modbus queried solar inverter and its generation value. I want to act on the current generated power, hence I tried as a ECMA script:

var Prod = items["Strom_Erzeugung"];

However, the item Strom_Erzeugung contains f.e, “8.6E+2 W” which, when used in a simple “if (Prod > 300)” does not work. Other items don’t exhibit this behavior so I’m thinking about the modbus binding as a source of the quest. What do you think on this?

Do you have any ideas for me? I’m totally lost on this.
Additionally, I did’nt find UoM handling for ECMA scripts like the DSL rule examples in the documentation https://www.openhab.org/docs/configuration/rules-dsl.html#manipulating-item-states#number-item

Thank you all very much & cheers,
Sascha

What do you want to do with the Item’s state? Compare it to a constant? Compare it to another Item? Do math with it?

It would be useful to confirm the type of your Item, Number or Number:Power?
Generic Modbus binding does not give units by default, but this can be configured.
More likely you are using one of the extensions, which do offer quantity type channels with units?

Thank you both for your pointers to the right direction. I’m sorry I hadn’t been verbose enough :frowning:

The item in question is type Group with a Sum aggregate function of base type Number:Power:

The group only has a single item at the moment, which is ThreePhaseInverter_ACPowerValue.

I’m trying to compare the number of the item’s state to a constant, say 300.
My problem is that the item’s state is not “0” (zero) but “0E+1 W” and this can’t be used in a ecma script like this:

if (items["Strom_Erzeugung"] > 300) {
  events.sendCommand("Light", "ON");
}

I hope to have made my point clear now.

Cheers,
Sascha

It’s trite, but “you can’t do that”. That’s to say, you can’t compare 30W with 300 … 300 what? kW? horsepower? mW?
Your Group state, as you point out, has a unit.
Your constant needs to have units as well, or comparison is meaningless.

if (items["Strom_Erzeugung"] > new QuantityType("300 W"))

This works if your Item is in kW, mW,or indeed horsepower.

This is a more future proof approach than extracting the numeric value for a numeric only comparison (which is possible).

3 Likes

Thank you very much @rossko57. I couldn’t find this in the ECMA context anywhere.

It seems to work - let’s see when sun rises :smiley:

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