Aktiv biper buzzer patern biping

Hi I need help with code. On ESP I have 2 sensors Microwave body and PIR sensor.
I and make Automation when both detect movement active buzzer start bip…
How to make this code shorter and last part buzzing to reppet. Maybe script for automation?
Some suggestion? This code work but I think must be better way to do this…
Thanks!

binary_sensor:
    # Microwave Sensor
  - platform: gpio
    pin: D2
    id: wave_motion
    name: "SensorNode Microwave"
    device_class: motion
    # PIS Sensor
  - platform: gpio
    pin: D6
    id: pir_motion
    name: "SensorNode PIR"
    device_class: motion
    
# Buzzer
switch:
  - platform: gpio
    pin: D5 
    name: "Biping"
    id: bip
    icon: "mdi:bullhorn-outline"
  # Biping patern  
  - platform: template
    name: "Bip patern"
    id: biping
    turn_on_action:
      - while:
          condition:
            lambda: 'return true;'
          then:
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            
            - delay: 1500ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
            - switch.turn_on: bip
            - delay: 50ms
            - switch.turn_off: bip
            
            - delay: 1500ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
            - switch.turn_on: bip
            - delay: 50ms
            - switch.turn_off: bip
            
            # after this repeat only bottom of this
            - delay: 1500ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
            - switch.turn_on: bip
            - delay: 50ms 
            - switch.turn_off: bip
            - delay: 50ms
    turn_off_action:
      - switch.turn_off: bip
    ```

Check out RTTL Buzzer component - will do what you appear to be doing with a lot less code:

Search “RTTL ringtone tutorial” in Google if you need further explanation of the format.

That is for a passive buzzer, but you are right it would be easier.

Ah - yes rereading you are correct. I have a bunch of tiny speakers in my parts bin I used with RTTL, which is why I suggested it. Maybe @sickworm you have one laying about? Only issue is they are not too loud compared to an active piezo.

Maybe a solution using Slow PWM?