How to write basic logic with/in ESPHome?

Hello, I’m pretty new to Home-Assistant and even more so with ESPHome. I understand the basics, but I don’t know how to write logic. Could someone help me to translate the following code into esphome-yaml? (My code is in Python)

d = true
a = actual_stat_of_level
if d:
  while a < 80:
    a = a + 5
    d = false
if !d:
  while a > 10
    a = a - 5
    d = true

I am aware that the code is taken out of context. However, I didn’t want to run the whole project here, I just wanted to address my specific problem.

I will also present my project here as soon as I have solved it.

lambda (I have to add more characters.)