Help with cm to % on D1 mini and HC-SR04

having errors displaying my tank level to my ssd1306 display. I am very new to coding and ive been researching this topic. here is what i have:

HC-SR04 sensor

sensor:

  • platform: ultrasonic
    trigger_pin: D6
    echo_pin: D7
    name: “Level Sensor”
    id: level
    update_interval: 250ms

  • platform: template
    sensors:
    level_sensor:
    entity_id: sensor.level_sensor
    value_template: ‘{{100-((states(“sensor.level_sensor”) | int) / 11 * 100) | int }}’
    unit_of_measurement: ‘% Full’
    friendly_name: Tank Level
    icon_template: ‘mdi:ruler’

font:

  • file: “font.ttf”
    id: font1
    size: 15

  • file: “font.ttf”
    id: font2
    size: 28

##SSD1306 display
i2c:
sda: D1
scl: D2

display:

  • platform: ssd1306_i2c
    model: “SSD1306 128x64”
    reset_pin: D0
    address: 0x3C
    lambda: |-
    it.print(25, 0, id(font1), “Water Level”);
    if (id(level).has_state()) {
    it.printf(100, 20, id(font2), TextAlign::TOP_RIGHT , id(level).state);
    }

Please read How to help us help you - or How to ask a good question. Although you should read the whole thing, point 11 tells you how t format your code properly.

Hi
Is it that you want to know how the printf works.

You need to tell printf what to print out. You need a string with an marker that tells you where to write the id(level).state.

it.printf(100, 20, id(font2), TextAlign::TOP_RIGHT , "%.1f" ,id(level).state);
This is how I used it for a project. 
it.printf(100, 20, id(font2), "%.1f" ,id(level).state);

This should print out the value with one decimal. I you want more change it to 2 for two decimals or 0 for no decimals.

It described Display Component — ESPHome
under Formatted Text.

I guess that this one can also give you some pointers.
https://www.cplusplus.com/reference/cstdio/printf/

I hope it help you
/Mattias

@birchman you are perfectly correct, however I don’t think level is actually what he wants to print. He surely wants to print the calculated value from the template sensor.

However that sensor seems to be wrongly configured. Pretty hard to see the code when the advice to format it properly isn’t taken, but entity_id: does not seem to be an option for a template sensor in esphome. In fact it looks more like a home assistant template sensor than an esphome template sensor.