Profile to invert Channel value in Item

I want to create a Number:QuantityType Item whose value is the negative of a specific Channel value. This would be an ideal use for a profile; but apparently OH has no such profile. ?? Or am I missing something.

PS in case someone wants to state the obvious: yes of course I know I could do this with a rule…

You could use a script transformation profile

Many thanks for the quick response. But could you kindly provide a quick example? :slight_smile:

Show me your item’s definition (yaml? .items?). What scripting language do you prefer / have installed?

Using jruby:

Number:SomeDimension MyItem { channel="xxxxxx" [ profile="transform:RB", toItemScript="|" ] }
1 Like

The issue is related to the binding which I am writing for the Growatt solar inverter. The inverter is connected to a storage battery. Natively the inverter channels include positive Number:Power values for both the battery charging, and battery discharging power. But I would like my OH item to display the discharge power as a negative value (for nicer y-axis display on a chart).

I am using .items file for the item definitions as below…

// items as currently defined
Number:Power Charge_Power "Charge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:charge-power"}
Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:discharge-power"}

// desired: invert discharge value via profile
Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {[profile="pleaseInvertMe"] channel="growatt:inverter:908bf52bdc:XXXX:discharge-power"}


Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:discharge-power" [ profile="transform:JS", toItemScript="| Quantity(input).multiply(-1)" ] }
1 Like

Brilliant! Many thanks. (but probably you missed a ] at the end…)

I’m glad you were paying attention :wink:

1 Like

I just tested it in real life and unfortunately the item is not updated (its value remains null) … I guess due to a syntax error in the item’s profile config parameters => Is this documented anywhere?

EDIT: perhaps the syntax should be something like…

  • | Quantity(input).multiply(BigDecimal.valueOf(-1)), or
  • |, or
  • |, or
  • |"0 W").subtract(input), or
  • | QuantityType.valueOf(0, Units.WATT).subtract(input), etc…

Try | console.log(input) what’s the input value like?

2023-09-12 14:44:06.011 [INFO ] [org.openhab.automation.script       ] - 70 W

Oh I remember JS Quantity is a bit different. Try

Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:discharge-power" [ profile="transform:JS", toItemScript="| Quantity(input).multiply(-1).toString(); " ] }

Thanks for the suggestion.

I was changing the Item configuration parameters on the fly in my running system, and not noticing any change in the item state results. I think I am still suffering from the recently reported bug where on the fly changes in .things or .items files are not automatically implemented. Aargh!

So I tried your above suggestion again with intervening system restarts.

I am also suffering from the current bug where after a restart the JS module is sometimes loaded after the bindings’ things and items. So initially the transforms (sometimes) did not work. However once the system is running stable they do indeed start to work. Aargh!

With intervening restarts and waits for the system to settle, I tried out a whole load of alternate scripts, and it seems that the following do indeed work.

Many thanks for your help. :slight_smile:

// your revised suggestion
Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:discharge-power" [ profile="transform:JS", toItemScript="| Quantity(input).multiply(-1).toString();" ] }

// alternate approach also works
Number:Power Discharge_Power "Discharge Power [%.0f W]" <energy> {channel="growatt:inverter:908bf52bdc:XXXX:discharge-power" [ profile="transform:JS", toItemScript="| Quantity(\"-\" + input).toString();" ] }