ESPHome - Simple door beep

I have an ESP32 based home alarm system configured and working flawlessly since a few months. I have 6 wired door sensors hooked up to the ESP32. There are two active relay connected buzzers (one in entry door and one in the basement) which beep on a door open/close automation in Home Assistant. The beeps templates are defined in ESPHome and are programmed into the ESP32. However the automation or simple door open/close trigger is in Home Assistant. So both buzzers beep simultaneously on door open/close. I want to move the logic of just the door open/close beep to ESP32. So in case my Home Assistant machine is down for whatever reason, the door beeps at least work. Online I see some scripts based on Arduino but nothing for ESPHome.

Here is part of the relevant config in ESP32. Would appreciate if someone can help me with a simple door open/close code for ESPHome.

Door Sensors

binary_sensor:
#Terminal 1
  - platform: gpio
    pin: 
      number: GPIO32
      mode: INPUT_PULLUP
    name: "Front Door"
    device_class: door
#Terminal 2
  - platform: gpio
    pin: 
      number: GPIO33
      mode: INPUT_PULLUP
    name: "Front Porch Door"
    device_class: door

Switch

switch:
- platform: gpio
  pin: 
    number: GPIO21
  name: "Siren"
  id: siren
  icon: mdi:bullhorn
  inverted: yes
  restore_mode: ALWAYS_OFF

- platform: gpio
  id: "beep1"
  pin:
    number: GPIO19
  inverted: yes
  internal: true
  restore_mode: ALWAYS_OFF

- platform: gpio
  id: "beep2"
  pin:
    number: GPIO18
  inverted: yes
  internal: true
  restore_mode: ALWAYS_OFF

- platform: template 
  name: "Door Open Beep 1"
  icon: "mdi:bell-alert"
  id: open_beep1
  turn_on_action:
    - switch.turn_on: beep1
    - delay: 100ms 
    - switch.turn_off: beep1
    - delay: 100ms
    - switch.turn_on: beep1
    - delay: 100ms
    - switch.turn_off: beep1
    - delay: 100ms
    - switch.turn_on: beep1
    - delay: 100ms
    - switch.turn_off: beep1
    - switch.turn_off: open_beep1

- platform: template
  name: "Door Close Beep 1"
  icon: "mdi:bell-alert"
  id: close_beep1
  turn_on_action:
      - switch.turn_on: beep1
      - delay: 750ms
      - switch.turn_off: beep1
      - switch.turn_off: close_beep1

- platform: template 
  name: "Door Open Beep 2"
  icon: "mdi:bell-alert"
  id: open_beep2
  turn_on_action:
    - switch.turn_on: beep2
    - delay: 100ms 
    - switch.turn_off: beep2
    - delay: 100ms
    - switch.turn_on: beep2
    - delay: 100ms
    - switch.turn_off: beep2
    - delay: 100ms
    - switch.turn_on: beep2
    - delay: 100ms
    - switch.turn_off: beep2
    - switch.turn_off: open_beep2

- platform: template
  name: "Door Close Beep 2"
  icon: "mdi:bell-alert"
  id: close_beep2
  turn_on_action:
      - switch.turn_on: beep2
      - delay: 750ms
      - switch.turn_off: beep2
      - switch.turn_off: close_beep2

So beep1 and beep2 are the two active buzzers and open_beep1/close_beep1 open_beep2/close_beep2 are the beeps template defined for multiple beeps (door open) and single long beep (door close). Both buzzers should beep simultaneously on door open/close.

I would like the automation moved from HA to ESP32 in case my system goes offline. Any help would be highly appreciated.

Bump!

Any help would be highly appreciated.

@banstro

Under binary_sensor you can use the Automations. I would take a look at on-press. Worth reading the following to see how they work.

Binary Sensor Component — ESPHome

1 Like

Have to say, when I read the OP it was a little confusing for me.

So consider trying to simplify your key challenges/needs.

Might just be me. But simplifying/summarising might help attract more help.

@Mahko_Mahko … perhaps it might be quite simple. Just an automation to run when the binary sensors, Front Door and Front Door Porch changes state? I do get lost in very long questions.

