Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save letsautomatenet/767c4e40cc157b48a3036d38989c8e2e to your computer and use it in GitHub Desktop.
Save letsautomatenet/767c4e40cc157b48a3036d38989c8e2e to your computer and use it in GitHub Desktop.
ESPHome Device - MQTT & HTTP Instead of Home Assistant
# Example from YouTube video: https://www.youtube.com/watch?v=0XH61uCqMDw
# Air Quality sensor adapted to not use Home Assistant
# but use MQTT and HTTP instead.
# Original Air Quality Sensor video: https://www.youtube.com/watch?v=XIY1xOzM3fs
### START - Update this section with relevant details ###
substitutions:
device_name: "device-name-here"
friendly_name: Device Name Here
board_type: d1_mini
# This is NOT USED in this script, as we are not connecting to Home Assistant
# for this example
# ESPHome API used by Home Assistant, etc
# If you go to this page it generates one for you automatically!
# https://esphome.io/components/api.html#configuration-variables
api_key: !secret api_key
ap_password: !secret ap_password # Fallback WiFi Access Point password
ota_password: !secret ota_password # Password used to remotely flash firmware
# MQTT Credentials
mqtt_host: !secret mqtt_host
mqtt_username: !secret mqtt_username
mqtt_password: !secret mqtt_password
### END ###
esphome:
name: ${device_name}
friendly_name: ${friendly_name}
esp8266:
board: ${board_type}
# Enable logging
logger:
level: INFO # VERY_VERBOSE, VERBOSE, DEBUG, INFO, WARN, ERROR, NONE
# Enable Home Assistant API
# api:
# encryption:
# key: ${api_key}
mqtt:
broker: ${mqtt_host}
username: ${mqtt_username}
password: ${mqtt_password}
http_request:
ota:
password: ${ota_password}
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: ${device_name}
password: ${ap_password}
captive_portal:
# Restart & Safe Mode Buttons
button:
- platform: restart
name: "Restart Device"
- platform: safe_mode
name: "Restart in Safe Mode"
# I2C Bus for air quality and distance sensors
i2c:
scl: D1
sda: D2
scan: true
id: bus_a
# LED Outputs
output:
- platform: gpio
id: green_led
pin: D5
- platform: gpio
id: red_led
pin: D6
- platform: gpio
id: blue_led
pin: D7
# Light switches to control LEDs
light:
- platform: binary
name: "Green LED"
output: green_led
id: green_led_light
- platform: binary
name: "Red LED"
output: red_led
id: red_led_light
- platform: binary
name: "Blue LED"
output: blue_led
id: blue_led_light
# Button to control blue LED
binary_sensor:
- platform: gpio
pin:
number: RX
inverted: true
mode:
input: true
pullup: true
id: pushbutton
name: Doorbell
filters:
- delayed_off: 10ms
on_press:
then:
- output.turn_on: blue_led
- http_request.post:
url: !secret webhook_doorbell
headers:
Content-Type: application/json
json:
text: "TEST"
verify_ssl: false
on_release:
then:
- output.turn_off: blue_led
sensor:
# FSR Pressure Sensor
- platform: adc
pin: A0
id: bedsensor1
name: "Bed Sensor"
update_interval: 1s
internal: false # Set to true to not send to HA
# Turn different LEDs on dependent on pressure applied
on_value_range:
- below: 0.6
then:
- light.turn_off: red_led_light
- light.turn_on: green_led_light
- below: 0.1
then:
- light.turn_off: red_led_light
- light.turn_off: green_led_light
- light.turn_on: blue_led_light
- above: 0.1
then:
- light.turn_off: blue_led_light
- above: 0.3
then:
- light.turn_on: green_led_light
- above: 0.6
then:
- light.turn_on: red_led_light
- light.turn_off: green_led_light
# Pressure Sensor. 5 second updates
- platform: template
name: "Bed Sensor 5s"
id: bedsensor2
accuracy_decimals: 2
update_interval: 5s
lambda: |-
return id(bedsensor1).state;
unit_of_measurement: "v"
# Air Quality Sensor. BME680
- platform: bme680
temperature:
name: "BME680 Temperature"
oversampling: 16x
pressure:
name: "BME680 Pressure"
humidity:
id: "humidity"
name: "BME680 Humidity"
gas_resistance:
id: "gas_resistance"
name: "BME680 Gas Resistance"
address: 0x77
update_interval: 60s
- platform: template
name: "BME680 Indoor Air Quality"
id: iaq
icon: "mdi:gauge"
# caulculation: comp_gas = log(R_gas[ohm]) + 0.04 log(Ohm)/%rh * hum[%rh]
lambda: |-
return log(id(gas_resistance).state) + 0.04 * id(humidity).state;
# Distance Sensor
- platform: vl53l0x
name: "Lounge Door Presence Distance 1"
id: loungedoord1
i2c_id: bus_a
address: 0x29
# enable_pin: GPIO14
timeout: 200us
update_interval: 500ms
unit_of_measurement: "m"
long_range: false
internal: false
# filters:
# - clamp:
# min_value: 10
# max_value: 75
# ignore_out_of_range: true
text_sensor:
# Simplified air quality reading
- platform: template
name: "BME680 IAQ Classification"
icon: "mdi:checkbox-marked-circle-outline"
lambda: |-
if (int(id(iaq).state) <= 50) {
return {"Excellent"};
}
else if (int(id(iaq).state) <= 100) {
return {"Good"};
}
else if (int(id(iaq).state) <= 150) {
return {"Lightly polluted"};
}
else if (int(id(iaq).state) <= 200) {
return {"Moderately polluted"};
}
else if (int(id(iaq).state) <= 250) {
return {"Heavily polluted"};
}
else if (int(id(iaq).state) <= 350) {
return {"Severely polluted"};
}
else if (int(id(iaq).state) <= 500) {
return {"Extremely polluted"};
}
else {
return {"unknown"};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment