Skip to content

Instantly share code, notes, and snippets.

@thomasmaxson
Last active September 7, 2025 16:49
Show Gist options
  • Select an option

  • Save thomasmaxson/1caba2eb5bd9bb0bb1aed157b5bfb15c to your computer and use it in GitHub Desktop.

Select an option

Save thomasmaxson/1caba2eb5bd9bb0bb1aed157b5bfb15c to your computer and use it in GitHub Desktop.
Home Assistant Blueprint to control your devices with an Sonoff Wireless Switch using ZHA.
# This blueprint uses an SONOFF Wireless Switch Remote Control connected through ZHA or Z2MQTT
# This blueprint allows you to control lights and run custom actions with the remote's buttons
#
# | Button Type | Interaction | Action Choices |
# | ------------- | ------------ | ----------------------- |
# | Main Button | Single Press | - Toggle light(s) state |
# | | | - Run custom action |
# | ------------- | ------------ | ----------------------- |
# | Main Button | Double Press | - Toggle light(s) state |
# | | | - Run custom action |
# | ------------- | ------------ | ----------------------- |
# | Main Button | Press & Hold | - Run custom action |
# | ------------- | ------------ | ----------------------- |
blueprint:
name: SONOFF, Wireless Switch
description: Control devices with your SONOFF Wireless Switch.
author: Thomas Maxson
source_url: https://gist.github.com/thomasmaxson/1caba2eb5bd9bb0bb1aed157b5bfb15c
homeassistant:
min_version: 2024.6.0
domain: automation
input:
remote:
name: SONOFF, Wireless Switch
description: SONOFF Wireless Switch remote used to trigger actions.
selector:
device:
multiple: false
filter:
- integration: mqtt
manufacturer: SONOFF
model: Wireless button
- integration: zha
manufacturer: eWeLink
model: WB01
button_single_press_config:
name: Single Press Configuration
icon: mdi:gesture-tap
collapsed: true
input:
event_button_press_single:
name: Single Press Event Type
description: >-
Select the event you want the **Button** to do.
<br>&bull; _Toggle light_: Toggle the power state of the light(s)
<br>&bull; _Custom Action_: Trigger your own custom action
default: "Toggle light"
selector:
select:
options:
- Toggle light
- Custom action
light_press_single:
name: Single Press Light(s)
description: Select the light entities you wish to control.
default: []
selector:
entity:
multiple: true
filter:
domain: light
action_button_press_single:
name: Single Button Press Event
description: The action to perform on _single press_ of the **Button**.
default: []
selector:
action: null
button_double_press_config:
name: Double Press Configuration
icon: mdi:gesture-double-tap
collapsed: true
input:
event_button_press_double:
name: Double Press Event Type
description: >-
Select the event you want the **Button** to do.
<br>&bull; _Toggle light_: Toggle the power state of the light(s)
<br>&bull; _Custom Action_: Trigger your own custom action
default: "Toggle light"
selector:
select:
options:
- Toggle light
- Custom action
light_press_double:
name: Double Press Light(s)
description: Select the light entities you wish to control.
default: []
selector:
entity:
multiple: true
filter:
domain: light
action_button_press_double:
name: Double Press Button Event
description: The action to perform on _double press_ of the **Button**.
default: []
selector:
action: null
button_other_events:
name: Other Button Events
icon: mdi:gesture-tap
collapsed: true
input:
action_button_press_hold:
name: Button Press & Hold Event
description: The action to perform on _press and hold_ of the **Button**.
default: []
selector:
action:
automation_config:
name: Configuration
icon: mdi:cog
collapsed: true
input:
config_mode:
name: Automation Mode
description: Mode that automation runs in.
default: restart
selector:
select:
options:
- single
- restart
- queued
- parallel
custom_value: false
multiple: false
config_max_num:
name: Mode Max
description: >
Maximum number of runs that can be executed or queued at a time.
<br>Ignored by _Single_ and _Restart_ Modes.
default: 10
selector:
number:
mode: slider
unit_of_measurement: runs
min: 1.0
max: 15.0
step: 1.0
mode: !input config_mode
max: !input config_max_num
max_exceeded: silent
variables:
var_remote: !input remote
var_light_press_single: !input light_press_single
var_light_press_double: !input light_press_double
var_event_button_press_single: !input event_button_press_single
var_event_button_press_double: !input event_button_press_double
var_action_button_press_hold: !input action_button_press_hold
triggers:
- alias: Button Press Once (via ZHA event)
trigger: event
event_type: zha_event
event_data:
device_id: !input remote
command: "toggle"
id: trigger_press_single_zha
- alias: Button Press Once (via MQTT event)
trigger: device
device_id: !input remote
domain: mqtt
type: action
subtype: single
id: trigger_press_single_mqtt
- alias: Button Press Twice (via ZHA event)
trigger: event
event_type: zha_event
event_data:
device_id: !input remote
command: "on"
id: trigger_press_double_zha
- alias: Button Press Twice (via MQTT event)
trigger: device
device_id: !input remote
domain: mqtt
type: action
subtype: double
id: trigger_press_double_mqtt
- alias: Button Press and Hold (via ZHA event)
trigger: event
event_type: zha_event
event_data:
device_id: !input remote
command: "off"
id: trigger_press_hold_zha
- alias: Button Press and Hold (via MQTT event)
trigger: device
device_id: !input remote
domain: mqtt
type: action
subtype: long
id: trigger_press_hold_mqtt
conditions:
- condition: template
value_template: "{{ var_remote | count > 0 }}"
actions:
- variables:
var_all_lights_on_single: >-
{{ expand( var_light_press_single ) | selectattr( "state", "eq", "on" ) | map( attribute = "entity_id" ) | unique | list }}
var_all_lights_off_single: >-
{{ expand( var_light_press_single ) | selectattr( "state", "eq", "off" ) | map( attribute = "entity_id" ) | unique | list }}
var_first_light_on_single: >-
{{ [] if ( var_light_press_single | length <= 0 ) else expand( var_light_press_single ) | selectattr( "state", "eq", "on" ) | map( attribute = "entity_id" ) | unique | list | first }}
var_all_lights_on_double: >-
{{ expand( var_light_press_double ) | selectattr( "state", "eq", "on" ) | map( attribute = "entity_id" ) | unique | list }}
var_all_lights_off_double: >-
{{ expand( var_light_press_double ) | selectattr( "state", "eq", "off" ) | map( attribute = "entity_id" ) | unique | list }}
var_first_light_on_double: >-
{{ [] if ( var_light_press_double | length <= 0 ) else expand( var_light_press_double ) | selectattr( "state", "eq", "on" ) | map( attribute = "entity_id" ) | unique | list | first }}
- choose:
- conditions:
- condition: trigger
id:
- trigger_press_single_zha
- trigger_press_single_mqtt
sequence:
- alias: "Determine event action to trigger"
choose:
- alias: "For Single button press, toggle light state"
conditions:
- alias: "Check if correct action choice is selected"
condition: and
conditions:
- alias: "Check if should toggle light(s)"
condition: template
value_template: "{{ var_event_button_press_single == 'Toggle light' }}"
- alias: "Check if light(s) to be controlled are set"
condition: template
value_template: "{{ var_light_press_single | length > 0 }}"
sequence:
- alias: "Determine weather to turn the light(s) on or off"
if:
- alias: "Check if the light(s) are currently off"
condition: template
value_template: "{{ var_all_lights_on_single | length == 0 }}"
then:
- alias: "Turn on light(s)"
action: light.turn_on
target:
entity_id: "{{ var_all_lights_off_single }}"
else:
- alias: "Turn off light(s)"
action: light.turn_off
target:
entity_id: "{{ var_all_lights_on_single }}"
- alias: "For Single press, run custom action"
conditions:
- alias: "Check if correct action choice is selected"
condition: template
value_template: "{{ var_event_button_press_single == 'Custom action' }}"
sequence: !input action_button_press_single
- conditions:
- condition: trigger
id:
- trigger_press_double_zha
- trigger_press_double_mqtt
sequence:
- alias: "Determine event action to trigger"
choose:
- alias: "For Double button press, manage light on/off state"
conditions:
- alias: "Check if correct action choice is selected"
condition: and
conditions:
- alias: "Check if should toggle light(s)"
condition: template
value_template: "{{ var_event_button_press_double == 'Toggle light' }}"
- alias: "Check if light(s) to be controlled are set"
condition: template
value_template: "{{ var_light_press_double | length > 0 }}"
sequence:
- alias: "Determine weather to turn the light(s) on or off"
if:
- alias: "Check if the light(s) are currently off"
condition: template
value_template: "{{ var_all_lights_on_double | length == 0 }}"
then:
- alias: "Turn on light(s)"
action: light.turn_on
target:
entity_id: "{{ var_all_lights_off_double }}"
else:
- alias: "Turn off light(s)"
action: light.turn_off
target:
entity_id: "{{ var_all_lights_on_double }}"
- alias: "For Double press, run custom action"
conditions:
- alias: "Check if correct action choice is selected"
condition: template
value_template: "{{ var_event_button_press_double == 'Custom action' }}"
sequence: !input action_button_press_double
- conditions:
- condition: trigger
id:
- trigger_hold_zha
- trigger_hold_mqtt
sequence: !input action_button_press_hold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment