Close

Integrating with Home Assistant through ESPHome

A project log for Standing desk remote control

Controlling standing desk by voice through google assitant, home assistant and esphome on esp8266

lubos-horacekLubos Horacek 04/12/2019 at 10:500 Comments

Amazing https://esphome.io/ project makes it super easy to integrate most of Arduino modules with Home Assistant through ESP8266 or ESP32. tried in on one or two Sonoff devices, but how difficult is to integrate something custom, reading and sending some UART data.

Turns out that it's extremely easy. Just implement custom sensor, that allows you to almost copy paste your Arduino IDE C code, register in yaml config and your height sensor reading data from UART is done.

Sinding messsges is even more easy, as there is already UART switch, that give you option to send message on switch trigger.

Custom sensor:

class TableHeightSensor : public PollingComponent,  public UARTDevice, public sensor::Sensor{
 public:
  TableHeightSensor(UARTComponent *parent) : PollingComponent(1000), UARTDevice(parent) {}

  byte last[3];
  float value = 0;
  float lastPublished = -1;

  void setup() override {
    // This will be called by App.setup())
  }

  void loop() override {
    while (available()) {
      byte in = read();
      if(last[2] == 0xAA && last[1] == 0xA6){
        value = (((255*in) + last[0]) / 43.225) - 2; // substracting to make it display same number as controller
      }
     last[2] = last[1];
     last[1] = last[0];
     last[0] = in;
     yield();
   }
  }

  void update() override {
    if(value != lastPublished) {
      publish_state(value);
      lastPublished = value;
    }
  }
};

ESPHome config:

uart:
  id: uart_bus
  tx_pin: GPIO5
  rx_pin: GPIO4
  baud_rate: 9600

sensor:
  - platform: wifi_signal
    name: "WiFi Signal ${upper_devicename}"
    update_interval: 60s
  - platform: custom
    lambda: |-
      auto height_sensor = new TableHeightSensor(id(uart_bus));
      App.register_component(height_sensor);
      return {height_sensor};
    sensors:
      name: "${upper_devicename} height"
      unit_of_measurement: cm
      accuracy_decimals: 1

switch:
  - platform: uart
    name: "Sitting"
    id: switch1
    icon: mdi:format-vertical-align-bottom
    data: [0x55, 0xAA, 0xD1, 0xD1, 0xD1]
    internal: False
  - platform: uart
    name: "Standing"
    id: switch4
    icon: mdi:format-vertical-align-top
    data: [0x55, 0xAA, 0xD7, 0xD7, 0xD7]
    internal: False
  # not bothering with exposing more buttons

Now just go to Hass dashboard and integrate one more ESPHome device:

Discussions