Cookbook for Sonoff Light Switches (#407)

* Cookbook for Sonoff Light Switches

Cookbook for various Sonoff Light Switch options including Basic, Dual and T1-3

* Removed references to myself

All references to I have been removed and re worded into a more generic text

* Fixed Formatting issues to Pass Travis Checks

* Update cookbook/sonoff-basic-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* Update cookbook/sonoff-dual-light-switch.rst

Co-Authored-By: Otto Winter <otto@otto-winter.com>

* YAML updates

Removed fast_connect, UART swotches from Dual R1, and reference to an issue that no longer exists
This commit is contained in:
Ben Suffolk 2019-11-29 17:27:26 +00:00 committed by Otto Winter
parent ad16db3789
commit b3d811d5a3
7 changed files with 690 additions and 0 deletions

View File

@ -0,0 +1,170 @@
DIY Light switch using a Sonoff Basic
=====================================
.. seo::
:description: An example of how to integrate a light switch into Home Assistant using ESPHome
:image: sonoff_light_switch.png
:keywords: Relay, Sonoff Basic, Sonoff Dual Dual R1, Light, HASS, Home Assistant, ESPHome
.. note::
This is a DIY solution, and you will need to have some knowledge of electrical wiring and enough
capabilities to do this work safely.
The author, and the ESPHome team, take no responsibility for any actions, injuries or outcomes
from following this guide.
In some countries you may need specific qualifications before you can carry out such work in
a residentaial property.
Background
----------
Moving your entire house to smart lighting can end up being very expense, for instance if you have a
light fitting with 5 lamps in it thats 5 expensive smart bulbs to buy just for one room! Smart bulbs
clearly have some great advantages, dimmable, colour temperature or even full colour changing. What
if all you're after is a cost effective way to turn them on and off?
The ideal solution would be to reaplce the light switch with one that can be controlled by home
assistant, whilst retaining the ease of use of a standard light that would also continue to work if
the network went down, or home assistant failed etc.
It turns out Sonoff do exactly this product, it called a T1-UK (other country options available),
however you soon found the touch aspect of them might not 'feel right' and certainly might not get the
approval of other members of your household. However if this solution apeals to you, check out the
:doc:`Cookbook guide for T1/T2/T3</cookbook/sonoff-t1-3>`
The other option to consider is a standard '2 way' light switch (like you might have on an upstairs
/ downstairs control in your house) but using relays for the second switch. For many this will have
the disadvantage that if you remotely changed the light status the switch is then 'upside down'.
The Solution
------------
Use a 'retractive' style light switch. That is one that is spring loaded and so always returns to the
'off' position. It's effectively a push button, that looks like a light switch. Combining this with a
Sonoff Basic gives you the ideal solution for somewhere around £5. Thats much cheaper than buying lots
of expensive smart bulbs, but of course you only get on / off control.
You will have several potential hurdles to overcome:
1. The first is that you do need to have a neutral at the location of the Sonoff. Many houses will not have
this as standard at the light switch.
2. You need enough space to accomodate your Sonoff Basic at your chosen location.
If you're lucky most of the locations will have plasterboard walls, and you will be able to simply drop a neutral
wire down the inside of the wall to the lights switch (where you locate the Sonoff).
Also, if you have plasterboard walls, most of the light switch back boxes are the plastic dry lining style boxes.
This can come in useful because if you remove the PCB from the plastic case it actually fits in the back box nicely.
.. warning::
If you plan to take the pcb out of the plastic case you need to make sure its properly insulated, and that the back
box is deep enough to hold the PCB as well as the switch. You should also insulate the PCB, for instance by dropping it
in a heavy duty glue lined heat shrink sleeve.
**The PCB has mains electricity flowing through it, if you are in any doubt about your capabilities do not attempt to do
this.**
If you have a light switch in a brick wall, an option might be to place the Sonoff Basic above the light in the ceiling void,
and use the cable that ran from the light to the switch as a low voltage cable to connect the Sonoff GPIO.
.. warning::
If you are going to reuse existing wiring to connect to the GPIO, you must make sure its connected directly to the switch
and does not have mains voltage on it from another circuit.
Implementation
--------------
So having established the pitfalls that you need to overcome lets crack on with making this work.
Please make sure you have read up about :doc:`the Sonoff Basic and how to flash it with ESPHome </devices/sonoff_basic>`.
As that won't be covered here.Also make sure you know your way around a soldering iron and can find the relevant information
about the location of the GPIO pins on the Sonoff Basic if you need to.
If you have a Sonoff Basic V1 devices GPOI14 is alredy presented on a pin header on the PCB next to the programing pins.
On the V2 and V3 PCBs, there is a solder pad underneath the PCB that will let you get at this GPIO.
You have 2 choices when it comes to picking which GPIO to use. GPIO0 or GPIO14. GPIO0 is used by the push button switch on the
the PCB so you will need to locate the right pin on the switch and solder a wire onto it if you're going to use that one. Whichever
one you pick, you will also need to use the ground or 0v pin for the other side of the switch. Once you have soldered your wires
into palce, a handy tip is to add a drop of glue over the wire, a little way away from the solder joint, so give some strain relief
to the joint.
Now you have a pair of wires from the GPIO and 0v to your retractive switch lets look at the code.
.. code-block:: yaml
esphome:
name: my_ls
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
logger:
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: True
id: button_1
on_press:
then:
- light.toggle: light_1
- platform: status
name: "My LS Status"
output:
- platform: gpio
pin: GPIO12
id: relay_1
light:
- platform: binary
name: "My Light"
id: light_1
output: relay_1
status_led:
pin:
number: GPIO13
inverted: yes
In the above code block, there is a *secrets.yaml* file so that you have just one place to change wifi
details for all your devices.
Although not visible day to day, there is also the status LED configured so that it can be used when setting
up / debugging. Also a configured binary sensor to give status incase you want to perform an action / alert
if the light switch disconnects for any reason.
.. note::
If you wante to use a pull cord switch (in a bathroom for instance) that works like a standard switch and
changes state each pull (as opposed to a retractive switch that you press and let go) then you can change
a single line *on_press:* to *on_state:* which will trigger the light toggle everytime the state of the
switch changes.
If you do this its important that you do not use GPIO0, otherwise if the device reboots and the switch happens
to be in the closed state the Sonoff will boot into flash mode and not work.
See Also
--------
- :doc:`/cookbook/sonoff-light-switch`
- :doc:`/guides/automations`
- :doc:`/devices/sonoff_basic`

View File

@ -0,0 +1,251 @@
DIY Light switch using a Sonoff Dual
====================================
.. seo::
:description: An example of how to integrate a dual light switch into Home Assistant using ESPHome
:image: sonoff_light_switch.png
:keywords: Relay, Sonoff Dual Dual R1, Light, HASS, Home Assistant, ESPHome
.. note::
This is a DIY solution, and you will need to have some knowledge of electrical wiring and enough
capabilities to do this work safely.
The author, and the ESPHome team, take no responsibility for any actions, injuries or outcomes
from following this guide.
In some countries you may need specific qualifications before you can carry out such work in
a residentaial property.
Please read up on :doc:`/cookbook/sonoff-basic-light-switch` to get the background and principals of
the this project. Its all basically the same, but with a double switch.
So we will be using GPIO4 and GPIO14 for the two retractive switches, again they will both short to 0v
when the switch is clicked.
R1
--
The R1 version of the Dual controls the relays via the UART, so the code gets a bit complex here.
.. code-block:: yaml
esphome:
name: dual_ls
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
logger:
baud_rate: 0
# Enable Home Assistant API
api:
ota:
uart:
tx_pin: GPIO01
rx_pin: GPIO03
baud_rate: 19200
switch:
- platform: template
id: relay_1
turn_on_action:
if:
condition:
switch.is_off: relay_2
then:
- uart.write: [0xA0, 0x04, 0x01, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x03, 0xA1]
turn_off_action:
if:
condition:
switch.is_off: relay_2
then:
- uart.write: [0xA0, 0x04, 0x00, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x02, 0xA1]
optimistic: true
- platform: template
id: relay_2
turn_on_action:
if:
condition:
switch.is_off: relay_1
then:
- uart.write: [0xA0, 0x04, 0x02, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x03, 0xA1]
turn_off_action:
if:
condition:
switch.is_off: relay_1
then:
- uart.write: [0xA0, 0x04, 0x00, 0xA1]
else:
- uart.write: [0xA0, 0x04, 0x01, 0xA1]
optimistic: true
binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
inverted: True
id: button_1
on_press:
then:
- light.toggle: light_1
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: True
id: button_2
on_press:
then:
- light.toggle: light_2
- platform: status
name: "Dual LS Status"
status_led:
pin:
number: GPIO13
inverted: yes
output:
- platform: template
type: binary
id: out_1
write_action:
if:
condition:
light.is_on: light_1
then:
- switch.turn_on: relay_1
else:
- switch.turn_off: relay_1
- platform: template
type: binary
id: out_2
write_action:
if:
condition:
light.is_on: light_2
then:
- switch.turn_on: relay_2
else:
- switch.turn_off: relay_2
light:
- platform: binary
name: "Dual L1"
id: light_1
output: out_1
- platform: binary
name: "Dual L2"
id: light_2
output: out_2
In the above code block, there is a *secrets.yaml* file so that you have just one place to change wifi
details for all your devices.
The logger baud_rate: 0 is required to make sure the logged does not send any data over the UART or it would
mess with the relays.
Although not visible day to day, there is also the status LED configured so that it can be used when setting
up / debugging. Also a configured binary sensor to give status incase you want to perform an action / alert
if the light switch disconnects for any reason.
R2
--
This one is a lot simpler as it uses real GPIO for its relays. Please note this is untested, but should work!
Its basically the same as the :doc:`T2 </cookbook/sonoff-t1-3>`
.. code-block:: yaml
esphome:
name: dual_ls
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
logger:
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_1
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_2
- platform: status
name: "Dual LS Status"
output:
- platform: gpio
pin: GPIO12
id: relay_1
- platform: gpio
pin: GPIO5
id: relay_2
light:
- platform: binary
name: "Dual L1"
id: light_1
output: relay_1
- platform: binary
name: "Dual L2"
id: light_2
output: relay_2
status_led:
pin:
number: GPIO13
inverted: yes
See Also
--------
- :doc:`/cookbook/sonoff-light-switch`
- :doc:`/guides/automations`
- :doc:`/devices/sonoff_basic`

View File

@ -0,0 +1,24 @@
Sonoff Light switch options
===========================
.. seo::
:description: A series of options on using Sonoff devices as a light switch within ESPHome
:image: sonoff_light_switch.jpg
:keywords: Relay, Sonoff Basic, Sonoff Dual Dual R1, Light, HASS, Home Assistant, ESPHome
There are a number of options for using Sonoff Devices as light switches, including purpose built ones
like the T1/2/3 Series
.. imgtable::
Sonoff T1/T2/T3, cookbook/sonoff-T1-3, sonoff_1t_t3.png
Sonoff Basic, cookbook/sonoff-basic-light-switch, sonoff_basic.jpg
Sonoff Dual, cookbook/sonoff-dual-light-switch, sonoff_dual_r2.jpg
See Also
--------
- :doc:`/guides/automations`
- :doc:`/devices/sonoff_basic`

244
cookbook/sonoff-t1-3.rst Normal file
View File

@ -0,0 +1,244 @@
Sonoff T1/T2/T3 UK
==================
.. seo::
:description: An example of how to integrate a T1 T2 or T3 Sonoff light switch into Home Assistant
using ESPHome
:image: sonoff_1t_t3.png
:keywords: Relay, Sonoff Basic, Sonoff Dual Dual R1, Light, HASS, Home Assistant, ESPHome
Please make sure you have read up about :doc:`the Sonoff T1 / T2 / T3 and how to flash it with ESPHome </devices/sonoff_t1_uk_3gang_v1.1>`.
So lets get straight on with the code
T1
--
.. code-block:: yaml
esphome:
name: my_t1
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
use_address: xxx.xxx.xxx.xxx
logger:
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_1
- platform: status
name: "T1 Status"
output:
- platform: gpio
pin: GPIO12
id: relay_1
light:
- platform: binary
name: "T1"
id: light_1
output: relay_1
status_led:
pin:
number: GPIO13
inverted: yes
In the above code block, there is a *secrets.yaml* file so that you have just one place to change wifi
details for all your devices.
The use_address is required because the sonoff T series don't work with mDNS properly. This means that it will
show as off line in the dashboard, and you will need to use the ip address to view the logs or upload new versions
of the firmware. You will also need to manually add the device in integrations by IP address. You will need to
assign a fixed IP in the above configuratino, or use a fixed IP assigned fro your DHCP server.
See `issue #810 <https://github.com/esphome/issues/issues/810>`__ for further details.
T2
--
.. code-block:: yaml
esphome:
name: my_t2
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
use_address: xxx.xxx.xxx.xxx
logger:
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_1
- platform: gpio
pin:
number: GPIO9
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_2
- platform: status
name: "T2 Status"
output:
- platform: gpio
pin: GPIO12
id: relay_1
- platform: gpio
pin: GPIO5
id: relay_2
light:
- platform: binary
name: "T2 L1"
id: light_1
output: relay_1
- platform: binary
name: "T2 L2"
id: light_2
output: relay_2
status_led:
pin:
number: GPIO13
inverted: yes
T3
--
.. code-block:: yaml
esphome:
name: my_t3
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
use_address: xxx.xxx.xxx.xxx
logger:
api:
ota:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_1
- platform: gpio
pin:
number: GPIO9
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_2
- platform: gpio
pin:
number: GPIO10
mode: INPUT_PULLUP
inverted: True
id: button
on_press:
then:
- light.toggle: light_3
- platform: status
name: "T3 Status"
output:
- platform: gpio
pin: GPIO12
id: relay_1
- platform: gpio
pin: GPIO5
id: relay_2
- platform: gpio
pin: GPIO4
id: relay_3
light:
- platform: binary
name: "T3 L1"
id: light_1
output: relay_1
- platform: binary
name: "T3 L2"
id: light_2
output: relay_2
- platform: binary
name: "T3 L3"
id: light_3
output: relay_3
status_led:
pin:
number: GPIO13
inverted: yes
See Also
--------
- :doc:`/cookbook/sonoff-light-switch`
- :doc:`/guides/automations`
- :doc:`/devices/sonoff_t1_uk_3gang_v1.1`

BIN
images/light_switch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

BIN
images/sonoff_1t_t3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -356,6 +356,7 @@ Cookbook
Brilliant / Mirabella Genio Smart Plugs, cookbook/brilliant-mirabella-genio-smart-plugs, cookbook-brilliant-mirabella-genio-smart-plugs.jpg
Zemismart RGBW Downlights, cookbook/zemismart-rgbw-downlights, cookbook-zemismart-rgbw-downlight.jpg
Teckin SB50, cookbook/teckin_sb50, teckin_sb50.jpg
Sonoff light switch options, cookbook/sonoff-light-switch, light_switch.png
Do you have other awesome automations or cool setups? Please feel free to add them to the
documentation for others to copy. See :doc:`Contributing </guides/contributing>`.