Sending a configurable amount of predefined pulses

I have an ESP32 controlling another system by sending 100 ms pulses. If the pulse is close to 100 ms, the system counts it. By sending a pulse, waiting more 100 ms and sending another I can make it count 2, and so on.

It was a very simple task to do with arduino, but I am struggling with esphome and home assistant. I have tried some things until the moment, the following is my last try… didn’t work well, just keeps toggling the output.

  - platform: template
    id: coins
    name: "Insert coins"
    optimistic: true
    min_value: 0
    max_value: 10
    step: 1
        - repeat:
            count: !lambda "return (int)x;"
            # count: !lambda "return (int)id(coins).state;"
            # count: 2
              - switch.turn_on: led
              - switch.turn_on: pulse
              - delay: 80ms
              - switch.turn_off: led
              - switch.turn_off: pulse
              - delay: 80ms

I have tried using lambda, but the delay inside it makes the core reboot. If a count and repeat approach cannot be done, my last resort will be to create some buttons and use the on_press with pre-defined pulses.

My recent experience with ESPHome and WiFi may be useful. On an ESP8266, the WiFi component consumes a large portion of the processor, and is EXTREMELY time sensitive. I have a few small loops in an update() routine and it causes the WiFi to lose connection and stack-dump when trying to re-establish the connection.

If you are using an ESP32, it should be much less sensitive to processor constraints and timing. Which model of ESP32 chip are you using?

I am using ESP32-WROOM-32. I could check that it is not good to insert delays inside lambda, even a few ms are enough to trigger a reset.

The “repeat” with the “number” component seems to be a good solution. When I uncomment “count: 2” everything goes alright, the problem is when I try to get the value from the home assistant component and pass to count. As the “count” cannot receive the value direct from “coins.state” I tried using lambda, but then the switch just keeps triggering forever.