The Problem

I’ve lived in several apartments where the wall switch controls a single socket of one outlet. The expectation is that you would plug a floor lamp into that outlet and use the wall switch to control it, but I like to have all of my lighting integrated into my home automation system. That leaves a couple options:

  • Replace the switch with a smart switch. This is the best answer and the one I would do, but I can’t modify the wiring in an apartment so that’s a non-starter.
  • Leave the switch in the “on” position at all times and put a smart bulb in the lamp. This would allow automated control of the light, but the switch is useless. Additionally, if someone accidentally turns off the switch, the light can no longer be controlled. This isn’t ideal, but it is a common compromise people make when installing smart light bulbs.

Neither answer is perfect for me, so I came up with a new idea.

The Solution

What if I could just plug in a device to the outlet that would detect if the switch was on or off? Functionally that would be identical to installing a smart switch, but it could be installed and removed in seconds by just unplugging it.

Enter the “outlet power sensor” (name pending — suggestions?)

Prototype sensor

Prototype sensor with switched output

The sensor has two separate plugs that must both be connected. One provides constant power to run the device, and the other is the sensor plug. When the sensor plug is connected to power, a binary_sensor in Home Assistant will be activated instantly.

By plugging these two plugs into the switched and unswitched sockets of an outlet, you can instantly convert a wall switch into a smart switch.

This version also includes two output sockets. One is connected to constant power so you can still use the outlet even though this device occupies both sockets. The other output is connected to a relay and exposed in Home Assistant as a separate switch component, allowing independent control of additional devices. If you don’t need outputs, the wiring can be simplified.

Real-World Usage

So it’s a useful device, but what does it actually do? Here’s an example from my setup.

In my bedroom, a wall switch next to the door normally controls an outlet in an inconvenient location, not where I wanted the main light. I put a floor lamp with a smart bulb in the ideal spot and set up control of the light with remotes on our bedside tables. That left only voice control to turn on the light when you enter the room and a confusing, effectively useless wall switch for guests.

I plugged an Outlet Power Sensor™ into the switched outlet and wrote a very simple automation so toggling the wall switch toggles the lamp. Now I can control the light from the wall switch, automations, bedside remotes, phones, and voice control. All without modifying any wiring.

I’m sold. How do I build one?

I had this idea a few years back and was surprised there wasn’t a product on the market for it. It seems the apartment-dwelling home automation market is still small. I’ve built a few versions over time — including a clever setup that involved disassembling an LED nightlight — but ultimately didn’t feel like the projects were safe enough to share with other people.

Then I realized I could build one out of a Shelly. If you’re unfamiliar, it’s a cheap WiFi relay intended to be mounted behind an existing switch to convert a standard switch into a smart switch. Sounds very similar to what we’re doing! The Shelly already provides the high-voltage connections we need safely, and it’s a trusted, UL-listed device. It’s definitely a step up from hacking up a nightlight.

Parts List

  • 1 × Shelly 1 - about $20
  • 2 × basic indoor extension cords like this one - about $3
    • Any basic extension cords will work. You’ll be cutting them down to your preferred length, so shorter is better.
    • If you’d like to use a grounded version like this one, you’ll have to identify and connect all of the ground wires together as well.
  • 2 × wire nuts or WAGOs - basically free (buy a mixed pack and keep them on hand)

Note that Shelly now has many versions. The Shelly 1 Mini would likely be a perfect replacement for the older Shelly 1. Just make sure you use the correct terminals and update the GPIO pins and board type accordingly in ESPHome.

Wiring

Wiring Diagram

Diagram for the simple version without outputs

For the version without outputs, the wiring is very simple. The Shelly is powered by the line/hot (L) and neutral (N) wires from the unswitched outlet. From the switched outlet, connect the line/hot side (smaller slot) to connect to the switch input (SW) on the Shelly. This way, the Shelly can sense when there is power on the switched outlet.

The O and I terminals are not used for this setup. If you want to have a separate switched output, you’ll use these terminals to pass through the relay.

Software - ESPHome

While the Shelly comes with very decent software that even includes the ability to run the device in a fully-local setup, I prefer to use ESPHome on my devices. ESPHome is an excellent open-source project that allows YAML configuration of devices and very clean integration with Home Assistant.

I won’t go into detail about how to flash ESPHome onto a Shelly, but there are a few options. I used a cheap FTDI USB to TTL serial adapter and connected directly to the device. The Tasmota and ESPHome docs will be helpful for that type of install. I recently came across a way to flash the Shelly over-the-air from the stock firmware, but I haven’t tested this process myself.

The configuration I use for ESPHome is available here, but it’s pretty simple.

First is the boilerplate stuff:

esphome:
  name: bedroom_outlet_sensor

esp8266:
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

logger:
api:
ota:

Note that the wifi settings use !secrets. This feature allows you to extract private information from the YAML configurations.

Then we get into the slightly more interesting stuff: inputs and outputs.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
    name: "Bedroom Outlet Sensor"
    device_class: power

# Only relevant if you included outputs.
switch:
  - platform: gpio
    pin: GPIO4
    name: "Bedroom Outlet Output"

That’s the entire configuration. It just sets up a binary_sensor representing the wall switch state and a switch component for the switched output.

Configuration in Home Assistant

Using ESPHome has another benefit: autodiscovery in Home Assistant. If you flash the new configuration to your Shelly, Home Assistant should prompt you to configure the device automatically.

After it’s set up, you should have a new entry in the “Settings → Devices” menu of Home Assistant:

Screenshot of Home Assistant integration

At this point, you can flip the switch on the wall and you should see the outlet sensor entity change state. That means we’re almost done.

Automation

We’ve made it to the last step: tying it all together.

How you proceed here will depend on what you want the wall switch to do when you flip it. Now that it’s just a binary_sensor in Home Assistant, anything is possible. I have it simply toggle the smart light in a lamp in the same room, but you could have it lock all the smart locks, turn the lights red, and play a lullaby on your speakers. These are the benefits of integrating everything into one central hub.

For a simple example, here’s an automation that just toggles a light:

automation:
  - alias: Bedroom Light Switch - Toggle Lamp
    trigger:
      - platform: state
        entity_id: binary_sensor.bedroom_outlet_sensor
        from: "off"
        to: "on"
      - platform: state
        entity_id: binary_sensor.bedroom_outlet_sensor
        from: "on"
        to: "off"
    action:
      - choose:
          # If currently off, turn on full brightness.
          - conditions:
              - condition: state
                entity_id: light.bedroom_lamp
                state: "off"
            sequence:
              - service: light.turn_on
                entity_id: light.bedroom_lamp
                data:
                  # Always set it to full brightness when turning on.
                  brightness: 255
          # If currently on, turn off.
          - conditions:
              - condition: state
                entity_id: light.bedroom_lamp
                state: "on"
            sequence:
              - service: light.turn_off
                entity_id: light.bedroom_lamp

Wrapping Up

I’ve been using two of these simple devices in my apartment for about 6 months now and I’m amazed by how good the experience is. As a renter, replacing fixtures and switches is out of the question, but these sensors give the full smart switch experience with no permanent modifications. They also improve the overall user experience because they use an existing, well-known setup that guests don’t have to understand to use. To everyone other than me, the switch just controls the light, and that’s all they have to know. Finally, by decoupling the light switch from the light itself, I can configure multiple inputs and multiple outputs. I can turn the light on with the light switch, have it dim automatically when we watch a movie, and turn it off from my bedside button as part of my nighttime routine.

Good home automation is all about improving the experience without getting in the way; this project has fulfilled both of those goals.