ESPHome SOS Morse Code Light Effect

I’m using ESPHome with Home Assistant for a bunch of home automation projects. I want to be able to flash some of the lights in a Morse code “SOS” pattern as an annoying alert that something has gone terribly wrong. For example, if it looks like there’s an unauthorized home entry while we’re out of town, the neighbors are likely to see the flashing lights. Here’s the lambda effect that makes that happen. (I found the Morse code timing information here: https://morsecode.world/international/timing.html)

      - lambda:
          name: SOS
          update_interval: 100ms # unit length -- this feels reasonable
          lambda: |-
            // https://morsecode.world/international/timing.html
            // Dit: 1 unit
            // Dah: 3 units
            // Intra-character space (the gap between dits and dahs within a character): 1 unit
            // Inter-character space (the gap between the characters of a word): 3 units
            // Word space (the gap between two words): 7 units
            static int idx = 0;
            static const char *pattern = "X X X   XXX XXX XXX   X X X       ";  // see cmnt above

            auto light = id(my_light).turn_on();
            light.set_transition_length(0); // in ms -- no transitions for morse code

            if (pattern[idx] == 'X') {
              light.set_brightness(1.0); // turn it on 100%
            } else {
              light.set_brightness(0.01); // effectively turn it off (but avoid all the on/off events) 
            }
            light.perform();
            idx = (idx + 1) % 34;  // 34 is the length of the pattern 

Leave a Reply

Your email address will not be published. Required fields are marked *