Control: Sonoff Basic R2 DIY Smart Plug Software
Sonoff Basic R2 DIY Smart Plug
Software:
Requirements for this guide:
- Home Assistant or OpenHAB server
- MQTT Server on Openhab or Home Assistant
Tools Required:
- Arduino + Male to Male Jumper Cables (Unless bought Pre-flashed)
- Mobile Device Ios or android
- Computer Mac Or Windows, the steps are the same
Links to all tools, parts, and software are in the links tab
If you want to win one go to:
Sonoff Tasmota Settings:
GPIO 14 ->
OPENHAB:
Create item in items file:
execute command in ssh: sudo nano /etc/openhab2/items/home.items
//Sonoff Basic
Switch MKSonoffBasic1 "Christmas Lights 1" <poweroutlet_us> [ "Switchable" ] {mqtt=">[broker:cmnd/sonoffBasic1/POWER:command:*:default],<[broker:stat/sonoffBasic1/POWER1:state:default]", autoupdate="false"}
Switch MKSonoffBasic1LED {mqtt=">[broker:cmnd/sonoffBasic1/POWER2:command:*:default],<[broker:stat/sonoffBasic1/POWER2:state:default]", autoupdate="false"}
Then press control + x then y and enter to save
Show the item in sitemap file:
execute command in ssh: sudo nano /etc/openhab2/sitemaps/home.sitemap
Switch item=MKSonoffBasic1
Then press control + x then y and enter to save
Create rules to control LED and turn on and off at a certain time:
execute command in ssh: sudo nano /etc/openhab2/rules/home.rules
//Sonoff LED Button
rule "Sonoff Basic 1 LED"
when
Item MKSonoffBasic1 changed
then
if(MKSonoffBasic1.state == ON)
{
sendCommand(MKSonoffBasic1LED, ON)
}
else if(MKSonoffBasic1.state == OFF)
{
sendCommand(MKSonoffBasic1LED, OFF)
}
end
//Christmas lights on 5pm
rule "Christmas Lights On"
when
Time cron "0 0 17 1/1 * ? *"
then
sendCommand(MKSonoffBasic1, ON)
end
//Christmas lights off 11pm
rule "Christmas Lights Off"
when
Time cron "0 0 23 1/1 * ? *"
then
sendCommand(MKSonoffBasic1, OFF)
end
Then press control + x then y and enter to save
execute command in ssh to restart openhab: sudo systemctl restart openhab2.service
HOME ASSISTANT:
configuration.yaml file:
switch SonoffBasic1:
- platform: mqtt
name: "Christmas Light 1"
icon: mdi:power-plug
state_topic: "stat/sonoffBasic1/POWER1"
command_topic: "cmnd/sonoffBasic1/POWER"
availability_topic: "tele/sonoffBasic1/LWT"
qos: 1
payload_on: "ON"
payload_off: "OFF"
payload_available: "Online"
payload_not_available: "Offline"
retain: true
switch SonoffBasic1LED:
- platform: mqtt
name: "Christmas Light 1 LED"
state_topic: "stat/sonoffBasic1/POWER2"
command_topic: "cmnd/sonoffBasic1/POWER2"
availability_topic: "tele/sonoffBasic1/LWT"
qos: 1
payload_on: "ON"
payload_off: "OFF"
payload_available: "Online"
payload_not_available: "Offline"
retain: true