Help turning template sensor into lambda

In an effort to improve the mold indicator integration which I use to turn on my dehumidifier when there is a chance of window condensation forming I have implemented the integration as a template sensor that uses an actual surface temperature from an ESP32 rather than a calibration factor and outside temperature:

template:
  - sensor:
      - name: Lounge Condensation Chance
        unit_of_measurement: "%"
        icon: >
          {% set chance = states('sensor.lounge_condensation_chance')|float(0) %}
          {% if chance == 0 %}
            mdi:water-alert-outline
          {% elif chance > 70 %}
            mdi:water-percent-alert
          {% else %}
            mdi:water-check-outline
          {% endif %}
        state_class: measurement
        state: >
          {# ### CHANGE THESE THREE SENSORS ### #}
          {% set room_temp = states('sensor.lounge_room_temperature')|float(0) %}
          {% set room_humid = states('sensor.lounge_room_humidity')|float(0) %}
          {% set crit_temp = states('sensor.lounge_window_temperature')|float(0) %}

          {% set MK2 = 17.62 %}
          {% set MK3 = 243.12 %}
          {% set alpha = MK2 * room_temp / ( MK3 + room_temp ) %}
          {% set beta = MK2 * MK3 / ( MK3 + room_temp ) %}
          {% set dewpt = MK3 * ( alpha + log( room_humid / 100 ) ) / ( beta - log( room_humid / 100 ) ) %}
          {% set alpha_crit = MK2 * crit_temp / ( MK3 + crit_temp ) %}
          {% set beta_crit = MK2 * MK3 / ( MK3 + crit_temp) %}
          {% set crit_humidity = ( e ** ( ( dewpt * beta_crit - MK3 * alpha_crit) / ( dewpt + MK3) ) * 100 )|round(0)|int %}
          {{ ([0,crit_humidity,100]|sort)[1] }}
        availability: >
          {# ### CHANGE THESE THREE SENSORS ### #}
          {{  has_value('sensor.lounge_room_temperature') and
              has_value('sensor.lounge_room_humidity') and
              has_value('sensor.lounge_window_temperature') }}

This works very well but it seems a shame to have an ESP32 doing nothing but measuring the temperature of the window. So I thought I could import the lounge temp and humidity into the ESP and calculate the condensation chance in the ESP.

Does anyone know how to convert that template sensor into an ESPHome template sensor?

I got nowhere with the lambda.

ESP config:

esphome:
  name: lounge-window-ds1820b
  platform: ESP32
  board: mhetesp32minikit

wifi:
  ssid: 'WAPLO'
  password: !secret wifi_pwd
  manual_ip:
    static_ip: 10.1.1.67
    gateway: 10.1.1.1
    subnet: 255.255.255.0

api:
  encryption:
    key: !secret api_encryption_key

logger:

ota:
  password: !secret esp_pwd

dallas:
  - pin: GPIO16
    update_interval: 3s

binary_sensor:
  - platform: status
    name: "Lounge Window Status"

sensor:
  - platform: wifi_signal
    name: "Lounge Window WiFi Signal"
    update_interval: 4s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 1

  - platform: dallas
    address: 0x793351d4437dea28
    name: "Lounge Window Temperature"
    id: window_temperature
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
          send_first_at: 1

  - platform: homeassistant
    id: lounge_temperature
    entity_id: sensor.lounge_room_temperature

  - platform: homeassistant
    id: lounge_humidity
    entity_id: sensor.lounge_room_humidity

  - platform: template
    name: "Lounge Room Condensation Chance"
    unit_of_measurement: "%"
    lambda: |-  # Help!

During development (and possibly permanently), I’d probably break it up step by step and have sensors for each parameter (alpha, beta etc). Or at least add them in one by one.

Then when I’ve got it working I’d either mark most of them as internal or have a go at merging them into a single sensor (Which I’d probably ask my friendly neighbourhood AI for some help with).

I’ll see if I can dig out some similar examples to help get started.

Edit: This isn’t particularly close to what you need (some string stuff in there) but may help with the general structure to get started…

  - platform: template
    name: "Movement Command ESP Composite Command"
    id: esp_motor_movement_uart_command
    icon: mdi:format-text-rotation-none
    update_interval: 60s    
    lambda: |-
      int int_m_1m_rotate_base = round(id(m_1m_rotate_base).state *-1);
      std::string str_m_1m_rotate_base = ("1m" + String(int_m_1m_rotate_base) + "\\r").c_str();
      
      int int_m_2m_tilt_shoulder = round(id(m_2m_tilt_shoulder).state);
      std::string str_m_2m_tilt_shoulder = ("2m" + String(int_m_2m_tilt_shoulder) + "\\r").c_str();
      
      int int_m_3m_tilt_elbow = round(id(m_3m_tilt_elbow).state);
      std::string str_m_3m_tilt_elbow = ("3m" + String(int_m_3m_tilt_elbow) + "\\r").c_str();
      
      int int_m_4m_wrist1 = round(id(m_4m_wrist1).state);
      std::string str_m_4m_wrist1 = ("4m" + String(int_m_4m_wrist1) + "\\r").c_str();
      
      int int_m_5m_wrist2= round(id(m_5m_wrist2).state);
      std::string str_m_5m_wrist2 = ("5m" + String(int_m_5m_wrist2) + "\\r").c_str();
      
      int int_m_8m_pincher = round(id(m_8m_pincher).state);
      std::string str_m_8m_pincher = ("8m" + String(int_m_8m_pincher) + "\\r").c_str();
      
      return {(
        str_m_1m_rotate_base 
        + str_m_2m_tilt_shoulder 
        + str_m_3m_tilt_elbow
        + str_m_4m_wrist1 
        + str_m_5m_wrist2
        + str_m_8m_pincher
              )
        };

Edit 2: If the above isn’t so helpful and you’re still stuck, let me know and I’ll have a go at something more specific (especially since you’ve helped me about a billion times;)