$5 Sensor That Monitors Your TV

DIY sensor that monitors the state of your TV with MQTT

Photo by Lucrezia Carnelos on Unsplash

In a smart home, TVs play a significant role. By simply tracking the state of your TV, there can be some great use cases…

Have the lights dim when you start watching TV and have them turn back on when you’re done.

Have a daily summary displayed on your smart home dashboard of how much time you’ve spent watching TV.

Have your speakers automatically switch to the TV input when the TV turns on.

The problem is, even with high-end TVs, there is often no reliable way to track the state of a TV. Sure, “smart” TVs claim to integrate with this and that smart home platform, but from my experience, they are often unreliable and have a long delay before updating their states.

I want to share how I found a reliable way of tracking the state of a TV.

What you need…

This sensor will communicate using MQTT, so you’ll need an MQTT broker and a smart home hub (e.g Home Assistant, OpenHAB, Hubitat) that can receive MQTT messages.

Note: If you are using Home Assistant this sensor can integrate natively using the ESPHome API, you DO NOT NEED MQTT.

An MQTT broke r (I recommend Mosquito , or you can use a cloud broker)

r (I recommend , or you can use a cloud broker) A smart home hub with MQTT capabilities (I highly recommend Home Assistant )

with MQTT capabilities (I highly recommend ) An ESP8266 or ESP32 NodeMCU (They are a lot cheaper on AliExpress )

or (They are a lot cheaper on ) A micro USB cable

The total cost, assuming you are already running the software, is around $5 depending on which platform you plan to purchase the NodeMCU.

The ESP8266 NodeMCU

Let’s make it…

The big idea for this sensor is that the NodeMCU is connected to the USB port of your TV. When the TV is on, there is power supplied to it. When the TV is off, there is no power to it and the NodeMCU goes offline. The status of the NodeMCU is updated with MQTT.

Step 1

Install ESPHome, the software that the NodeMCU will run. It is supported on multiple platforms and the installation steps are documented on their website.

The ESPHome dashboard

Step 2

Upload this code to the NodeMCU, using ESPHome. The uploading steps can also be found on their website.

esphome:

name: tv_status_sensor

# Delete these two lines if you are using ESP32

platform: ESP8266

board: nodemcuv2

# Uncomment these two lines if you are using ESP32

# platform: ESP32

# board: nodemcu-32s # Change this to your Wi-Fi config

wifi:

ssid: your_wifi_ssid

password: your_wifi_password

fast_connect: true

ap:

ssid: “TV Sensor Fallback Hotspot”

# Set a password in case Wi-Fi fails

password: “change_this_please!” # Fallback portal

captive_portal: # Logging

logger: # Uncomment this if you use Home Assistant

#api:

# password: “change_this_please!” # Fill your MQTT information

# Delete this if you're using Home Assistant

mqtt:

broker: 10.0.0.2

username: livingroom

password: MyMQTTPassword # Over-The-Air updates

ota:

password: “change_this_please!” # TV status sensor

binary_sensor:

platform: status

name: “TV Status”

Compile and upload the code

Step 3

Using the micro USB cable, connect the NodeMCU to the USB port on your TV.

The two USB ports on the back of our TV

All done!

Voila! You’ve made a simple TV tracking sensor! Configure the sensor with MQTT (or the ESPHome API if you use Home Assistant).

Enjoy!