Changing an ESPhome variable via HA

Hello everyone!

I usually write code for ESPhome devices to work on their own even if connection to the server is lost, but I’m having some problems trying to configure a timer and change it’s time via Home Assistant.

Talking about this part of code:

script:
  - id: timer_relay_1
    mode: restart
    then:
      - switch.turn_on: relay1
      - delay: 180s
      - switch.turn_off: relay1

I’d like to change that “delay” preset via home assistant, and I’d like the ESP32’ll remember that setting in case, i.e. of a power loss with the server not recovering.

How can I acheive this? Searched on the forums but found nothing other setting up an helper and a global variable, while I’d like to see that setting in the ESP32 page and not on a different entity

Many thanks

Davide

I think you can use number template along with a lambda delay (convert to ms if needed in lambda)

If you want to be able to adjust a number while the esp is down then you probably need to use the HA helper approach.

Correct. The number can be defined in ESPHome and then you configure it via HA or the ESPHome Web interface for your device, or it can be an entity imported from HA into ESPHome. You can have it both ways but, if you want it independent from HA being around, you want to choose the first approach not the second.

number:
  - platform: template
    id: relay_delay_time
    name: "Relay Delay Duration"
    min_value: 10
    max_value: 600
    initial_value: 180
    step: 1
    unit_of_measurement: "s"
    optimistic: true

script:
  - id: timer_relay_1
    mode: restart
    then:
      - switch.turn_on: relay1
      - delay: !lambda "return id(relay_delay_time).state * 1000;" # Переводимо секунди в мілісекунди
      - switch.turn_off: relay1
1 Like
number:
  - platform: template
    ...
    restore_value: true

Thank you so much to everyone,

Template: numer is what I was missing, everything is working now!