Need help with Ohms or Voltage to a Percentage Sensor

hi i finally got a fuel sending unit to work had some help and then needed to figure out some attenuation

but i wanna a make a fuel gauge card 0% to 100%
either i wanna use the voltage or ohms
voltage
2.25v to 0%
0.74v to 100%
sensor.esphome_web_a02894_fuel_tank_sensor

or
35 to 100%
260 to 0%
sensor.esphome_web_a02894_fuel_tank_voltage

and i wanna increment by 1 i tried googling and reading others battery templates but i didnt understand it convert,

https://esphome.io/components/sensor/index.html#calibrate-linear

Note- Anyone who knows me knows that I couldn’t come up with it myself. I cheated and found a similar solution.

Try this in your configuration.yaml:

template:
  - sensor:
      - name: "Fuel Percentage"
        unit_of_measurement: "%"
        state: >
          {% set voltage = states('sensor.esphome_web_a02894_fuel_tank_sensor')|float(0) %}
          {% set min_voltage = 0.74 %}
          {% set max_voltage = 2.25 %}
          {% set percentage = ((max_voltage - voltage) / (max_voltage - min_voltage)) * 100 %}
          {{ [0, percentage, 100]|sort|join(' ') }}

@stevemann ah any help is good… i tried the other link by tom but that didnt help me made me more confused…
so i tried this… but got error… i change the fuel_tank_sensor to fuel_tank_voltage as the other is for ohms
but i got this error complaining about class or string or something do you happen to know what is neeed?

Error while dispatching event for sensor.esphome_web_a02894_fuel_tank_voltage to <Job track state_changed event {'sensor.esphome_web_a02894_fuel_tank_voltage'} HassJobType.Callback <bound method TrackTemplateResultInfo._refresh of <TrackTemplateResultInfo {Template<template=({% set voltage = states('sensor.esphome_web_a02894_fuel_tank_voltage')|float(0) %} {% set min_voltage = 0.74 %} {% set max_voltage = 2.25 %} {% set percentage = ((max_voltage - voltage) / (max_voltage - min_voltage)) * 100 %} {{ [0, percentage, 100]|sort|join(' ') }}) renders=6>: <RenderInfo Template<template=({% set voltage = states('sensor.esphome_web_a02894_fuel_tank_voltage')|float(0) %} {% set min_voltage = 0.74 %} {% set max_voltage = 2.25 %} {% set percentage = ((max_voltage - voltage) / (max_voltage - min_voltage)) * 100 %} {{ [0, percentage, 100]|sort|join(' ') }}) renders=6> all_states=False all_states_lifecycle=False domains=frozenset() domains_lifecycle=frozenset() entities=frozenset({'sensor.esphome_web_a02894_fuel_tank_voltage'}) rate_limit=None has_time=False exception=None is_static=False>}>>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 662, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: '0 100.0 100'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 352, in _async_dispatch_entity_id_event
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 938, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1294, in _refresh
    self.hass.async_run_hass_job(self._job, event, updates)
  File "/usr/src/homeassistant/homeassistant/core.py", line 938, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 435, in _handle_results
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 664, in state
    raise ValueError(
ValueError: Sensor sensor.fuel_percentage has device class 'None', state class 'None' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '0 100.0 100' (<class 'str'>)

The problem is in your error message. “0 100.0 100” is a string of three numbers.

Look at dev tools ->states and see what

sensor.esphome_web_a02894_fuel_tank_sensor

is returning.

I believe the issue is the last template line.
What is it supposed to do?
Why is it not just outputting the percentage?
0 100.0 100 is not a percentage and that makes the template fail I believe

In your esp device sensor config:

sensor:
  - platform: adc
    pin: GPIOXX
    name: "Fuel Tank Sensor"
    unit_of_measurement: "%"
    filters:
      - calibrate_linear:
         method: least_squares
         datapoints:
          - 0.74 -> 100
          - 2.25 -> 0

You are correct. Remove the last line. (It was an old example I was looking at).

This is an even better solution. The filter does all the work for you.