Creating a Custom ESPHOME sensor
Example based on VEML6070
Stage-1
- Create a (logical named) file, go to HA File Editor, at the top select add new file:
- Enter the required file name e.g. ‘/config/veml6070_sensor.h’
- Add this text to the file:
- #include “esphome.h”
#include “Adafruit_VEML6070.h”class VEML6070CustomSensor : public PollingComponent, public Sensor {
public:
Adafruit_VEML6070 uv = Adafruit_VEML6070();Sensor *uv_sensor = new Sensor();
Sensor *uvi_sensor = new Sensor();VEML6070CustomSensor() : PollingComponent(15000) {}void setup() override {
Wire.begin();
uv.begin(VEML6070_1_T); // 0 – 2055 output equates to 0 – 11 UVI
}void update() override {
uint16_t uv_value = uv.readUV();
uint16_t uvi_value = uv.readUV() / 186.82;
ESP_LOGD(“custom”, “UV Light : %i”, uv_value);
ESP_LOGD(“custom”, “UV Index : %i”, uvi_value);
uv_sensor->publish_state(uv_value);
uvi_sensor->publish_state(uvi_value);
}
}; - Go to ESPHOME and create a new sensor, enter these definitions:
- esphome:
name: veml6070
platform: ESP32
board: esp32dev
platformio_options:
board_build.f_cpu: 8000000L
includes:
– /config/veml6070_sensor.h
libraries:
– “https://github.com/adafruit/Adafruit_VEML6070”deep_sleep:
run_duration: 15s
sleep_duration: 15min# Enable logging
logger:i2c:
sda: 21
scl: 22
scan: true
id: bus_a# Enable Home Assistant API
api:ota:
password: “0892843a5e8b4573f68bcbd4827bb41a”wifi:
ssid: “your SSID”
password: “your WiFi password”# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “Veml6070 Fallback Hotspot”
password: “FE90XN1LMu3Z”captive_portal:sensor:
– platform: custom
lambda: |-
auto veml6070 = new VEML6070CustomSensor();
App.register_component(veml6070);
return {veml6070->uv_sensor, veml6070->uvi_sensor};
sensors:
– name: “VEML6070 UV”
id: uv
unit_of_measurement: “mW/cm²”
accuracy_decimals: 0
– name: “VEML6070 UVI”
id: uvi
unit_of_measurement: “”
accuracy_decimals: 0– platform: adc
pin: 35
attenuation: 11db
name: “VEML6070 Battery”
accuracy_decimals: 1
unit_of_measurement: “V”
filters:
– multiply: 1.7796
– median:
window_size: 3
send_every: 3
send_first_at: 1 - Upload to the ESP device using ESPHOMEFLASHER
Creating an ESPHOME Sensor with e-paper display displaying BME280 sensor values
The unit reads a BME280, gets the current time from NTP time servers, then displays the result on an e-paper display, it also produces Home Assistant entities that enables HA to display the sensor readings on a gauge or graph. After updating the display the unit goes into deepsleep to conserve power and so can be battery operated.
- Connect an e-paper display to the standard SPI bus of an ESp32 and in this example a BME280 to the I2C bus.
- Create a new ESPHOME sensor, choose the ESP32 paste this definition into the configuration:
- esphome:
name: esphome-epaper
platform: ESP32
board: esp32dev# Enable logging
logger:# Enable Home Assistant API
api:ota:
password: “69316421ed5d91f82eba624ddbd56bb5”wifi:
ssid: “your WiFi SSID”
password: “your WiFi password”# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “Esphome-Epaper Fallback Hotspot”
password: “9xmfEToiLKs7”captive_portal:deep_sleep:
run_duration: 5s #gives time to reset when the IP resolution stage is attempted and enables an OTA update! if shorter needs a esphomeflasher update
sleep_duration: 15minfont:
– file: “/config/fonts/OpenSans-Regular.ttf”
id: font1
size: 15
– file: “/config/fonts/OpenSans-Bold.ttf”
id: font2
size: 15
– file: “/config/fonts/RobotoCondensed-Bold.ttf”
id: font3
size: 15
– file: “/config/fonts/RobotoCondensed-Regular.ttf”
id: font4
size: 15spi:
clk_pin: SCK
mosi_pin: MOSIi2c:
sda: 21
scl: 22
scan: truetime:
– platform: sntp
id: sntp_time
– platform: homeassistant
id: esptimesensor:
– platform: bme280
temperature:
name: “BME280 Temperature”
id: bme280_temperature
oversampling: 16x
humidity:
name: “BME280 Humidity”
id: bme280_humidity
pressure:
name: “BME280 Pressure”
id: bme280_pressure
filters:
– offset: 4.9
address: 0x76
update_interval: 60sdisplay:
– platform: waveshare_epaper
cs_pin: SS
dc_pin: 17
busy_pin: 4
reset_pin: 16
model: 2.90in
rotation: 270
full_update_every: 30
lambda: |-
it.print(0, 0, id(font3), “Sensor Readings”);
// Print time in HH:MM format
it.strftime(190, 0, id(font3), “%H:%M:%S”, id(sntp_time).now());
it.printf(0, 15, id(font3), “Temp: %.1f°C”, id(bme280_temperature).state);
it.printf(0, 30, id(font3), “Humi: %.1f%%”,id(bme280_humidity).state); // %% – for literal % sign
it.printf(0, 45, id(font3), “Pres: %.1fhPa”,id(bme280_pressure).state);
- esphome:
- If a new ESP32 then use the’ esphomeflasher‘ tool to upload the code, otherwise perform a wireless update, that’s it.
Creating a Custom ESPHOME sensor
Example based on VEML6075
Stage-1
- Create a (logical named) file, go to HA File Editor, at the top select add new file:
- Enter the required file name e.g. ‘/config/veml6075_sensor.h’
- Add this text to the file:
- #include “esphome.h”
#include “Adafruit_VEML6075.h”class VEML6075CustomSensor : public PollingComponent, public Sensor {
public:
Adafruit_VEML6075 uv = Adafruit_VEML6075();Sensor *uva_sensor = new Sensor();
Sensor *uvb_sensor = new Sensor();
Sensor *uvi_sensor = new Sensor();VEML6075CustomSensor() : PollingComponent(15000) {}
void setup() override {
Wire.begin();
uv.begin();
uv.setIntegrationTime(VEML6075_100MS);
uv.setHighDynamic(true);
uv.setForcedMode(false);
uv.setCoefficients(2.22, 1.33, // UVA_A and UVA_B coefficients
2.95, 1.74, // UVB_C and UVB_D coefficients
0.001461, 0.002591); // UVA and UVB responses
}void update() override {
float uva = uv.readUVA();float uvb = uv.readUVB();
float uvi = uv.readUVI();
if (uva < 0) uva = 0;
if (uvb < 0) uvb = 0;
if (uvi < 0) uvi = 0;
ESP_LOGD(“custom”, “The value of sensor uva is: %.0f”, uva);
ESP_LOGD(“custom”, “The value of sensor uvb is: %.0f”, uvb);
ESP_LOGD(“custom”, “The value of sensor uvi is: %.0f”, uvi);
publish_state(uva);
uva_sensor->publish_state(uva);
uvb_sensor->publish_state(uvb);
uvi_sensor->publish_state(uvi);
}
};
- Go to ESPHOME and create a new sensor, enter these definitions:
- esphome:
name: veml6075
platform: ESP32
board: esp32dev
includes:
– /config/veml6075_sensor.h
libraries: - – SPI
– Wire
– adafruit/Adafruit BusIO @ 1.9.6
– “https://github.com/adafruit/Adafruit_VEML6075”
deep_sleep:
run_duration: 10s
sleep_duration: 15min# Enable logging
logger:# Enable Home Assistant API
api:ota:
password: “c93783d31727db236639c87cb5242536”wifi:
ssid: “your SSID”
password: “your PASSWORD”# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “Veml6075 Fallback Hotspot”
password: “pUUuYvRvvoc0”captive_portal:i2c:
sda: 21
scl: 22
scan: true
id: bus_asensor:
– platform: custom
lambda: |-
auto veml6075 = new VEML6075CustomSensor();
App.register_component(veml6075);
return {veml6075->uva_sensor, veml6075->uvb_sensor, veml6075->uvi_sensor};
sensors:
– name: “VEML6075 UVA”
id: uva
unit_of_measurement: “µW/cm²”
accuracy_decimals: 0
– name: “VEML6075 UVB”
id: uvb
unit_of_measurement: “µW/cm²”
accuracy_decimals: 0
– name: “VEML6075 UVI”
id: uvi
unit_of_measurement: “”
accuracy_decimals: 0– platform: adc
pin: 35
attenuation: 11db
name: “VEML6075 Battery”
accuracy_decimals: 1
unit_of_measurement: “V”
filters:
– multiply: 1.7796
– median:
window_size: 3
send_every: 3
send_first_at:
- esphome:
- Next, INSTALL the code, but choose the ‘Manual Download‘ option, save the file, perhaps on your Desktop. If a new ESP32 then use the’ esphomeflasher‘ tool to upload the code, select the USB port, select the binary just saved with the Manual download option, then perform a write to flash.
- Once flashed you can try a wireless update, that’s it. be aware, if the ESP is sleeping then you wont always be able (by resetting) to wake it up to perform a wireless OTA update, in which case use the esphomeflasher tool again.
Creating an ESPHOME sensor for VINDRIKTNING Air-Quality Sensor
-
Stage-1
-
create a new sensor, add these lines to the sensor, save then upload. This example uses the Wemos D1 Mini
-
esphome:
name: air-quality
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: “877ebb7031b78b0e8caa2949b702f4fe”
wifi:
ssid: YourSSID
password: YourPasword
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “Air-Quality Fallback Hotspot”
password: “ZPvCXWGnniJd”
captive_portal:
uart:
rx_pin: D2
baud_rate: 9600
sensor:
– platform: pm1006
pm_2_5:
name: “Particulate Matter 2.5µm Concentration”
Graphing and Gauges
For Green set the values 1 to 35
For Amber set the range to 85
For Red set the range from 86
Adding a PMS5003 to Home Assistant
- Create a new Device, from within the ESPHome menu.
- Paste this code into the device characteristics:
- esphome:
name: aqsesp8266:
board: d1_mini# Enable logging
logger:# Enable Home Assistant API
api:ota:
password: “9ffe759df3ac5708404214d785ea05ff”wifi:
ssid: Your SSID
password: Your WiFi password# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: “Aqs Fallback Hotspot”
password: “LXfiYprMrqQH”captive_portal:
uart:
rx_pin: D7
tx_pin: D6
baud_rate: 9600sensor:
– platform: pmsx003
type: PMSX003
pm_1_0:
name: “Particulate Matter <1.0µm Concentration”
pm_2_5:
name: “Particulate Matter <2.5µm Concentration”
pm_10_0:
name: “Particulate Matter <10.0µm Concentration”switch:
– platform: gpio
pin:
number: D3
id: pms_set
name: “Start measuring”interval:
– interval: 120s
then:
– switch.turn_on: pms_set
– delay: 20s
– switch.turn_off: pms_set
- esphome:
- Connect the (in this example) Wemos D1 Mini to the PMS5003
- Apply power, then go to Home Assistant ‘Devices’ select the device ‘AQS’ and then choose add entities to a dashboard, chose the style you need.
- Entity names are:
- Particulate Matter <1.0µm Concentration
- Particulate Matter <2.5 µm Concentration
- Particulate Matter <10.0µm Concentration