[SOLVED] QuantityType Dimensionless conversion gives validation error

To convert a QuantityType Item (Dimensionless) to an integer I use the following statement in a rule:

val int CurrentCO2level = (LR_NHC_CO2.state as QuantityType<Dimensionless>).intValue

The rule works fine and gives the right result, but the validator in VS Code Extension flags it as an error, stating Dimensionless cannot be resolved to a type. [org.eclipse.xtext.diagnostics.Diagnostic.Linking].

Is there a better way of converting it, that does not give an error in VS Code?

Have you tried this?

val int CurrentCO2level = (LR_NHC_CO2.state as Number).intValue
1 Like

OK, that was way too obvious for me. :grinning:

It shows I was trained in programming when strongly typed was the norm and type casting considered dangerous because of different processor types…
Meanwhile I’ve been reading up on types in Xtend/DSL and I understand that QuantityTypes (and DecimalTypes) are derived from the Number type.

BTW: If I comment-out the line with the type error, it is still shown in VS Code as an error.

You can post this as a bug in the VSCode openHAB repo.

Can you tick the solution, please? Thanks

val int CurrentCO2level = (LR_NHC_CO2.state as QuantityType<Number>).intValue

So far as I understand dimensionless, it means a ratio not a pure number, so for example a percentage that you might convert to ppm or dB

When I display the Item it shows the state as “1011 ppm”:

LR_NHC_CO2 (Type=NumberItem, State=1011 ppm, Label=CO2 level, Category=...

When I print the QuantityType like this:

..., "QuantityType value={}", (LR_NHC_CO2.state as QuantityType<Dimensionless>).intValue...

It shows:

...QuantityType value=1011

The same value as produced by:

val int CurrentCO2level = (LR_NHC_CO2.state as Number).intValue

It looks like there is no conversion involved for Dimensionless other than removing the unit symbol (ppm in my case). Since there are 3 different dimensions that are represented by Dimensionless according to the docs, I would expect that no unit conversion can take place for this QuantityType as it can be different things.
In case of percentage, that would mean that the receiving end of a percentage value just ‘knows’ it is a percentage (like Dimmer or RollerShutter).

< offtopic>
There are, again, intermittent errors reaching the API service of Netatmo :frowning_face:

2019-01-05 08:16:40.735 [ERROR] [nternal.handler.NetatmoDeviceHandler] - Unable to connect Netatmo API : 404 Not Found

</ offtopic>

You should print it this way:

..., "QuantityType value={}", LR_NHC_CO2.state.toString...

When trying to reproduce this (after VS Code was restarted this morning), the issue did not occur anymore, so I’ll wait and see if something similar occurs.