ESPHome + HA integration - how to have initial values in ESPHOME

Hi,
I’m currently working on a small project with an Arduino Nano ESP32 to control a fan based on temperature. The device is also integrated into HA.
I added input numbers in HA to set the temperature thresholds (low and high) and the corresponding sensors in ESPHome.
Everything works fine, except for the initial values.
If the Nano powers up while HA is ON, then the initial values are provided by HA.
But if the Nano powers up while HA is OFF, I would like the Nano to operate with the same thresholds.
So, how can I do that? I searched the forum, but all I got is someone saying that I have to do some black magic with lambda … Not sure what he meant.
Please help,

Here is the ESPHome code:

substitutions:
  minimum_fan_speed: "10" # What is the minimum fan speed, as a percentage

esphome:
  name: instruments
  friendly_name: Instruments

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "zzz"

ota:
  - platform: esphome
    password: "zzz"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Instruments"
    password: "zzz"

captive_portal:

# Add a temperature sensor on pin D2
one_wire:
  - platform: gpio
    pin: GPIO5
sensor:
  - platform: dallas_temp
    name: "Temperature"
    id: temperature
    icon: "mdi:temperature-celsius"
    update_interval: 5s
    on_value:
      then:
        - script.execute: set_fan_state

# Add a temperature threshold low
  - platform: homeassistant
    name: "Temperature Threshold Low"
    entity_id: sensor.instruments_temperature_threshold_low
    id: temperature_threshold_low
    icon: "mdi:temperature-celsius"

# Add a temperature threshold high
  - platform: homeassistant
    name: "Temperature Threshold High"
    entity_id: sensor.instruments_temperature_threshold_high
    id: temperature_threshold_high
    icon: "mdi:temperature-celsius"

# Add a RPM sensor on pin D3
  - platform: pulse_counter
    pin: GPIO6
    name: "Fans RPM"
    id: fans_rpm
    icon: "mdi:rotate-3d-variant"
    unit_of_measurement: 'RPM'
    filters:
      multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

# Add a sensor template to display PWM duty ccycle in HA
  - platform: template
    name: "Fans PWM Duty Cycle"
    id: fans_pwm_duty_cycle
    lambda: |-
      if (id(fans).state == false)
      {
        return 0;
      }
        else
      {
        return {};
      }
    icon: "mdi:rotate-3d-variant"
    unit_of_measurement: "%"
    accuracy_decimals: 0

# Add a PWM signal on pin D10
output:
  - platform: ledc
    pin: GPIO21
    frequency: 25000 Hz
    id: fans_pwm

# Add a fan
fan:
  - platform: speed
    output: fans_pwm
    name: "Fans"
    id: fans
    icon: "mdi:fan"
    on_turn_off:
      - sensor.template.publish:
          id: fans_pwm_duty_cycle
          state: !lambda 'return 0;'
    on_speed_set:
      - sensor.template.publish:
          id: fans_pwm_duty_cycle
          state: !lambda 'return id(fans).speed;'

script:
  - id: set_fan_state
    then:
      - if:
          condition:
            lambda: |-
              return id(temperature).state < id(temperature_threshold_low).state;
          then:
            - fan.turn_off: fans
          else:
            - fan.turn_on:
                id: fans
                speed: !lambda |-
                  if (id(temperature).state >= id(temperature_threshold_high).state) {
                    return 100;
                  }
                  else {
                    float calc_speed = ((100-id(${minimum_fan_speed})) / (id(temperature_threshold_high).state-id(temperature_threshold_low).state))*(id(temperature).state-id(temperature_threshold_low).state)+id(${minimum_fan_speed});
                    return calc_speed;
                  }

Here is the HA configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

input_number:
  instruments_temperature_threshold_low:
    name: "Instruments Temperature Threshold Low"
    initial: 25
    min: 20
    max: 60
    step: 1

  instruments_temperature_threshold_high:
    name: "Instruments Temperature Threshold High"
    initial: 40
    min: 20
    max: 60
    step: 1

sensor:
 - platform: template
   sensors:
     instruments_temperature_threshold_low:
       value_template: '{{ states.input_number.instruments_temperature_threshold_low.state | float }}'
     instruments_temperature_threshold_high:
       value_template: '{{ states.input_number.instruments_temperature_threshold_high.state | float }}'

Define “same”.

By “same”, I meant that I would like the Nano to use it’s own initial values that would be the same as those configured in HA.
The code I came up with is based on my search in the forum. So far, I only found a way to configure initial values in HA.
The idea is that I want the flexibility to modify the thresholds in HA, but ideally I’d like to have the initial vaues set in ESPHome. This way, the device can operate autonomously without HA.
Do I make sense?

I’m not following you completely, but Esphome has for example number component that you can modify in HA but it’s running on esphome and it can retain the value after power outage.

I already use input numbers defined in HA to set the thresholds initial values.
When HA powers up, it uses the thresholds initial values to set the values of the thresholds to be displayed on the HA GUI.
When the Nano powers up, it gets the thresholds values from HA and uses these values to perform the fan speed calculations.
If the Nano remains powered while HA is powered off, the Nano will continue performing its tasks with the last values received from HA.
Now, assuming HA is off when the Nano powers up, what thresholds values will the Nano use, since HA is not there to provide the values?
This is why I would like to configure thresholds initial values in ESPHome.
Did I explained better?

Yes and no.
I don’t know your setup. If you create template number in esphome, it’s running in esphome independently from HA. You can change the value on HA, but if HA is offline it doesn’t rely on that.

So what you’re saying is instead of specifiyng the input numbers in HA (as I did and showed in my configuration.yaml inserted at the beginning of the post), I should specify them in the ESPHome device yaml?
Sorry to ask a basic question, but how do you show these numbers back in HA?

iIt’s easier if you just experiment.
Add this to your esphome code:

number:
  - platform: template
    name: "Template number"
    optimistic: true
    min_value: 0
    max_value: 100
    step: 1