This post may contain affiliate links. Please read my disclaimer for more info.

One of the first things I usually do in the morning is sit down at my desk in my office and work on this blog or catch up on the news. To add some automation to my setup I wanted to detect if I was at my desk so I could properly light the room. Using an Ultrasonic Distance Sensor with a NodeMCU, I used distance to determine if someone was sitting at the desk or not.

I’m just using this today to trigger lighting in the room. You could use this to start up your PC using Wake on LAN or anything else to automate your routine. Think of this setup as a basic form of room based presence detection. I am the main person who uses the office in my household and I’m always at my desk when I’m working. This idea could be extended to cover multiple desks within a room.

Hardware

The main hardware component here is the Ultrasonic Distance Sensor. The sensor uses two pins to send an ultrasonic sound wave to measure distance. The sensor will be mounted to the underside of the desk. When someone is sitting at the desk, their legs and body will make the distance read by the sensor shorter than if no one is in the chair. For this project, I’m not looking for an accurate measurement of how far someone is from the sensor. Instead, I’m just trying to determine if someone is in the chair or not.

An Ultrasonic Distance Sensor is perfect for this task, since it can measure from 2cm to 500cm and just requires two pins from a microcontroller. I was already using a NodeMCU in my office to measure temperature for my automated ceiling fan, so I’ll just add the distance sensor to my setup.

The circuit for the distance sensor is pretty straightforward. You need 5V and GND to power the sensor and two GPIOs for the “trigger” and “echo” pins.

After soldering it up on a protoboard and putting it in a case, I mounted it underside my desk pointed toward the chair.

Software

esphome

On the software side, I’ll be using esphome again to generate the firmware for the device. The esphome project has some instructions for installation in their documentation if you’ve never used it before. I really like the direction of esphome and how easy it makes to create robust IoT applications. The Ultrasonic Distance Sensor component of esphome allows you to easily configure the sensor.

For my setup, I used the following configuration file.

esphome: name: office platform: ESP8266 board: nodemcuv2 wifi: ssid: !secret wifi_ssid password: !secret wifi_password sensor: - platform: ultrasonic trigger_pin: 4 echo_pin: 5 name: "Office Desk Chair Distance" update_interval: 100ms filters: - delta: 0.10 - platform: dht pin: 14 temperature: name: "Office Temperature" humidity: name: "Office Humidity" model: DHT22 update_interval: 60s status_led: pin: 16 # Enable logging logger: # Enable Home Assistant API api: ota: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 esphome : name : office platform : ESP8266 board : nodemcuv2 wifi : ssid : !secret wifi_ssid password : !secret wifi_password sensor : - platform : ultrasonic trigger _ pin : 4 echo _ pin : 5 name : "Office Desk Chair Distance" update _ interval : 100ms filters : - delta : 0.10 - platform : dht pin : 14 temperature : name : "Office Temperature" humidity : name : "Office Humidity" model : DHT22 update _ interval : 60s status _ led : pin : 16 # Enable logging logger : # Enable Home Assistant API api : ota :

A few interesting pieces of this configuration:

The Ultrasonic Sensor is pretty straightforward to set up. I’m using pins 4 and 5 from the NodeMCU and reading the sensor every 100ms.

To cut down on the amount of data transmitted, I’m using the delta filter to only send the data when it changes by 0.10.

Like I mentioned earlier, this also has a DHT22 to provide temperature data to automate my ceiling fan.

Home Assistant

On the Home Assistant side, you first need to enable the ESPHome integration so that Home Assistant knows about your device. Next, I wanted to create a template sensor that tracks if someone is sitting in the chair or not. After sitting in various positions, I found that if the distance measured is less than 0.65m than there likely was someone sitting in the chair. Using some basic templating, these states are visible in Home Assistant.

--- # Sensor to determine if someone is in the office desk chair platform: template sensors: office_desk_chair: friendly_name: "Office Desk Chair" value_template: >- {% if (states('sensor.office_desk_chair_distance') | float ) < 0.65 %} Occupied {% else %} Unoccupied {% endif %} 1 2 3 4 5 6 7 8 9 10 11 12 13 --- # Sensor to determine if someone is in the office desk chair platform : template sensors : office _ desk _ chair : friendly _ name : "Office Desk Chair" value _ template : > - { % if ( states ( 'sensor.office_desk_chair_distance' ) | float ) < 0 . 65 % } Occupied { % else % } Unoccupied { % endif % }

This sensor compares the value of the distance sensor to 0.65 . When the value is less than 0.65 than there is likely someone sitting in the chair, so the sensor state is set to Occupied else it is set to Unoccupied . In the Home Assistant user interface, you get a new sensor showing the value.

Finally, we can write some automations based on this. Some basic ones to get started will be turning on the lights whenever the chair is occupied, and turning off the lights after the chair has been unoccupied for 5 minutes. I’m really the only one that uses the office, so this makes sense for my setup, but you may need to add some additional logic if you have multiple desks.

To turn on the lights we just trigger on the state change and call the light.turn_on service.

--- alias: Office Turn On Lights Sitting trigger: - platform: state entity_id: sensor.office_desk_chair to: Occupied action: - service: light.turn_on entity_id: light.office_lights 1 2 3 4 5 6 7 8 9 10 --- alias : Office Turn On Lights Sitting trigger : - platform : state entity _ id : sensor.office_desk_chair to : Occupied action : - service : light.turn_on entity _ id : light.office_lights

To turn off the lights, we trigger when the sensor has been unoccupied for 5 minutes straight. That way, if you go to the bathroom or get something to eat, the lights aren’t off when you get back.

--- alias: Office Turn Off Lights Sitting trigger: - platform: state entity_id: sensor.office_desk_chair to: Unoccupied for: minutes: 5 action: - service: light.turn_off entity_id: light.office_lights 1 2 3 4 5 6 7 8 9 10 11 12 --- alias : Office Turn Off Lights Sitting trigger : - platform : state entity _ id : sensor.office_desk_chair to : Unoccupied for : minutes : 5 action : - service : light.turn_off entity _ id : light.office_lights

Next Steps

Thanks for reading my tutorial on the Ultrasonic Distance Sensor. This was a pretty easy project to get up and running and overall is fairly accurate. To improve my setup in the future I could add a couple more distance sensors under my desk to reduce missed readings if I’m sitting off center. I’d also like to add more sensors to the room so that the lights could come on before I get to my desk. Finally, I plan on using the Wake on LAN Component of Home Assistant to automatically boot up my computer.

Make sure to follow Self Hosted Home on social media and signup for my mailing list to get the latest articles. Let me know in the comments other uses you’ve found for these cheap sensors.