Probably problem with item formatting in rules

Hi,
I have issues comparing item from a binding with my own item. I don’t know where the problem sits hence even post subject is hypothetical.
I installed the binding WeatherUnderground replacement (for PWS data uploaders only!) and it nicely loads relevant data. Then I need to play with forecasts to control sprinklers and tried to create rule to compare PrecipitationChance against my threshold. I created test rule with test items as below:

rule "Test"
when
        Item test2 changed to ON
then
        logInfo("Test", "Value: " + LocalWeather_ForecastDay0Day_PrecipitationChance.state.toString)
logInfo("Test", " Day Prec: " + LocalWeather_ForecastDay0Day_PrecipitationRain.state.toString)
 logInfo("Test", " Day Chance: " + LocalWeather_ForecastDay0Day_PrecipitationChance.state.toString)
 logInfo("Test", " Night Prec: " + LocalWeather_ForecastDay0Night_PrecipitationRain.state.toString)
 logInfo("Test", " Night Chance: " + LocalWeather_ForecastDay0Night_PrecipitationChance.state)
 logInfo("Test", " szansa dzien: " + szansa_opadu_dzien.state.toString)
 logInfo("Test", " szansa noc: " + szansa_opadu_wieczor.state.toString)
 logInfo("Test", " prognoza: " + prognoza_opadu.state.toString)
        if (test1.state == ON && LocalWeather_ForecastDay0Night_PrecipitationChance.state >= szansa_opadu_wieczor.state )
                                 {
                                sendCommand(test3, ON)
                                 }
end

Items which I’m comparing are defined as follows:


Number szansa_opadu_wieczor "Szansa opadu wieczór [%.0f %%]"

and the item from binding (I’ve configured it in PaperUI as biding gives such a possibility) is (from GitHub):

<channel-type id="precipitationChance">
		<item-type>Number:Dimensionless</item-type>
		<label>Precipitation Chance</label>
		<description>Chance of precipitation</description>
		<state readOnly="true" pattern="%.0f %unit%" />
	</channel-type>

in rule debug (limited to relevant output only) I am getting

2019-04-28 22:13:41.039 [INFO ] [.eclipse.smarthome.model.script.Test] -  Night Chance: 50 %
2019-04-28 22:13:41.051 [INFO ] [.eclipse.smarthome.model.script.Test] -  szansa noc: 50

needless to say that states of test1/test2/test3 items are properly set and rule works if I remove part for comparision of the preciptiationChance. I guess it has something to do with types (especially that debug displays (50 % vs 50) but since definition of items is number with similar formatting I don’t know what and how to postprocess to fit each other. I will appreciate any hints here.

Okay, so one Item is Number:Dimensionless and one is Number, so they do not compare directly.
You change your Items to be matching types, or you might change the comparison

ok, thanks! let me try. I subconciously expected this kind of thing but wasn’t sure how units work

it is so confusing with this UoM types.
Now after changing to:

 if (test1.state == ON && (LocalWeather_ForecastDay0Night_PrecipitationChance.state as QuantityType<Number>).intValue >= szansa_opadu_wieczor.state )
2019-04-28 23:18:47.665 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Test': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_greaterEqualsThan(int,byte) on instance: null

Rules uses different comparisons, depending on the nature of the operators. Here you’ve tried integer against Number, I believe. Try

if (test1.state == ON && (LocalWeather_ForecastDay0Night_PrecipitationChance.state as QuantityType).decimalValue >= szansa_opadu_wieczor.state )

or

if (test1.state == ON && (LocalWeather_ForecastDay0Night_PrecipitationChance.state as QuantityType).intValue >= szansa_opadu_wieczor.state.intValue )

none of this works actually, it throws either:

Rule 'Test': 'intValue' is not a member of 'org.eclipse.smarthome.core.types.State';

or

Rule 'Test': 'decimalValue' is not a member of 'org.eclipse.smarthome.core.library.types.QuantityType<java.lang.Number>

not being a day-to-day developer I don’t really feel it. IMHO it became truly complex to understand how to compare two values which seem to be very close to each other.

My mistake, .doubleValue is what you want from the UoM Item

if (test1.state == ON && (LocalWeather_ForecastDay0Night_PrecipitationChance.state as QuantityType<Number>).doubleValue >= szansa_opadu_wieczor.state )

or you get the other Item state as a Number to do the intValue part

(LocalWeather_ForecastDay0Night_PrecipitationChance.state as QuantityType<Number>).intValue >= (szansa_opadu_wieczor.state as Number).intValue )

It’s always possible to look and see what other people use, in this forum

@rossko57, thanks - the second method works (the first one throws error as above). Obviously before asking I had a look at the forum at similar cases but honestly I couldn’t find anything straightaway similar to mine. I’m technical but not a day-to-day coder and have to say these mappings introduced lots of complexity. I guess there was a good reason for it but still. Thanks for your help!

I would describe comparison methods for UoM as “clumsy”, at best.