Lambda for delay switch

Hi,
I have no programming background and I am really struggling with esphome lambdas.
What I want to do:
This is part of my irrigation project - in HA I want to be able to define for how long drip irrigation should be on for - for that I want to use input number helper, and use that in esphome as a switch delay value

This is what I have so far:

sensor:
  - platform: homeassistant
    name: Irrigation Duration
    entity_id: input_number.esphome_irrigation_delay_duration
    id: ha_irrigation_duration
  - platform: template
    name: Delay Minutes
    id: delay_minutes
    lambda: |-
      if (id(ha_irrigation_duration).state == 15.0) {
        return 15.0 * 60;
      } else {
        return id(ha_irrigation_duration).state * 60;
      }


switch:
  - platform: gpio
    name: "FlowerBed"
    id: relay1
    pin: 19
    on_turn_on:
      then:
        - lambda: |-
            // Turn off the relay after the delay
            id(relay1).turn_off();
            // Delay the turn off of the relay
            delay(id(delay_minutes).state);

this compiles fine but it seems to crash my nodemcu32 device: here is the log file:

[10:20:35][C][homeassistant.sensor:030]: Homeassistant Sensor 'Irrigation Duration'
[10:20:35][C][homeassistant.sensor:030]:   State Class: ''
[10:20:35][C][homeassistant.sensor:030]:   Unit of Measurement: ''
[10:20:35][C][homeassistant.sensor:030]:   Accuracy Decimals: 1
[10:20:35][C][homeassistant.sensor:031]:   Entity ID: 'input_number.esphome_irrigation_delay_duration'
[10:20:56][D][homeassistant.sensor:024]: 'input_number.esphome_irrigation_delay_duration': Got state 2.00
[10:20:56][D][sensor:127]: 'Irrigation Duration': Sending state 2.00000  with 1 decimals of accuracy
[10:21:05][D][switch:013]: 'FlowerBed' Turning ON.
[10:21:05][D][switch:056]: 'FlowerBed': Sending state ON
[10:21:05][D][switch:017]: 'FlowerBed' Turning OFF.
[10:21:05][D][switch:056]: 'FlowerBed': Sending state OFF
WARNING 192.168.1.7: Connection error occurred: [Errno 104] Connection reset by peer
INFO Processing unexpected disconnect from ESPHome API for 192.168.1.7
WARNING Disconnected from API
INFO Successfully connected to 192.168.1.7

The device starts OK, when I change the value of the input number in HA that is being picked up by the device, when I turn the switch On in HA I can hear the relay click and 1-2 seconds later the switch becomes greyed out in HA and the device reboots.
Any ideas what is going on here and how to fix this?
Thanks

Not directly, but a few of things:

  • lambda delays are in milliseconds, and I’ve not seen the syntax you’re using before;
  • why do you think the turn_off will happen after the delay when you’ve written it before?
  • why the if statement in the Delay Minutes template?
  • did you ask ChatGPT to generate this code?

Perhaps try this, assuming your oddly-named delay_minutes template is actually a value in seconds:

sensor:
  - platform: homeassistant
    name: Irrigation Duration
    entity_id: input_number.esphome_irrigation_delay_duration
    id: ha_irrigation_duration
  - platform: template
    name: Delay Minutes
    id: delay_minutes
    lambda: return (id(ha_irrigation_duration).state) * 60;
 
switch:
  - platform: gpio
    name: "FlowerBed"
    id: relay1
    pin: 19
    on_turn_on:
      then:
        - delay: !lambda "return (id(delay_minutes).state) * 1000;"
        - switch.turn_off: relay1

Thank you, that is working exactly as I want it.

1 Like

is there a way in esphome to create a sensor that would show the time that is left on that timer when the relay is switched on?

Probably, but I’d do it in HA myself. This (untested) should evaluate when the switch changes state and at the top of each minute:

template:
  - sensor:
      - name: "Irrigation minutes remaining"
        icon: mdi:timeline-clock-outline
        unique_id: b25c9ee4-42fc-4c77-ae09-7247db316dfb
        unit_of_measurement: 'min'
        state: >
          {% if is_state('switch.flowerbed', 'off') %}
            0
          {% else %}
            {% set elapsed = (now()|as_timestamp -
                              states['switch.flowerbed'].last_changed|as_timestamp(0)) / 60 %}
            {% set total = states('input_number.esphome_irrigation_delay_duration')|int(0) %}
            {{ (total - elapsed)|round(0) }}
          {% endif %}