How to create calculated "Time to empty" sensor?

Hello, from my OBD I am getting fuel tank volume in %. How do I create sensor that shows remaining time to empty based average consumption in last 10 minutes? Lets say the sensor is called sensor.obd_fuel_level and it shows something like 18.12345%

I understand I need to create differential sensor with change in last 10 minutes, I can do that. But how then I calculate remaining time from volume and speed of change? Can it be all combined into definition of differential sensor or do I need to create yet another sensor with formula?

the formula would be time_to_empty = 10 * volumet / (volumet-10min - volumet)

THE ANSWER:

sensor obd_tank_diff:
  - platform: derivative
    source: sensor.obd_fuel_level
    unit: "%/min"
    name: OBD Fuel tank change per minute
    round: 2
    unit_time: min
    time_window: "00:10:00"
    
template:
  - sensor:
      - name: OBD Time to empty tank
        unit_of_measurement: "min"
        state: >
          {% set obdfl = states('sensor.obd_fuel_level') | float %}
          {% set obdflspd = states('sensor.obd_fuel_tank_change_per_minute') | float %}
          {{ (obdfl / obdflspd)  | round(1, default=0) }}

Jan

I wrote this

using the % of the battery and working out when to replace the Battery

Then use a template, they are well documented, and also this may help. Derivative - Home Assistant

Thanks. I know python, but this should be solvable with one code in sensor configuration. Too bad I am such an amateur in yaml…

Thanks for advice. As I said, I am yaml amateur. I develop software for 30 years yet the yaml construction of templates is a great secret for me…

It really drives me crazy. Why this statement {% (states('sensor.obd_fuel_level')) %} produces error “TemplateSyntaxError: tag name expected” and not number?