Get the value from an item runs into several errors

Hi,
i´m using OH 4.0.1 on Ubuntu.

I need the “power export” value from my solar energy inverter to set the maximum charge power to the wallbox. A first test looks fine:

rule "Set max. power charge from PV"
when
    Item Sungrow_WR_Modbus_Export_Power received update
then
	logInfo("RULE",Sungrow_WR_Modbus_Export_Power.state.toString)
end

Result:

2023-08-16 16:22:49.419 [INFO ] [org.openhab.core.model.script.RULE  ] - 4861 W
2023-08-16 16:22:54.685 [INFO ] [org.openhab.core.model.script.RULE  ] - 4859 W
2023-08-16 16:23:00.003 [INFO ] [org.openhab.core.model.script.RULE  ] - 4819 W

I don´t know how to convert i.e. “4819 W” to a integer value. I´ve tried different ways (replace, parse::integer, …). All ends with errors.

Does anyone have an idea for this?

Regards

Why do you need to convert it to an integer?

If you plan on using units at all, you should use the units. If you don’t want to mess with units, change the Item type to a Number and that will strip off the units.

In those rare cases where you do need the raw value without units (e.g. need to pass the value to some function that only takes Number) you can use

(MyItem.state as Number).intValue

Note, this is how it works in Rules DSL. Other languages have other approaches.

In Rules DSL, to define a constant with a unit use |:

if((MyItem.state as QuantityType<?>) > 1100|W)

Hi Rick,

thanks for your fast support.
Your suggestions worked all for me!

Regards.