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?
Any tips what the best way is to do this properly?
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.
In your formula, the second kmA has to be Mileage.
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…
OK, the calculation with mileage goes automatically? Even the units are filled in correctly?
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