ESPhome templating

I made a sensor about a year ago and cant remember how i made it and need to change it a little.
what i want is to have a template sensor either in esphome or HA that gives me
oil level percentage with 0 decimal places at the minute i have is a bit messy, 3 sensors doing the job that i think a clever person an do in 1.
currently. in esphome

sensor:
  - platform: ultrasonic
    trigger_pin: D6
    echo_pin: D5
    name: "Ultrasonic Sensor"
    update_interval: 10s
  - platform: ultrasonic
    trigger_pin: GPIO12
    echo_pin: GPIO14
    name: "Oil Tank Level Percent"
    update_interval: 10s
    icon: mdi:oil
    filters:
      - offset: -0.15
      - lambda: return (1.0 - (x/1.0)) * 100.0;
    unit_of_measurement: "%"

esphome defaults to 2 places so i have a working template sensor in HA but its a sensor too many

- platform: template
  sensors:
    Oil Tank Percent:
      entity_id: sensor.oil_tank_level_percent
      value_template: {{ states('sensor.oil_tank_level_percent') | float | round(0) }}%

The only thing I can think of is to create a text sensor in ESP-Home out of the distance sensor. That way HA (maybe) keeps it as one decimal

The ESPHome sensor platform supports a precision option called accuracy_decimals:

  - platform: ultrasonic
    trigger_pin: GPIO12
    echo_pin: GPIO14
    name: "Oil Tank Level Percent"
    update_interval: 10s
    icon: mdi:oil
    filters:
      - offset: -0.15
      - lambda: return (1.0 - (x/1.0)) * 100.0;
    unit_of_measurement: "%"
    accuracy_decimals: 0

See the base sensor options (for all sensors) here: https://esphome.io/components/sensor/index.html#base-sensor-configuration

1 Like