Turn on a switch by a binary_sensor on the ESP device

Hi
I would trigger the buzzer connected at GPIO17 when i detect water in my basement. Now i´m sending the sate to HA and trig the buzzer by automation , but it would be better for me to have this internal in the ESP device, then the alarm will go off even if the HA server is down of some reason.

If possible i would like the buzzer not just turn on , but shifting from on to off in a cycle to give a pulsing sound,

  - platform: adc
    pin: GPIO32
    name: "hp_room_water_level"
    id: "hp_room_water_level"
    attenuation: 11db #adjust the input voltage range from 1 volt to 3 volt
    update_interval: 10s

# Example configuration entry
binary_sensor:
  - platform: template
    name: "basement_flooded"
    lambda: |-
      if (id(hp_room_water_level).state > 2.00) {
        // basement is ok.
        return false;
        #set GPIO17 low
      } else {
        // basement is flooded.
        return true;
        #set GPIO17 high
      }

switch:
  - platform: gpio
    pin: GPIO17
    name: "buzzer_one"
    id: "buzzer_one"

sensor used to detect water:

Something like (untested):

binary_sensor:
  - platform: template
    name: "basement_flooded"
    lambda: |-
      if (id(hp_room_water_level).state > 2.00) {
        // basement is ok.
        return false;
        #set GPIO17 low
      } else {
        // basement is flooded.
        return true;
        #set GPIO17 high
      }
    on_press:
      then:
        - output.turn_on: buzzer_one
    on_release:
      then:
        - output.turn_off: buzzer_one

switch:
  - platform: slow_pwm
    pin: GPIO17
    name: "buzzer_one"
    id: "buzzer_one"
    period: 1s
1 Like

Hi and thanks for your input.
small modification’s switch: has been changed to output: to be able handle the slow_pwm.

Now the busser are trigged by the internal code on the ESP, that is exact what i was looking for.

also the -output.set_level was added to get the pulsing sound , other vise the signal was on the whole time.

    - platform: adc
    pin: GPIO32
    name: "hp_room_water_level"
    id: "hp_room_water_level"
    attenuation: 11db
    update_interval: 10s

binary_sensor:
    - platform: template
      name: "basement_flooded"
      lambda: |-
        if (id(hp_room_water_level).state > 2.00) {
          // basement is ok.
          return false;
        } else {
          // basement is flooded.
          return true;
        }
      on_press:
        then:
          - output.turn_on: buzzer_one
          - output.set_level:
             id: buzzer_one
             level: 50%
      on_release:
        then:
          - output.turn_off: buzzer_one

output:
  - platform: slow_pwm
    pin: GPIO17
    id: "buzzer_one"
    period: 1s