How to pass a variable from Home Assistant front end to ESPHome?

It’s amazing how posting the problem changes my thinking and solves the problem. Here’s the solution below for anyone else with this issue.

In HA, define your input numbers:



koi_water_change:
  name: Water change litres
  icon: mdi:water
  initial: 1000
  min: 0
  max: 1200
  step: 100

drum_wash_timer:
  name: Drum wash time
  icon: mdi:timer
  initial: 5
  min: 0
  max: 20
  step: 1

spindrifter_off_timer:
  name: Spindrifter off timer
  icon: mdi:timer
  initial: 5
  min: 0
  max: 20
  step: 1

Next, define sensor template using the input_numbers:

- platform: template
  sensors:
    koi_water_change:
      value_template: '{{ states.input_number.koi_water_change.state | int }}'
    drum_wash_timer:
      value_template: '{{ states.input_number.drum_wash_timer.state | int }}'
    spindrifter_off_timer:
      value_template: '{{ states.input_number.spindrifter_off_timer.state | int }}'

Lastly, in ESPHome, define the homeassistant sensors:

  - platform: homeassistant
    name: "Water change litres"
    entity_id: sensor.koi_water_change
  
  - platform: homeassistant
    name: "Drum wash timer"
    entity_id: sensor.drum_wash_timer
  
  - platform: homeassistant
    name: "Spindrifter off timer"
    entity_id: sensor.spindrifter_off_timer

Then use the sensor in your ESPHome automation:

        - lambda: |-        
            delay(id("Spindrifter off timer")*60);
6 Likes