Retrofit a Motion enabled Light with a Smart Home Assistant Sensor

Retrofitting existing electrical installations with some degree of additional home automation capabilities is a cumbersome activity. Most of the time, the fixed wired installations do not support adding the necessary home automation wires. Even if there is enough space for your home automation wires, the installation routes often do not end where your home automation server is located.

Long story short, my goal was to get a state change in my Home Assistant server whenever my front door is activated by detected motion.

In the absence of a wired solution, I found a quite simple and low cost solution that I will share within the remainder of this blog.

The same approach can be used for many other purposes and it definitely adds additional smartness to your existing traditional electrical installation.

Use a cheap ESP8266 board send a MQTT message

The idea is extremely simple. Instead of using any complex logic or sensors, we will program the WLAN connected ESP8266 board to always send a MQTT message in a loop when it’s powered on.

That simple behavior can be used on the Home Assistant side to decide in a so-called binary sensor if the board is running or not.

Then, we use the traditional motion enabled light as a sensor and power our ESP8266 board with the same cables as the light uses.

As a result, the ESP board is running when the light is on and it’s off when the light is off too.

The MQTT message is only received at the Home Assistant side if the motion light is on.

Hardware setup

The hardware setup is pretty cheap and simple. You need a 220 to 5V power supply module, as it is shown below, which sells under 5 USD per piece:

The ESP8266 board is shown below, which comes at a cost of around 5 USD per piece:


Connect your motion lights power cables to the 220 to 5V module (and make sure that you ask a professional electrician to help you with that, as a wrong connection can result in deadly accidents)

The two output wires of 5V positive and ground go to the boards Vin and GND pins. That’s all wiring that is necessary.

Program your ESP board

In order to send a constant MQTT message to your MQTT broker, you can use the following Arduino code snippet below:

* Select NodeMCU 0.9 for board configuration
* */
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Wifi: SSID and password
const char* WIFI_SSID = "YOUR_WIFI_SSID";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "sensor2_dht22_s_buero";
const PROGMEM char* MQTT_SERVER_IP = "";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "homeassistant";
// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "/home/house/frontdoor/sensor1";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
#include <ArduinoJson.h>
void publishData() {
 // create a JSON object
 // doc :
 StaticJsonBuffer<200> jsonBuffer;
 JsonObject& root = jsonBuffer.createObject();
 // INFO: the data must be converted into a string; a problem occurs when using floats...
 root["movement"] = "1";
       "movement": "1" ,
 char data[200];
 root.printTo(data, root.measureLength() + 1);
 client.publish(MQTT_SENSOR_TOPIC, data, true);
void connect() {
 Serial.println("INFO: Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
   Serial.println("INFO: connected");
 } else {
   Serial.print("ERROR: failed, rc=");
   Serial.println("DEBUG: try again in 5 seconds");
   // Wait 5 seconds before retrying
void setup() {
 // init the serial
 // init the WiFi connection
 Serial.print("INFO: Connecting to ");
 int count = 0;
 while (count < 25 && WiFi.status() != WL_CONNECTED) {
 Serial.println("INFO: WiFi connected");
 Serial.println("INFO: IP address: ");
 // init the MQTT connection
void loop() {
 // if MQTT lost connection then reconnect
 int count = 0;
 while (!client.connected()) {
   if(count > 25) {

Receive the message in a Home Assistant Binary Sensor

Now that your board sends a constant MQTT message whenever it is powered up, we have to receive that message in Home Assistant and convert it into a convenient binary sensor value.

See below the configuration of the companion binary sensor (which goes into the configuration.yaml file) on the Home Assistant side that receives the MQTT motion light messages:

 - platform: mqtt
   name: "Front Door Sensor"
   state_topic: "/home/house/frontdoor/sensor1"  
   value_template: '{{ value_json.movement }}'
   payload_on: "1"
   payload_off: "0"
   device_class: motion
   # automatically set the binary sensor to unavailable after 1 minute
   expire_after: 60

The important part above is to not forget the ‘expire_after’ setting that allows the binary sensor to reset to the ‘unknown’ state after 60 seconds of not receiving any MQTT update.

Chart your motion light activity

Now that you have a binary sensor defined, you can easily follow your motion light activity over time, as it is shown in the screenshot below:


By connecting a cheap ESP board to any of your traditional electric installations it is easily possible to retrofit your house with some additional smartness.

Connecting a ESP board directly with the power supply of your motion light allows you to seamlessly monitor its activation state over time.

The binary sensor in Home Assistant helps you to keep track and chart the motion state of your light over time and to easily set up any kind of automation on top, such as to record a short video of your front door in case the motion light goes on.

If you want to know more about cool Home Assistant projects, refer to my ebook on Home Assistant, shown below: