How to share hardwired signals between two ESPHome modules?

I think I´m on something here that can have the potential to work.
This is what I have done:

  • The “Custom UART Text Sensor” example is taken stight off from Lambda Magic
  • I replaced the “Custom UART Switch” with a own variant of a binary sensor, using the “delayed_off” for the heartbeat.function.

The function is:

  • If the right string is recievd by the UART the output will turn on for 5s and then turn off
  • If the right string is recievd again within 5s the output will remain on
  • The flag “b_prohibit” allows the output to only be set once and clears the “uart_readline” textsensor, forcing a new string to be recieved to keep the output enabled.
  • If the string is sent once per second it can lose up to 4 of 5 strings and still work, and will stop within 5s if the string is stuck or lost.
  • The if statement “if (id(uart_readline).state == “OPEN_VALVE_1”)” can be extended with an or statement ( || ) to allow different triggers if needed.
  • I’m using "uart.write: “Water ON” and “OFF” for testing so far instead of digital outputs.

I have just tested this a little on the bench, but so far it looks good!

binary_sensor:
  - platform: template
    id: water_valve1
    name: "Water Valve 1"
    lambda: |-
      static bool b_prohibit = false;
      if (id(uart_readline).state == "OPEN_VALVE_1") {
        if (b_prohibit == false) {
          id(uart_readline).publish_state("");
          b_prohibit = true;
          return true;
        } else {
          return false;
        }
      } else {
        b_prohibit = false;
        return false;
      }
    filters:
      - delayed_off: 5s
    on_press:
      then:
        - uart.write: "Water ON\n"
    on_release:
      then:
        - uart.write: "Water OFF\n"