Skip to content

Instantly share code, notes, and snippets.

@agdamsbo
Last active June 5, 2023 05:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agdamsbo/819c27a5b986c38327d4b19057cbfb6d to your computer and use it in GitHub Desktop.
Save agdamsbo/819c27a5b986c38327d4b19057cbfb6d to your computer and use it in GitHub Desktop.
Configuration example of ESPhome configuration for Shelly 1 switch with multi click functionality.
substitutions:
devicename: ude-syd-shelly1-3
channel_1: Hoveddør og parkering
ip: 192.168.1.107
ssid: !secret wifi_iot_ssid
wifi_pass: !secret wifi_iot_password
password: !secret esphome_api_password
dns1: 1.1.1.1
dns2: 1.0.0.1
# NOTES
# This is highly inspired by others work and is easily used with the linked blueprint in Home Assistant.
# Blueprint: https://gist.github.com/agdamsbo/f49277bd8dd9bbada055c49600bea9e2
# INSPIRATION
# ESPhome decoupled approach: https://www.esphome-devices.com/devices/Shelly-1/
# ESPhome text_sensor approach: https://gist.github.com/albertnis/f33066b0b3623a9839a3429c8f19f4d4
# Blueprint: https://community.home-assistant.io/t/trigger-different-actions-on-a-single-double-or-double-click-on-a-binary-sensor/255902/15
esphome:
name: ${devicename}
platform: ESP8266
board: esp01_1m
wifi:
ssid: ${ssid}
password: ${wifi_pass}
manual_ip:
static_ip: ${ip}
gateway: 192.168.1.1
subnet: 255.255.255.0
dns1: ${dns1}
dns2: ${dns2}
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: ${devicename}
password: ${password}
manual_ip:
static_ip: 192.168.100.2
gateway: 192.168.100.1
subnet: 255.255.255.0
dns1: ${dns1}
dns2: ${dns2}
captive_portal:
# Enable logging
logger:
level: DEBUG
# Enable Home Assistant API
api:
password: ${password}
ota:
password: ${password}
web_server:
port: 80
# Text sensors with general information.
text_sensor:
# Expose ESPHome version as sensor.
- platform: version
name: ${devicename} ESPHome Version
# Expose WiFi information as sensors.
- platform: wifi_info
ip_address:
name: ${devicename} IP
- platform: template
name: ${devicename} Uptime Readable
id: uptime_human
icon: mdi:clock-start
# Multi-click input handling
- platform: template
name: "ude-syd-kontakt"
id: ude_syd_kontakt
icon: "mdi:toggle-switch"
on_value:
then:
- if:
condition:
text_sensor.state:
id: ude_syd_kontakt
state: ""
else:
- delay: 200ms
- text_sensor.template.publish:
id: ude_syd_kontakt
state: !lambda 'return "";'
# Sensors with general information.
sensor:
# Uptime sensor.
- platform: uptime
name: ${devicename} Uptime
id: uptime_sensor
update_interval: 60s
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(uptime_sensor).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
# WiFi Signal sensor.
- platform: wifi_signal
name: ${devicename} WiFi Signal
update_interval: 300s
# Shelly 1 detached switch config with multi click options and fallback in case of wifi or api fail
# Changed to include light, from https://www.esphome-devices.com/devices/Shelly-1/
# Event trigger not working, using text_sensor example from https://gist.github.com/albertnis/f33066b0b3623a9839a3429c8f19f4d4
output:
- platform: gpio
pin: GPIO4
id: shelly_relay
light:
- platform: binary
name: "${channel_1} light"
output: shelly_relay
id: lightid
restore_mode: RESTORE_DEFAULT_OFF
binary_sensor:
- platform: gpio
pin:
number: GPIO5
# mode: INPUT_PULLUP
name: "${devicename} button"
internal: true
id: button
# config for multi click actions
on_multi_click:
# single click - always toggle relay
- timing:
- ON for at most 400ms
- OFF for at least 300ms
then:
- logger.log: "${devicename} single click"
- text_sensor.template.publish:
id: ude_syd_kontakt
state: !lambda 'return "single";'
- light.toggle: lightid
# double click
- timing:
- ON for at most 400ms
- OFF for at most 300ms
- ON for at most 400ms
- OFF for at least 300ms
then:
- if:
condition:
and:
- wifi.connected:
- api.connected:
# double click in case wifi and api are conncected
then:
- logger.log: "${devicename} double click"
- text_sensor.template.publish:
id: ude_syd_kontakt
state: !lambda 'return "double";'
# toggle relay in case either wifi or api are not connected
else:
- light.toggle: lightid
# triple click
- timing:
- ON for at most 400ms
- OFF for at most 300ms
- ON for at most 400ms
- OFF for at most 300ms
- ON for at most 400ms
- OFF for at least 50ms
then:
- if:
condition:
and:
- wifi.connected:
- api.connected:
# triple click in case wifi and api are conncected
then:
- logger.log: "${devicename} triple click"
- text_sensor.template.publish:
id: ude_syd_kontakt
state: !lambda 'return "triple";'
# toggle relay in case either wifi or api are not connected
else:
- light.toggle: lightid
# hold
- timing:
- ON for at least 2s
then:
- if:
condition:
and:
- wifi.connected:
- api.connected:
# hold in case wifi and api are conncected
then:
- logger.log: "${devicename} long click"
- text_sensor.template.publish:
id: ude_syd_kontakt
state: !lambda 'return "hold";'
# toggle relay in case either wifi or api are not connected
else:
- light.toggle: lightid
@mnestor
Copy link

mnestor commented Jun 4, 2023

Is this for the Shelly Button 1 or the Shelly Relay 1?

@agdamsbo
Copy link
Author

agdamsbo commented Jun 5, 2023

Is this for the Shelly Button 1 or the Shelly Relay 1?

This is the relay, Shelly 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment