ESPHome: Buzzer alert when sensor values above certain value

I’m looking for an ESPHome yaml example for a simple gpio alert buzzer that beeps in one second intervals when one of the sensor values is above a certain value. I found a solution for a single sensor by starting, stopping a script in the sensors on_value_range, but that does only work for a single sensor. For multiple sensors I tried using global alert bool variables for each sensor, but I’m not sure how to use the variables in the script.

Can someone provide a working example for such an alert buzzer?

In the meantime I found a way to do this. Here is my configuration:

i2c:
  sda: D2
  scl: D1

output:
  - platform: gpio
    id: buzzer
    pin: 13

globals:
  - id: co2Alert
    type: bool
  - id: tvocAlert
    type: bool

script:
  - id: buzzerLoop
    mode: single
    then:
      while:
        condition:
          - lambda: |-
              return id(co2Alert) || id(tvocAlert);
        then:
          - output.turn_on: buzzer
          - delay: 1000ms 
          - output.turn_off: buzzer
          - delay: 1000ms

sensor:
  - platform: ccs811
    eco2:
      name: "CCS811 eCO2 Value"
      retain: False
      on_value_range:
        - above: 3000
          then:
            - lambda: |-
                id(co2Alert) = true;
            - script.execute: buzzerLoop
            - logger.log: "CO2 alert"
        - below: 3000
          then:
            - lambda: |-
                id(co2Alert) = false;
    tvoc:
      name: "CCS811 Total Volatile Organic Compound"
      retain: False
      on_value_range:
        - above: 500
          then:
            - lambda: |-
                id(tvocAlert) = true;
            - script.execute: buzzerLoop
            - logger.log: "TVOC alert"
        - below: 500
          then:
            - lambda: |-
                id(tvocAlert) = false;
    address: 0x5A
    update_interval: 60s
    baseline: 0x037D