Use Home Assistant number as delay in ESPHome

First of all, you should always try to avoid mixing sensor entities between HA and Esphome. There’s no reason to create an input_number sensor in HA and then import that sensor into Esphome when Esphome is perfectly capable of creating that sensor and then you keep everything on the esp board so that it’s still functional with and without HA if ever you have a problem with HA. Doing it this way, you will have the backup web_server that you can open up and set or change your configuration choices whereas if your number entity was made in HA, then you wouldn’t be able to configure how many minutes to set your runtime too. I would use this configuration and then just delete the Number entity you made in HA and use this one provided by Esphome in its place.

You should set it up like this.

number:
  - platform: template
    id: max_run_time_minutes
    name: "Max Run Time - All Zones"
    icon: mdi:av-timer
    unit_of_measurement: "Min."
    optimistic: true
    step: 1
    mode: box
    min_value: 1
    max_value: 60
    initial_value: 1
    on_value:
       - sensor.template.publish:
          id: runtime
          state: !lambda 'return x;'

      - globals.set:
          id: max_time2run
          value: !lambda 'return id(max_run_time_minutes).state * 60000;

    

sensor:
  - platform: template
    id:  runtime
    name: "Current Runtime Min."

FYI part of your original issue was that you were trying to set a number and then you were using a text_sensor to display it. A text_sensor expects and requires the value to be a text/string and your Number entity outputs a number/float. You can’t mix match different entity types like string-float, string-int, int-float, etc etc. You need to convert one of them so that it is the same type as the other so, your text input you would have needed to convert that value into a float from it’s original type, string. It’s easier and just less unnecessary steps if you don’t use text sensor and instead just use template sensor.

1 Like

Thanks for the advice, very much appreciated.

No thank you’s required, thats what the bill coming in the mail is for. ; )

Did that end up working out how you wanted, i assume?

This may help you more if it wasn’t completely clear about creating entities in Esphome rather than in HA and importing them into Esphome. Now that isnt always possible to do but, for the most part it is and me personally, i would suggest doing it the way i explained so you have redundancy which is equivalent to added reliability.

This is for my kepad for the garage door. Because i created all the schedule configs and adding new users/pin codes etc in Esphome, they aren’t only just added to HA to be used there but, in the event of a “bad day” and HA calls in sick. I wont be left with 0 options or ways to access the keypad and for example disable a lost/stolen RFID tag so that it can’t be used by someone unauthorized. I can simply access it through a browser and do what i need. Had i created those functions through helpers in HA then none of it would be available without HA and i believe they call that, “Bent over a barrel”

1 Like

Thanks, again. I think this is what I want to do as I want to set a default run time that resides on the device itself. I am still trying to get it the code to validate though.

Seems an issue with the on_value block. Not sure if it is an indication problem.

Here’s the error:


INFO ESPHome 2025.4.0
INFO Reading configuration /config/esphome/switch_back_4ch.yaml...
ERROR Error while reading config: Invalid YAML syntax:

while scanning a quoted scalar
  in "/config/esphome/switch_back_4ch.yaml", line 269, column 26
found unexpected end of stream
  in "/config/esphome/switch_back_4ch.yaml", line 269, column 78

This is the code block that seems to cause an issue.

    on_value:
       - sensor.template.publish:
          id: runtime
          state: !lambda 'return x;'
          
       - globals.set:
          id: max_time2run
          value: !lambda 'return id(max_run_time_minutes).state * 60000;'

I noticed there was no ' at the end of the last line value: !lambda 'return id(max_run_time_minutes).state * 60000;

Now I am getting:

number.template: [source /config/esphome/switch_back_4ch.yaml:251]
  platform: template
  id: max_run_time_minutes
  name: Max Run Time - All Zones
  optimistic: True
  min_value: 0
  max_value: 5
  initial_value: 3
  step: 1
  unit_of_measurement: min
  icon: mdi:av-timer
  mode: box
  on_value: 
    - 
      Unable to find action with the name 'sensor.template.publish'.
      sensor.template.publish: 
        id: runtime
        state: !lambda |-

There appears to be an issue with the sensor.template.publish: block. Commenting that out and it validates. Not sure what the issue is, seems to be consistent with the esp home documentation.

Did you add this template sensor to the yaml?

1 Like

No. No I did not. :person_facepalming: Now it’s validating.

Just uploaded the code, it appears to be working just fine. Been trying to sort this out for ages thanks for the advice and for your patience. Greatly appreciated.

1 Like

Not to keep beating this drum if you already understand this but, i kind of get the impression that you might still be a little uclear of the differences and thats no big deal, its a lot to take in and fully comprehend or at least it was for me initially.

Whether you create the number entity via a helper in HA or you use the Number entity that you create in Esphome that is then added to HA, both ways will set the time on the esp device itself. Where they differ and that wont work is if your wifi goes out for example and HA cant communicate with the esp device to update it or tell it that time number you specified. If you created it your Number entity in Esphome and set the time from your HA Number entity and then your wifi goes out like the last example, the Time number will not be effected and will be whatever you set it to in HA, even if it cant communicate back with HA it doesnt matter. The other benefit is if while your wifi was down and you needed to change that Time number, you can do it without needing to use HA only because that Number entity was created in Esphome and its all together and stored on that esp that you can then access without having to go through HA.

Sorry to keep repeating it, its just important to understand the differences so that when you create something, then it works exactly as you intended to work when those pesky scenarios happen or your router dies and only then do you realize that you have a problem and sometimes you can have a big problem(trust me on this one…) I am only trying to help you and orhers get the benefit of not having to repeat and learn from mistakes I’ve made and learned the hard way.

The best advice I can give you is if your unsure about something or your sitting there asking yourself questions is to not be afraid to flash test configurations on an esp board and check things out for yourself so that your certain about the questions you think you are right about. A good example and one i have to do from time to time is when im tweaking some sensors filters and IDK if you’ve looked through them all but, there are many of them that by just reading the description of the filters, they seem like they’re very similar or appear to do the same things and can be confusing and annoying sometimes.

For me, I find that the quickest way to fully understand the each of them and their minor but important differences is to just try multiple filters or even combinations of filters untill i get what im looking for.

1 Like

Thanks, that all makes sense and reflects my initial intention, though I think it fair to say your method is works much better. The way I initially set it up was to have a ‘hard coded’ number that the device would set upon boot up and that could be updated if required via the home assistant input number entity. The intent being to ensure the number was set on the device itself rather than relying on HA to set it. Still the method you illustrated is by far the best way to do it. Thanks for taking the time to explain that to me.

Yep, i agree its far better. No problem, glad i could be of help.