Passive buzzer 2 pin

Trying to do when PIR sensor detect movement buzzer start.
I have already made a PIR sensor on one esp8266 and it works properly https://esphome.io/cookbook/pir.html
On second Wemos d1 mini using a 2 pin passive buzzer connected between D1 and GND. Code below Im new in this…
How to get entity so I can make automation rule (start buzzer on PIR movement)?

esphome:
  name: node_buzzer_001
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "****"
  password: "****"
 
    

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Node Buzzer 001 Fallback Hotspot"
    password: "********"
    
captive_portal:

# Enable logging
logger:

ota:

output:
  - platform: esp8266_pwm
    pin: D1
    id: buzzer


# Enable Home Assistant API
api:
  services:
    - service: pir_sensor
      then:
        - output.esp8266_pwm.set_frequency:
            id: buzzer
            frequency: 800Hz
        - output.set_level:
            id: buzzer
            level: 50%
        - delay: 120ms
        - output.turn_off: buzzer
        - delay: 200ms
        - output.esp8266_pwm.set_frequency:
            id: buzzer
            frequency: 1000Hz
        - output.set_level:
            id: buzzer
            level: 50%
        - delay: 120ms
        - output.turn_off: buzzer

It seems that you have two ESP’s one PIR sensor ESP and onebuzzer ESP.
So there are two possible ways:

  1. If Buzzer and PIR sensor are in the same place, you can connect both hardware parts to the same ESP

  2. If the PIR sensor needs to be in another room than the buzzer, you have two separate ESPs

in case 1, you can do the automation easily in ESP Home locally on the ESP8266. But I would recommend Automating the sequence in Home Assistant. This is a lot more readable and flexible since you can easily use the automation editor.

I have two separate ESPs and needs to be separate. Pir work good and cant get entity or something from Buzzer to attach Action in Automations.

OK, it seems you could enhanve the passive buzzer with this rtttl esphome component to even be able to play monophonic melodies:

So if HA should do the automation the following is necessary:

  • You can create a “virtual” switch in your buzzerESP that can be triggered by HA with this component: https://esphome.io/components/switch/index.html.
  • This component then triggers the melody/tone.
  • make HA do the automation between your PIR Sensor and the “virtual” switch.

If your buzzerESP should do the automation the following is necessary:

Can you help me with some code for create a “virtual” switch?

I am on vacation right now, so I do not have an ESP with me to test. But you can try the following:

output:
  - platform: esp8266_pwm
    pin: D1
    id: buzzer_output

rtttl:
  output: buzzer_output


switch:
  - platform: gpio
    pin: D2 #choose a free pin here, just to make ESPHome happy. we do not need it for anything
    name: "Buzzer"
    on_turn_on:
    - rtttl.play: 'MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'
    on_turn_off:
    - rtttl.stop
3 Likes

Works Thanks!

Another thing bothers me. How to make a melody spin in a circle, replay while is switch on.
I do this but not work…

output:
  - platform: esp8266_pwm
    pin: D1
    id: passive_buzzer
    
rtttl:
  output: passive_buzzer

switch:
  - platform: gpio
    pin: D2 #choose a free pin here, just to make ESPHome happy. we do not need it for anything
    id: buzz_pin

  - platform: template
    name: "Buzzer"
    icon: "mdi:bullhorn-outline"
    turn_on_action:
    - while:
          condition:
            switch.is_on: buzz_pin
          then:
            - rtttl.play: 'siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e'
    turn_off_action:
    - rtttl.stop
    turn_on_action:
    - while:
          condition:
            switch.is_on: buzz_pin
          then:
            - rtttl.play: 'siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e,d,e'
            - delay: 5s # add delay here, the old rtttl.play should end before the new starts
    turn_off_action:
    - rtttl.stop

I can only image that adding a delay should do the trick. I would assume that when you use the “while”, it will start the rtttl.play quicker than it takes to play the tune. therefore it does not work. Adding a delay might help…

Hey @CeeCee, can you assist?, I can’t seem to get this to work with the following config on a D1 Mini and this Passive Electronic Buzzer.

output:
  - platform: esp8266_pwm
    pin: D6
    id: buzzer

rtttl:
  output: buzzer
sensor:
  - platform: dallas
    address: 0x05012064058f6328
    name: "Probe 01"
    id: probe01
    filters:
      - lambda: return x * (9.0/5.0) + 32.0;
    unit_of_measurement: "°F"
    on_value_range:
      above: 81
      below: 79
      then:
        - rtttl.play: 'MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'
        - delay: 5s
        - rtttl.stop

I think you have to redefine your range. How can the value at the same time above 81 and below 79?? That’s really a Mission Impossible :slight_smile:

2 Likes