Differences in mileages

I’m trying to calculate the difference between 2 mileages. This is done by number:lenght.
My problem is that the current state is in ‘km’, and the previous state is just a number.
I tried KmA ‘as Number’, ‘as Quantity’, ‘as DecimalType’. But this doesn’t change anything? :blush:
Any tips what the best way is to do this properly?

Item

Number:Length Mileage "KM-teller [%,d %unit%]" {channel="bmwconnecteddrive:CONV:user:XXX:range#mileage" }

Rule

var KmA = Mileage.state as QuantityType
   logInfo("Verbruik", "KmA: " + KmA )
var KmB = Mileage.historicState(now.minusDays(1)).state
   logInfo("Verbruik", "KmB: " + KmB )
var KmC = ( KmA - KmB )
   logInfo("Verbruik", "KmC: " + KmC )

Log

2020-12-04 15:42:19.523 [INFO ] [ipse.smarthome.model.script.Verbruik] - KmA: 103726.0 km
2020-12-04 15:42:19.524 [INFO ] [ipse.smarthome.model.script.Verbruik] - KmB: 103531.0
2020-12-04 15:42:19.524 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'km difference': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_minus(org.eclipse.smarthome.core.library.types.QuantityType,org.eclipse.smarthome.core.library.types.QuantityType) on instance: null

You will need to convert the KmB’s HistoricState back to a QuantityType…

var KmA = Mileage.getStateAs(QuantityType)
logInfo("Verbruik", "KmA: {}", KmA)
var KmB = Mileage.historicState(now.minusDays(1)).state
logInfo("Verbruik", "KmB: {}", KmB)
var KmC = KmA - new QuantityType(KmB.toString + KmA.getStateAs(QuantityType).unit.toString)
logInfo("Verbruik", "KmC: {}", KmC)
1 Like

Error:

2020-12-04 17:29:45.462 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'km difference': 'getStateAs' is not a member of 'org.eclipse.smarthome.core.library.types.QuantityType'; line 100, column 51, length 28

Almost right. :blush:
In your formula, the second kmA has to be Mileage. :wink:
So at the end, I’m having:

var KmA = Mileage.getStateAs(QuantityType)
logInfo("Verbruik", "KmA: {}", KmA)
var KmB = Mileage.historicState(now.minusDays(1)).state
logInfo("Verbruik", "KmB: {}", KmB)
var KmC = KmA - new QuantityType(KmB.toString + x1Mileage.getStateAs(QuantityType).unit.toString)
logInfo("Verbruik", "KmC: {}", KmC)

Thanks you very much !

I find it a bit weird that you have to calculate with the units. Would expect the way around.
Next step, calculate the average use of the car. So mileage and liters… :wink:

Oops… KmA is already a QuantityType…

var KmC = KmA - new QuantityType(KmB.toString + KmA.unit.toString)

:+1:

I have exactly the same rule, but in Jython!

1 Like

OK, the calculation with mileage goes automatically? Even the units are filled in correctly? :blush:
Went much faster (=easier) then the historical state.

My end script for consumption car:

rule "Mileage difference"
when
        Item carAMileage changed
then
        var KmA = carAMileage.getStateAs(QuantityType)
        var KmB = carAMileage.historicState(now.minusDays(1)).state
        var KmC = KmA - new QuantityType(KmB.toString + carAMileage.getStateAs(QuantityType).unit.toString)

        var LiA = carAFuel.getStateAs(QuantityType)
        var LiB = carAFuel.historicState(now.minusDays(1)).state
        var LiC = new QuantityType(LiB.toString + carAFuel.getStateAs(QuantityType).unit.toString) - LiA

        if ((LiC > 0) && (KmC > 0)) {
                var carAVerbruik = ( KmC / LiC )
                carAVerbruikCalc.postUpdate(carAVerbruik)
                logInfo("CarA", "Wagenverbruik A: {}", carAVerbruik)
                }
end

And the result:

  Wagenverbruik A: 18.8 km/l
1 Like