1 Like

Thank you. I didn’t see this earlier. All I need is to move HA automation of door open/close beep to Esp32.

Thanks for the link. I got it working for on-press and on-release. I had to add this for each sensor. To simplify the code can I consolidate the on-press and on-release for a defined sensors under a single group?

Maybe this ESPHome code fragment of mine can point you towards your desired result:

switch:

  • platform: gpio
    name: “Garage Door 1 Relay”
    icon: “mdi:electric-switch”
    pin:
    number: GPIO5
    inverted: no
    id: garage_door_1_relay
    internal: yes
    on_turn_on:

    • delay: 500ms
    • switch.turn_off: garage_door_1_relay
  • platform: gpio
    name: “Garage Door 2 Relay”
    icon: “mdi:electric-switch”
    pin:
    number: GPIO4
    inverted: no
    id: garage_door_2_relay
    internal: yes
    on_turn_on:

    • delay: 500ms
    • switch.turn_off: garage_door_2_relay
  • platform: gpio
    name: “Garage Door 3 Relay”
    icon: “mdi:electric-switch”
    pin:
    number: GPIO14
    inverted: no
    id: garage_door_3_relay
    internal: yes
    on_turn_on:

    • delay: 500ms
    • switch.turn_off: garage_door_3_relay
  • platform: template
    name: Garage Door 1
    icon: “mdi:garage-variant”
    turn_on_action:

    • output.esp8266_pwm.set_frequency:
      id: buzzer1
      frequency: 800Hz
    • output.set_level:
      id: buzzer1
      level: 50%
    • output.turn_on: buzzer1
    • delay: 5s
    • output.turn_off: buzzer1
    • switch.turn_on: garage_door_1_relay
    • delay: 500ms
    • switch.turn_off: garage_door_1_relay
  • platform: template
    name: Garage Door 2
    icon: “mdi:garage-variant”
    turn_on_action:

    • output.esp8266_pwm.set_frequency:
      id: buzzer1
      frequency: 800Hz
    • output.set_level:
      id: buzzer1
      level: 50%
    • output.turn_on: buzzer1
    • delay: 5s
    • output.turn_off: buzzer1
    • switch.turn_on: garage_door_2_relay
    • delay: 500ms
    • switch.turn_off: garage_door_2_relay
  • platform: template
    name: Garage Door 3
    icon: “mdi:garage-variant”
    turn_on_action:

    • output.esp8266_pwm.set_frequency:
      id: buzzer1
      frequency: 800Hz
    • output.set_level:
      id: buzzer1
      level: 50%
    • output.turn_on: buzzer1
    • delay: 5s
    • output.turn_off: buzzer1
    • switch.turn_on: garage_door_3_relay
    • delay: 500ms
    • switch.turn_off: garage_door_3_relay

output:

  • platform: esp8266_pwm
    pin:
    number: GPIO12
    inverted: no
    id: buzzer1

  • platform: esp8266_pwm
    pin:
    number: GPIO13
    inverted: no
    id: buzzer2

Thank you! I will update my code accordingly. Right now I am using two 12v active buzzers which is causing my Esp32 to reboot. I have ordered two passive buzzers and expecting delivery any day. Your code will help me a lot for switching from active to passive.

