Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spent Fuel Calculation Improvement in Reports #5228

Open
victor-butler opened this issue Nov 29, 2023 · 8 comments
Open

Spent Fuel Calculation Improvement in Reports #5228

victor-butler opened this issue Nov 29, 2023 · 8 comments

Comments

@victor-butler
Copy link

Is your feature request related to a problem? Please describe.
The Spent Fuel in the Trips/Stops/Summary reports is currently giving wrong information if there has been a refuel event. Sometimes the Spent Fuel appears as a negative value which makes no sense.

Describe the solution you'd like
Here's the proposal on how to fix this for devices that report fuel level:

  • Create a Total Fuel Counter (totalFuel) similar to the totalDistance which traccar already has
  • totalFuel counter is set to 0 by default
  • Create a currentFuel variable to record the latest fuel level reported by the device.
  • currentFuel is set to 0 by default
  • record the totalFuel in every positions record similarly to totalDistance

Calculations:

  • A new fuel level data is reported by the device (fuel variable in Traccar). Example: "fuel": 50
    So we have fuel = 50; currentFuel = 0; totalFuel = 0;
    Now before adding a new positions record in the database we compare fuel with currentFuel:
if(fuel >= currentFuel) {         //refuel event or no fuel change
   currentFuel = fuel;
} else {                          //calculate fuel usage
   totalFuel = totalFuel + (currentFuel - fuel);
   currentFuel = fuel;
}

Now we have fuel = 50; currentFuel = 50; totalFuel = 0;

  • the next fuel level data that is reported by the device will be "fuel": 49
    The code above will evaluate to: fuel = 49; currentFuel = 49; totalFuel = 1;
    This way we can successfully calculate the total fuel usage along the device lifespan.

Fuel Usage in Trips/Stops/Summary Reports:
Now it is very simple to calculate the fuel usage between the two dates:
Instead of comparing the fuel values between two position records, we compare the totalFuel:

  • get the totalFuel vaulue for Date From: startFuel
  • get the totalFuel value from Date To: endFuel
  • Spent Fuel = endFuel - startFuel

In the example above, where we have only two data records, the Spent Fuel will be: 1-0 = 1.

Let me know if you need further details.

@tananaev
Copy link
Member

It's not that easy. The level can fluctuate and if you keep accumulating, the error will accumulate as well.

@victor-butler
Copy link
Author

victor-butler commented Nov 29, 2023

This will be a problem only if the level fluctuates upwards, I've already tested the scenarios and added a code fix below:

Scenario 1 - fuel goes up:

if(fuel >= currentFuel) {         //refuel event or no fuel change
   if(fuel >= currentFuel + 3){   //add this line to decrease the impact of fluctuating fuel level. 
      currentFuel = fuel;
   }
} 

By adding the comparison with a margin of error 3 (or other small value like 4 or 5), we will decrease the possibility of error due to fluctuating fuel level. Because who really refuels their car with less than 3,4,5 liters?
The threshold can be configurable in the settings for more flexibility.

Scenario 2 - fuel goes down:

} else {            //calculate fuel usage
   totalFuel = totalFuel + (currentFuel - fuel);
   currentFuel = fuel;
}

In this scenario, there is no possibility of an error:

  1. If the fuel goes up by a small amount we are in scenario 1. Then we do not accumulate anything and we also disregard the small fuel change with the code addition that I just posted.
  2. If the fuel goes down, we accumulate just once. Next accumulation will be if the fuel goes down again below the current level.

The only problem would be if the fuel falsely goes up by more than 3, 4 or 5 margin of error.
Then we will wrongly accumulate, I agree but how often would that happen?
Compared to the current negative fuel usage, this will be pretty accurate.

Let me know what you think.

@surafel58
Copy link

surafel58 commented Dec 21, 2023

Dear @victor-butler I wanted to appreciate you for the algorithm you came up with for spent fuel calcualtion I really needed it thank you. I also wanted to ask you that did you test it? or does it really work most of the time? what about if we want to calculate the refuel Volume in a given trips/stops/summary? how can we do that following this algorithm?

@sandreli
Copy link

This is very interesting and would be nice to implement, especially with a configurable variable for the allowed margin. Currently we are trying something similar in computed attributes in order not to have those ugly graphs. However it would be very interesting to have an improved fuel calculation with refuel events

@geduxas
Copy link
Contributor

geduxas commented Jan 24, 2024

I hate old topic's but question was who fill 3-5l. Let me explain, i drive caddy ecofuel, which runs on CNG, and it has extra fuel tank, which is around 10L capacity :D so.. 2-3L fuel up is normal for me :D i think i am not alone.

@sandreli
Copy link

Yes. Exactly why I was suggesting with a device attribute. As there are cases where a couple of litres fuelling is normal, and there are cases where the device is not sending correct info and is registering a 1-2..% increase in fuel which is not right.

@victor-butler
Copy link
Author

victor-butler commented Mar 16, 2024

Dear @victor-butler I wanted to appreciate you for the algorithm you came up with for spent fuel calcualtion I really needed it thank you. I also wanted to ask you that did you test it? or does it really work most of the time? what about if we want to calculate the refuel Volume in a given trips/stops/summary? how can we do that following this algorithm?

It works quite well, deviations are minimal, you can use the variable to adjust. You can get the refuel amount in the same condition we use to check for the refuel event. I haven't done this yet but it's a good idea.

if(fuel >= currentFuel) {         //refuel event or no fuel change
   currentFuel = fuel;
   refuelAmount = fuel - currentFuel;   //refuel amount
}

Let me know if you want me to share to code.

@victor-butler
Copy link
Author

I hate old topic's but question was who fill 3-5l. Let me explain, i drive caddy ecofuel, which runs on CNG, and it has extra fuel tank, which is around 10L capacity :D so.. 2-3L fuel up is normal for me :D i think i am not alone.

Yes, you and the 0.00000001% of caddys used with traccar to measure their enormous fuel consumption. Your example is like standard deviation 5/5 and has nothing to do with the other 99.99999% of regular use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants