Operations between KWH powers

Hi everyone,

I’m trying to do a simple subtraction between two powers. The items are converted to KWH. I’m using OH4. I’m attaching the code because I don’t understand how to make it work. Thank you for your time.


var cValue = (Thing_Generica_MQTT_inverter_enerout_inverter_enerout.state as QuantityType<Power>)
Prodotti_Giornalieri.postUpdate(cValue)
  
var dValue = (Thing_Generica_MQTT_inverter_enerin_inverter_enerin.state as QuantityType<Power>)
consumi_giornalieri_inverter.postUpdate(dValue)
  
risparmiati_enel.postUpdate(dValue - cValue)

The error I get is this:

  1. Ambiguous feature call.
    The extension methods
    postUpdate(Item, State) in BusEvent and
    postUpdate(Item, Number) in BusEvent
    both match.; line 14, column 309, length 10
  2. Ambiguous feature call.
    The extension methods
    postUpdate(Item, State) in BusEvent and
    postUpdate(Item, Number) in BusEvent
    both match.; line 18, column 525, length 10
  3. Ambiguous feature call.
    The extension methods
    postUpdate(Item, State) in BusEvent and
    postUpdate(Item, Number) in BusEvent
    both match.; line 22, column 747, length 10
  4. Ambiguous feature call.
    The extension methods
    postUpdate(Item, State) in BusEvent and
    postUpdate(Item, Number) in BusEvent
    both match.; line 26, column 869, length 10

Can you please post the details of your items you want to substract? Is the dimension power [kW] or energy [kWh]?

If the units are identical (not that sure because of the error messages), you could take “.numericState” to subtract them

The items are expressed in kilowatt hours

And the rule code is for Rule DSL (v1)?
I don’t have experiance with this.
I would use blockly to solve this simple problems.
The error log is more clear then.

This happens from time to time in Rules DSL. You’ll also see this when you postUpdate with a DeciamlType.

What this means is there are two postUpdate functions on this Item, one the takes a State type and another that takes a Number type. But QuantityType is both a State and a Number so there’s nothing to tell the rules engine which postUpdate you really want to call.

To solve it you can tell it which one to call (e.g. consumi_giornalieri_inverter.postUpdate(dValue as Number)) or you can bypass the issue by using toString (e.g. consumi_giornalieri_inverter.postUpdate(dValue.toString)).

I recommend toString because it doesn’t require you to know what types you are dealing with.

Dealing with these sorts of situations is one reason I no longer recommend Rules DSL, now that we have better options with Blockly and such.

Note that in Blockly, the argument passed to postUpdate always ends up as a String. This is one reason why this is never a problem in Blockly or JS Scripting.

I tried Blocky, I had never tried it before, I have to say that it works well, I solved the problem, thank you all

  • kWH is ENERGY
  • POWER is kW
    => these are not inter convertable.