Also, I think your code indentation was changed while posting. Could you please add ``` to the start and end of your code. That will preserve the indentations. Thank you once again!

So here is what I have now. I had to change from esp8266_pwm to ledc as I have ESP32. Also I have defined the frequency aspects of the code to single outputs beep1 and beep2 and then defined what beep1 and beep2 should sound like via template. This still does not resolve the grouping issue. I would like to group the doors together so that I don’t have to add the open/close beeps for each door sensor.

binary_sensor:
#Terminal 1
  - platform: gpio
    pin: 
      number: GPIO32
      mode: INPUT_PULLUP
    name: "Front Door"
    device_class: door
    on_press:
      then:
        - switch.turn_on: open_beep1
        - switch.turn_on: open_beep2
    on_release:
      then:
        - switch.turn_on: close_beep1
        - switch.turn_on: close_beep2

#Terminal 2
  - platform: gpio
    pin: 
      number: GPIO33
      mode: INPUT_PULLUP
    name: "Front Porch Door"
    device_class: door
    on_press:
      then:
        - switch.turn_on: open_beep1
        - switch.turn_on: open_beep2
    on_release:
      then:
        - switch.turn_on: close_beep1
        - switch.turn_on: close_beep2
# Piezo Buzzer 1
output:
  - platform: ledc
    pin:
      number: GPIO19
    id: buzzer1
    inverted: yes

# Piezo Buzzer 2
  - platform: ledc
    pin:
      number: GPIO18
    id: buzzer2
    inverted: yes
switch:
- platform: output 
  id: beep1
  output: buzzer1
  on_turn_on: 
    then:
      - output.turn_on: buzzer1
      - output.ledc.set_frequency:
          id: buzzer1
          frequency: "800Hz"
      - output.set_level:
          id: buzzer1
          level: "50%"
  on_turn_off:
    then:
      - output.turn_off: buzzer1

- platform: output 
  id: beep2
  output: buzzer2
  on_turn_on: 
    then:
      - output.turn_on: buzzer2
      - output.ledc.set_frequency:
          id: buzzer2
          frequency: "800Hz"
      - output.set_level:
          id: buzzer2
          level: "50%"
  on_turn_off:
    then:
      - output.turn_off: buzzer2
- platform: template 
  name: "Door Open Beep 1"
  icon: "mdi:bell-alert"
  id: open_beep1
  turn_on_action:
    - switch.turn_on: beep1
    - delay: 100ms 
    - switch.turn_off: beep1
    - delay: 100ms
    - switch.turn_on: beep1
    - delay: 100ms
    - switch.turn_off: beep1
    - delay: 100ms
    - switch.turn_on: beep1
    - delay: 100ms
    - switch.turn_off: beep1
    - switch.turn_off: open_beep1

- platform: template 
  name: "Door Open Beep 2"
  icon: "mdi:bell-alert"
  id: open_beep2
  turn_on_action:
    - switch.turn_on: beep2
    - delay: 100ms 
    - switch.turn_off: beep2
    - delay: 100ms
    - switch.turn_on: beep2
    - delay: 100ms
    - switch.turn_off: beep2
    - delay: 100ms
    - switch.turn_on: beep2
    - delay: 100ms
    - switch.turn_off: beep2
    - switch.turn_off: open_beep2

- platform: template
  name: "Door Close Beep 1"
  icon: "mdi:bell-alert"
  id: close_beep1
  turn_on_action:
      - switch.turn_on: beep1
      - delay: 750ms
      - switch.turn_off: beep1
      - switch.turn_off: close_beep1

- platform: template
  name: "Door Close Beep 2"
  icon: "mdi:bell-alert"
  id: close_beep2
  turn_on_action:
      - switch.turn_on: beep2
      - delay: 750ms
      - switch.turn_off: beep2
      - switch.turn_off: close_beep2

I just posted an example over here that groups binary sensors which could be of some use.

Thanks will look into this.

Hi! Thank you for your post! I also have an ESP based alarm system, mine is from Konencted and I just re-flashed their boards with ESPHome. That was also my first contact with ESPHome so I am very new to it. I have two questions:

  1. Is a template the only option to get multiple beeps from the buzzer? What I would like to get is 2 beeps every time the switch is turned on. For now I am ok with HA triggering an automation where if a door/window sensor is triggered, a switch controlling the buzzer is turned on (but see question 2). I managed with a single beep easily by defining the switch as a momentary switch but struggle to get the double beep action. Ideally I would like a momentary switch with a repeat action.

  2. Is there a way to have an automation on an ESP to trigger buzzers wired to different ESPs when one of the sensors wired to one of the ESPs gets triggered? I would like to get a beep from all of the buzzers throughout the house, not only from the one that is wired to the ESP to which the sensor that was triggered is connected. That’s why I moved the automation back to HA.