Optocoupler to detect mains electricity on/off

I have a central heating system from Honeywell called Evohome and I wanted to know when the wireless heating and hot water controllers turned my boiler on and off.

I guess this has been covered on here before but I’ll post what I’ve done in case it helps others.

The components I’ve used are as follows:

Optocoupler: https://www.ebay.co.uk/itm/240V-220V-AC-Mains-Sensor-opto-isolator-optoisolator-optocoupler-5V-3-3V-Arduino/272619019157

ESP8266: https://www.aliexpress.com/item/32529101036.html

Here is a pic of the two in action:

As you can see below, my heating system junction box looks like a spaghetti mess!

However, if you’re careful with the 240V, you can trace all the cables and work out what it does. Here is my wiring diagram that includes the optocoupler module (I created this in Google Drawing):

The ESP module is flashed with the Tasmota firmware: https://tasmota.github.io/docs/

Within Tasmota, set the module as Generic and set up a switch/relay pair. You need to use relays so that we can have multiple POWER entries in MQTT, and have the state retained for when HA restarts.

I set the following commands within the Tasmota console:

Set so that on/off messages are sent via MQTT
switchtopic 1

Sets switches to switchmode 2 (inverted follow mode (0 = ON, 1 = OFF))
switchmode1 2
switchmode2 2
switchmode3 2

Need to set a specific switch MQTT topic which is needed for enabling switchretain on
switchtopic optocoupler

Set so that HA can pick up the switch state after a reboot
switchretain on

The console will show the following when you toggle power on/off

19:32:29 MQT: cmnd/optocoupler/POWER1 = OFF (retained)
19:32:40 MQT: cmnd/optocoupler/POWER2 = ON (retained)
19:32:43 MQT: cmnd/optocoupler/POWER2 = OFF (retained)

My Tasmota home screen looks like this:

You need to set up and configure MQTT as well. In my case Tasmota sends MQTT messages to an MQTT broker that Home Assistant monitors via the sensors below.

Within Home Assistant, I use the following binary sensors:

binary_sensor:
  - platform: mqtt
    name: "heating_sw1"
    state_topic: "cmnd/optocoupler/POWER1"
    payload_on: "ON"
    payload_off: "OFF"

  - platform: mqtt
    name: "heating_sw2"
    state_topic: "cmnd/optocoupler/POWER2"
    payload_on: "ON"
    payload_off: "OFF"

  - platform: mqtt
    name: "heating_sw3"
    state_topic: "cmnd/optocoupler/POWER3"
    payload_on: "ON"
    payload_off: "OFF"

The wiring between the Optocoupler and ESP module is as follows:

VCC - 3V3
GND - GND
OUT1 - GPIO 12
OUT2 - GPIO 13
OUT3 - GPIO 14

Here is a close-up pic:

To create the short wires seen above, I used this crimping set:

I designed and 3D printed the box to house them in seeing as 240v is involved:

Here is the setup I used for testing:

Hopefully that’s covered everything, but I’ll edit and update if needs be.

5 Likes

Nice write-up. So in general terms this doesn’t switch the device(s) , it tells HA when the device(s) are being switched on and off?

Good point! Yep, the Optocoupler just detects when mains power is live or not, and can’t be used to switch the device on/off.

Will that optocoupler work on 120 volt mains? I am in the US and am looking to make something that can detect weather or not 120 is present on a couple of circuits.