Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The Variables for this device are here AND in the secrets file!
- substitutions:
- device_name_friendly: 'Kamer'
- device_name: 'kamer' #DNS NAME!
- #More changes after here required.
- esphome:
- name: ${device_name}
- platform: ESP32
- board: mhetesp32minikit
- on_boot: #For fun let the light transition trough random colours when booting
- # - light.turn_on:
- # id: rgblicht
- # brightness: 100%
- # red: 0%
- # green: 0%
- # blue: 100%
- # - delay: 1s
- # - light.control:
- # id: rgblicht
- # effect: "Random Effect"
- # - delay: 10s
- # - light.control:
- # id: rgblicht
- # effect: "none"
- - if: #Turn off the screen after booting if microwave and pir sensor don't see motion.
- condition:
- and:
- - binary_sensor.is_off: microwave
- - binary_sensor.is_off: pir
- then:
- - lambda: id(scherm).turn_off();
- wifi:
- ssid: !secret wifi_ssid_iot
- password: !secret wifi_password_iot
- #fast_connect: true #probably not necessary
- #reboot_timeout: 45min #probably not necessary
- # power_save_mode: none #fixed regular disconnects causing sensor data to be 'unavailable'. No longer necessar
- # Optional manual IP
- manual_ip:
- static_ip: 192.168.0.25 #Necessary to be able to read the logs and update the device over wifi rather than using an USB cable
- gateway: 92.168.0.1 #probably not necessary
- subnet: 255.255.255.0 #probably not necessary
- # dns1: 195.130.130.1 #probably not necessary
- # dns2: 195.130.131.1 #probably not necessary
- # Enable fallback hotspot (captive portal) in case wifi connection fails
- #ap:
- # ssid: '${device_name_friendly}Fallback'
- # password: !secret wifi_fallback_password
- #captive_portal:
- # Enable logging
- #logger:
- # level: DEBUG
- # level: VERY_VERBOSE I can only get the CO2 sensor manual calibration to work in very verbose mode
- # Enable Home Assistant API
- api:
- password: !secret esphome_local
- ota:
- password: !secret esphome_local
- esp32_ble_tracker: #required to activate the bluetooth low energy tracker. Which is needed for the Xiaomi Smart Scale integration and for bluetooth_proxy
- bluetooth_proxy: #Home Assistant can expand its Bluetooth reach by communicating through the Bluetooth proxy component in ESPHome
- light:
- - platform: fastled_clockless
- chipset: WS2812
- pin: GPIO23
- num_leds: 1
- rgb_order: GRB
- name: '${device_name_friendly} RGB LED'
- id: rgblicht
- effects:
- - random:
- name: "Random Effect"
- transition_length: 1s
- update_interval: 1s
- # Define pins to be used on the ESP32 for UART which is used by the senseair platform to provide the data
- uart:
- rx_pin: GPIO16
- tx_pin: GPIO17
- baud_rate: 9600
- # setup ic bus
- i2c:
- sda: 21
- scl: 22
- scan: True
- id: bus_a #Probably not necessary
- sensor:
- # - platform: wifi_signal
- # name: '${device_name_friendly} WiFi Signal'
- # update_interval: 60s
- - platform: bh1750 #setup illuminance sensor
- id: luminosity
- name: '${device_name_friendly} illuminance'
- address: 0x23
- #measurement_duration: 254 #Higher value allows to measure darker scenes and probably also more accurate but reduces Max range. No longer an option as it is now calculated dynamically
- filters:
- - delta : 0.5
- update_interval: 5s
- on_value:
- then:
- - light.turn_on:
- id: rgblicht
- brightness: !lambda |-
- //sets the brightness of the LED based on luminosity sensor. 0LUX sets brightness to 20% (below that my LED flickers) while 200LUX or higher sets brightness to 100%
- static float brightness_intense = 0.0;
- static float brightness_max = 1.0;
- static float brightness_min = 0.0;
- brightness_intense = (x / 250.0) + 0.2;
- brightness_intense = clamp(brightness_intense,brightness_min,brightness_max);
- return brightness_intense;
- - platform: aht10 #setup temperature & humidity sensor.
- temperature:
- id: '${device_name}temp'
- name: "${device_name_friendly} Temperature"
- filters:
- - offset: +0.5
- - delta : 0.25
- humidity:
- id: '${device_name}hum'
- name: "${device_name_friendly} Humidity"
- filters:
- - offset: -4.0
- - delta : 0.5
- update_interval: 5s
- - platform: senseair #Setup the sensair sensor
- id: '${device_name}CO2'
- co2:
- name: '${device_name_friendly} CO2'
- id: '${device_name}co2state'
- on_value:
- then:
- - light.turn_on:
- id: rgblicht
- state: on
- # brightness: 100% #Already defined this based on luminosity sensor
- # Objective: 400ppm=Green; 900ppm=Yellow; 1200ppm=Red with transitions in between
- # -->Between 400ppm and 900ppm Red should gradually increase from 0 to 1: Formula: (X-400)/(900-400) & clamp function will restrict result to be between 0 and 1.
- # -->Between 900ppm and 1200ppm Green should gradually decrease from 1 to 0: Formula: 1-(X-900)/(1200-900) & clamp function will restrict result to be between 0 and 1.
- red: !lambda |-
- static float red_intense = 0.0;
- static float red_max = 1.0;
- static float red_min = 0.0;
- ESP_LOGD("main", "Input Number is: %f", x);
- red_intense = ((x-400.0) / 500.0);
- ESP_LOGD("main", "Red Number is: %f", red_intense);
- red_intense = clamp(red_intense,red_min,red_max);
- ESP_LOGD("main", "Red Number clamped is: %f", red_intense);
- return red_intense;
- green: !lambda |-
- static float green_intense = 0.0;
- static float green_max = 1.0;
- static float green_min = 0.0;
- ESP_LOGD("main", "Input Number is: %f", x);
- green_intense = (1 - (x-900.0) / 300.0);
- ESP_LOGD("main", "Green Number is: %f", green_intense);
- green_intense = clamp(green_intense,green_min,green_max);
- ESP_LOGD("main", "Green Number clamped is: %f", green_intense);
- return green_intense;
- blue: 0.0
- filters:
- - delta : 4.0 #only send the result if the parts per million difference with the last sent result is higher than this
- update_interval: 4s #delta filter above will prevent too many updates
- - platform: homeassistant #Get value of my outdoor temperature sensor from home assistant
- id: outdoor_temperature
- entity_id: sensor.temperature_buiten
- internal: true
- binary_sensor:
- - platform: gpio #Microwave technology motion sensor
- pin: GPIO36
- name: '${device_name_friendly} Microwave'
- id: microwave
- internal: true #Don't expose this sensor to Home Assistant
- device_class: motion
- on_state: #turns on the OLED display if microwave motion is detected and turns it off 15 seconds after PIR and Microwave sensors stop detecting motion
- then:
- - if:
- condition:
- - binary_sensor.is_on: microwave
- then:
- - lambda: id(scherm).turn_on();
- else: #The microwave sensor is not detecting motion (anymore)
- - wait_until: #This will stop waiting after 15 seconds or as soon as motion is detected.
- condition:
- or:
- - binary_sensor.is_on: microwave
- - binary_sensor.is_on: pir
- timeout: 15s
- - if: #If wait_untill finished due to the timeout it means the motion sensors have not detected any motion for 15s and thus the screen can be turned off. As we don't know whether this is the reason wait_untill finished we need to check whether indeed both sensors are off.
- condition:
- and:
- - binary_sensor.is_off: microwave
- - binary_sensor.is_off: pir
- then:
- - lambda: id(scherm).turn_off();
- - platform: gpio #PIR technology motion sensor
- pin: GPIO19
- id: pir
- internal: true #Don't expose this sensor to Home Assistant
- name: '${device_name_friendly} PIR'
- device_class: motion
- on_state: #turns on the OLED display if PIR motion is detected and turns it off 15 seconds after PIR and Microwave sensors stop detecting motion
- then:
- - if:
- condition:
- - binary_sensor.is_on: pir
- then:
- - lambda: id(scherm).turn_on();
- else: #The pir sensor is not detecting motion (anymore)
- - wait_until: #This will stop waiting after 15 seconds or as soon as motion is detected.
- condition:
- or:
- - binary_sensor.is_on: microwave
- - binary_sensor.is_on: pir
- timeout: 15s
- - if: #If wait_untill finished due to the timeout it means the motion sensors have not detected any motion for 15s and thus the screen can be turned off. As we don't know whether this is the reason wait_untill finished we need to check whether indeed both sensors are off.
- condition:
- and:
- - binary_sensor.is_off: microwave
- - binary_sensor.is_off: pir
- then:
- - lambda: id(scherm).turn_off();
- - platform: template #Create virtual motion sensor that is only triggered if both Microwave and PIR sensor see motion
- id: motion
- name: '${device_name_friendly} Motion'
- device_class: motion
- lambda: !lambda |-
- if ((id(microwave).state) and (id(pir).state)) {
- return true;
- } else {
- return false;
- }
- switch:
- - platform: template
- name: "Trigger background calibration"
- turn_on_action:
- - senseair.background_calibration: '${device_name}CO2'
- #- platform: template #result will be printed in the logs
- # name: "Get background calibration result"
- # turn_on_action:
- # - senseair.background_calibration_result: '${device_name}CO2'
- #- platform: template #result will be printed in the logs
- # name: "Get ABC Period"
- # turn_on_action:
- # - senseair.abc_get_period: '${device_name}CO2'
- - platform: template
- name: "Disable ABC"
- turn_on_action:
- - senseair.abc_disable: '${device_name}CO2'
- - platform: template
- name: "Enable ABC"
- turn_on_action:
- - senseair.abc_enable: '${device_name}CO2'
- font: #Define fonts to be used for the display. font file is to be stored in Home Assistant in folder config/esphome
- - file: 'BebasNeue-Regular.ttf'
- id: font0
- size: 14
- - file: 'BebasNeue-Regular.ttf'
- id: font1
- size: 20
- - file: 'BebasNeue-Regular.ttf'
- id: font2
- size: 26
- image: #Define images to be used for the display. Image file is to be stored in Home Assistant in folder config/esphome
- - file: "nomotion.png"
- id: nomotionimage
- resize: 35x35
- - file: "motion.png"
- id: motionimage
- resize: 35x35
- graph:
- # Define auto-ranged CO2 graph
- - id: ${device_name}co2graph
- sensor: ${device_name}co2state #sensor to be used
- duration: 1h #show range of 1 hour
- width: 128
- height: 64
- border: false
- x_grid: 10min #show 1 gridline per 10 minutes
- y_grid: 100 #show 1 gridline per 100PPM co2
- display: #Set up the display
- - platform: ssd1306_i2c
- model: "SSD1306 128x64"
- id: scherm
- # reset_pin: D0 #Optionally you can also connect the RESET pin to a pin on the ESP which may improve reliability. My display doesn't have this pin.
- # address: 0x3C #defaults to 0x3C which should be the right address. Otherwise check the logs.
- update_interval: never #The interval to re-draw the screen. Defaults to 5s. Set to never as I control the updates separately futher below.
- pages: #Insteadf of 1 screen that never changes this defines pages that you can transition between
- - id: page1 #Page with values of all my sensors
- lambda: |-
- // Als buitensensor HomeAssistant beschikbaar: Print Temp gemonteerde sensor + buitensensor van HomeAssistant; anders print enkel temp gemonteerde sensor.
- if (id(outdoor_temperature).has_state()) {
- it.printf(0, 0, id(font1), "°C In:%.1f Out:%.1f", id(${device_name}temp).state, id(outdoor_temperature).state);
- } else {
- it.printf(0, 0, id(font1), "%.1f°C", id(${device_name}temp).state);
- }
- // Print Humidity & LUX value (%% prints %)
- it.printf(0, 20, id(font1), "%.0f%% %.0fLUX", id(${device_name}hum).state, id(luminosity).state);
- // Print CO2 value
- it.printf(0, 40, id(font2), "%.0fppmCO2", id(${device_name}co2state).state);
- //print motion state using an image
- if (id(motion).state) {
- it.image(100, 20, id(motionimage));
- } else {
- it.image(100, 20, id(nomotionimage));
- }
- //Print on bottom right P0M1 if microwave triggered, P1M0 if PIR trigered, P1M1 if both triggered and P0M0 if none triggered
- it.printf(it.get_width(), 50, id(font0), TextAlign::TOP_RIGHT, "%s%s", id(pir).state ? "P1" : "P0", id(microwave).state ? "M1" : "M0");
- - id: page2 # Page with CO2 sensor graph. SHows that gridlines are 10 minues and 100PPm each. Also show the CO2 value on top right.
- lambda: |-
- // Draw the graph at position [x,y]
- it.graph(0, 0, id(${device_name}co2graph));
- it.print(0, 48, id(font0), "10min");
- it.print(0, 0, id(font0), "100ppm");
- it.print(60, 0, id(font0), "CO2");
- it.printf(it.get_width(), 0, id(font1), TextAlign::TOP_RIGHT, "%.0f", id(${device_name}co2state).state);
- # cycle through pages on a timer and regularly update the content. In practice this shows page2 for 3 seconds and then page1 for 10 seconds. Not sure why it's not 7 seconds for page1.
- interval:
- - interval: 10s
- then:
- - display.page.show: page2
- - component.update: scherm
- - delay: 3s
- - display.page.show: page1
- - component.update: scherm
- - delay: 1s
- - component.update: scherm
- - delay: 1s
- - component.update: scherm
- - delay: 1s
- - component.update: scherm
- - delay: 1s
- - component.update: scherm
- - delay: 1s
- - component.update: scherm
Add Comment
Please, Sign In to add comment