esphome:
    name: co2_sensor_lab 
    platform: ESP32
    board: esp32doit-devkit-v1 
    on_boot:
            - display.page.show: boot

logger:
    level: DEBUG
wifi:
    ssid: !secret wifi_ssid
    password: !secret wifi_passwd

mqtt:
  broker: mqtt.unhb
  birth_message:
    topic: co2_sensor/status
    payload: online
  will_message:
   topic: co2_sensor/status
   payload: offline

uart:
  rx_pin: GPIO16 
  tx_pin: GPIO17
  baud_rate: 9600
  id: uart_mhz19

light:
  - platform: neopixelbus
    pin: GPIO4
    num_leds: 12
    name: "ws2812 ring"
    id: "ws2812"
    restore_mode: ALWAYS_OFF

output:
  - platform: ledc
    pin: GPIO12
    id: buzzer

sensor:
  - platform: mhz19
    update_interval: 10s
    automatic_baseline_calibration: true 
    uart_id: uart_mhz19
    temperature:
      name: "MH-Z19 Temperature"
      id: mhz19_temp
    co2:
      name: "MH-Z19 CO2"
      id: mhz19_co2
      on_value:
       then:
          if:
           condition:
                 sensor.in_range:
                  id: mhz19_co2 
                  above: 0
                  below: 800
           then:
            - light.turn_off:
               id: ws2812
            - display.page.show: co2
           else:
                if:
                 condition:
                  sensor.in_range:
                   id: mhz19_co2
                   above: 800
                   below: 1500
                 then:
                     - light.turn_on:
                        id: ws2812
                        brightness: 80%
                        red: 50%
                        green: 50%
                        blue: 0%
                 else:
                         if:
                                 condition:
                                         sensor.in_range:
                                                 id: mhz19_co2
                                                 above: 1500
                                 then:
                                         - light.turn_on:
                                                 id: ws2812
                                                 brightness: 100%
                                                 red: 100%
                                                 green: 0%
                                                 blue: 0%
                                         - output.turn_on: buzzer
                                         - output.ledc.set_frequency:
                                            id: buzzer
                                            frequency: "5000Hz"
                                         - output.set_level:
                                            id: buzzer
                                            level: "80%"
                                         - delay: 500ms
                                         - output.turn_off: buzzer
                                         - lambda: |-
                                            static int num_executions = 0;
                                            ESP_LOGD("main", "I am at execution number %d", num_executions);
                                            num_executions += 1;
i2c:
  sda: GPIO21 
  scl: GPIO22

font:
  - file: "font.ttf"
    id: cust_font
    size: 28  

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    brightness: 100%
    update_interval: 10s
    pages:
            - id: boot
              lambda: |-
               it.printf(64, 32, id(cust_font), COLOR_ON, TextAlign::TOP_CENTER, "starte...");
            - id: co2
              lambda: |-
               it.printf(64, 32, id(cust_font), COLOR_ON, TextAlign::TOP_CENTER, "%.0fPPM", id(mhz19_co2).state);
#            it.printf(64, 40, id(cust_font),TextAlign::TOP_CENTER, "%.0f°C", id(mhz19_temp).state);