ESP Home

Creating a Custom ESPHOME sensor

Example based on VEML6070

Stage-1

  1. Create a (logical named) file, go to HA File Editor, at the top select add new file:
    1. Enter the required file name e.g.  ‘/config/veml6070_sensor.h’
    2. Add this text to the file:
    3. #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);
      }
      };
    4. Go to ESPHOME and create a new sensor, enter these definitions:
    5. 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

    6. 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.

  1. Connect an e-paper display to the standard SPI bus of an ESp32 and in this example a BME280 to the I2C bus.
  2. Create a new ESPHOME sensor, choose the ESP32 paste this definition into the configuration:
    1. esphome:
      name: esphome-epaper
      platform: ESP32
      board: esp32dev# Enable logging
      logger:# Enable Home Assistant API
      api:ota:
      password: “69316421ed5d91f82eea624ddbd56bb5”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: 15min

      font:
      – 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: 15

      spi:
      clk_pin: SCK
      mosi_pin: MOSI

      i2c:
      sda: 21
      scl: 22
      scan: true

      time:
      – platform: sntp
      id: sntp_time
      – platform: homeassistant
      id: esptime

      sensor:
      – 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: 60s

      display:
      – 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);

  3. 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

  1. Create a (logical named) file, go to HA File Editor, at the top select add new file:
    1. Enter the required file name e.g.  ‘/config/veml6075_sensor.h’
    2. Add this text to the file:
    3. #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);
      }
      };

  2. Go to ESPHOME and create a new sensor, enter these definitions:
    1. esphome:
      name: veml6075
      platform: ESP32
      board: esp32dev
      includes:
      – /config/veml6075_sensor.h
      libraries:
      – “https://github.com/adafruit/Adafruit_VEML6075”# Example configuration entry
      deep_sleep:
      run_duration: 10s
      sleep_duration: 15min

      # Enable logging
      logger:

      # Enable Home Assistant API
      api:

      ota:
      password: “c93783d31727db136639c87cb5242536”

      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_a

      sensor:
      – 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: 

  3. 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.
  4. 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.