Skip to content

Instantly share code, notes, and snippets.

@Didgeridrew
Created February 25, 2021 18:28
Show Gist options
  • Save Didgeridrew/866d31e2c70e90326a90ed86f3c6e922 to your computer and use it in GitHub Desktop.
Save Didgeridrew/866d31e2c70e90326a90ed86f3c6e922 to your computer and use it in GitHub Desktop.
Sonoff Mini Smart switch w/ Smart bulbs ESPHOME & Home Assistant
# This is a branch of Brian Hanifin's project (https://brianhanifin.com/posts/esphome-shelly1-dumb-light-switch-smart/)
# adapted for Sonoff Mini R2 smart switches.
substitutions:
device_name: smart_switch_for_smart_bulbs
project: Sonoff Mini R2
button1_gpio: GPIO00
switch1_gpio: GPIO04
relay1_gpio: GPIO12
hass_light: light.bedroom_overhead
device_ip:
gateway_ip:
subnet_ip:
esphome:
name: ${device_name}
platform: ESP8266
board: esp01_1m
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: ${device_ip}
gateway: ${gateway_ip}
subnet: ${subnet_ip}
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Sonoff Mini Basic Config"
password: !secret ap_password
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 30min
password: !secret ha_api_password
ota:
password: !secret ha_ota_password
time:
- platform: homeassistant
sensor:
- platform: wifi_signal
name: ${device_name} Wifi Signal Strength
update_interval: 60s
- platform: uptime
name: ${device_name} Uptime
script:
- id: hass_light_toggle
then:
if:
condition:
api.connected:
then:
- if:
condition:
switch.is_off: relay_1
then:
# Turn the relay back on and turn on the light.
- switch.turn_on: relay_1
- homeassistant.service:
service: light.turn_on
data:
entity_id: $hass_light
brightness: "254"
else:
# Have Home Assistant toggle the light.
- homeassistant.service:
service: light.toggle
data:
entity_id: $hass_light
else:
# When HA is unavailable, toggle the relay.
- switch.toggle: relay_1
#######################################
# Device specific Config Begins Below #
#######################################
binary_sensor:
- platform: gpio
pin: $switch1_gpio
id: reset
internal: true
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_state:
- script.execute: hass_light_toggle
switch:
- platform: gpio
name: ${device_name}_switch
icon: "mdi: lightbulb_outline"
pin: $relay1_gpio
restore_mode: ALWAYS_ON
id: relay_1
status_led:
pin:
number: GPIO13
inverted: true
output:
- platform: esp8266_pwm
id: onboard_blue_led
pin: GPIO13
inverted: True
light:
# the 4 lines below define the Blue LED light on Sonoff Mini, to expose in HomeAssistant remove line "internal: true"
- platform: monochromatic
name: ${device_name}_blueled
output: onboard_blue_led
internal: true # hides the Blue LED from HomeAssistant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment