Blocky variables numbers only

Hey community! So this is probably a very basic scripting question but I’m still struggling.

I want to populate some variables with numbers from items. I then want to compare those numbers in the variables to other numbers or variables. The issue is, I cannot get rid of the unit behind the number, W in this case, which makes it impossible to compare the numbers later in the logic.

The global project is to create a PV surplus charging rule with a SolarEdge inverter and Go-e Wallbox. I have basically access to all the data I need.

In the following case I populate the variable “SolarEdgeImport” with the value of the item “SolarEdge_PrivateAPI_Import”. This works ok so far. The issue is that I get the number with the unit W. So for example “1200 W”. Unfortunately I cannot create any logic with this. Same with the charge current in Amps (A) or battery state of charge in %.

Is this something I have to modify on the item config itself?

I’m glad for any hint in the right direction.

Here’s a basic test rule I created.

or the script

var SolarEdgeBatterySoC, SolarEdgeBatteryDischarge, SolarEdgeImport, SolarEdgeExport, AllowCharging, BatteryMinSoC, Phases, ChargeCurrent;


SolarEdgeBatterySoC = items.getItem('SolarEdgePrivateAPI_BatteryLevel').state;
console.info(SolarEdgeBatterySoC);
SolarEdgeBatteryDischarge = items.getItem('SolarEdge_Private_API_Battery_Discharge').state;
console.info(SolarEdgeBatteryDischarge);
SolarEdgeImport = items.getItem('SolarEdgePrivateAPI_Import').state;
console.info(SolarEdgeImport);
SolarEdgeExport = items.getItem('SolarEdgePrivateAPI_Export').state;
AllowCharging = items.getItem('goe_forcestate_switch').state;
BatteryMinSoC = items.getItem('goe_battery_min_soc').state;
Phases = items.getItem('GoeCharger_Phases').state;
ChargeCurrent = items.getItem('GoeCharger_Maximum_Current').state;
console.info(ChargeCurrent);
if (AllowCharging == 'ON') {
  if (SolarEdgeImport > 200 || SolarEdgeBatteryDischarge > 200) {
    if (ChargeCurrent > 6) {
      items.getItem('GoeCharger_Maximum_Current').sendCommand((ChargeCurrent - 1));
      console.info('Charging current has been reduced by 1 A');
    } else if (SolarEdgeBatterySoC <= BatteryMinSoC) {
      items.getItem('goe_pv_surpluscharging_switch').sendCommand('OFF');
      items.getItem('goe_forcestate_switch').sendCommand('OFF');
      console.info('PV surplus charging has been stopped due to no export');
    }
  }
}

And the item config:

Which specific version of openHAB?

In case you’re on OH4:
Use the get name of item-block and select numeric state
image

1 Like

yes I’m on OH4! Great I will try that!

Alternatively to @Larsen 's suggestion, you can use the get quantity state from the item(s) and use the blocks in the units of measurement category of blocks to do the comparisons and math on the values with the units.

An advantage there is you can mix and match compatible units and still get the right answer. It’s also a little bit more self documenting.

2 Likes