Aqara wall switch to control lights

Tags: #<Tag:0x00007f3f16916818> #<Tag:0x00007f3f169166d8> #<Tag:0x00007f3f16916570>

I’ve made a script I want to share, to control the lights using a wall switch.
The switch is a aqara zigbee wall switch that is connected with Zigbee2MQTT. The lights are an Ikea Tradfri bulb (also Zigbee2MQTT) and a light with a sonoff. The button can detect long presses and double presses, this allows to control brightness and color of the light with a single button, but only if the light supports it.

Automation:

- id: '1552853418090'
  alias: SlaapkamerLampknop
  trigger:
  - entity_id: sensor.slaapkamerlampknop
    platform: state
  condition: []
  action:
  - data_template:
      button: '{{ trigger.to_state.state}}'
      entity: '{{ trigger.to_state.entity_id}}' 
    service: python_script.slaapkamerlampknop

python script:

#compare color
def compare_color(color1,color2):
    return (-5 <= (color1[0]-color2[0]) <= 5 and -5 <= (color1[1]-color2[1]) <= 5 and -5 <= (color1[2]-color2[2]) <= 5)
    
#get state and entity_id of button from automation
buttonState = data.get('button')
buttonEntity = data.get('entity')

#get target light based on trigger device and button
if buttonEntity == 'sensor.slaapkamerlampknop' and buttonState.startswith('left'):
    entity_id = 'light.myfirstbulb'
elif buttonEntity == 'sensor.slaapkamerlampknop' and buttonState.startswith('right'):
    entity_id = 'light.nachtkastlamp'
else:
    entity_id = ''
    
if entity_id != '':
    #find out which features are supported by the target light
    light = hass.states.get(entity_id)
    features = light.attributes.get('supported_features')
    if buttonState.endswith('long') and features % 2 >= 1:
        #change brightness on long button press
        service = 'turn_on'
        brightness = light.attributes.get('brightness') or 0
        if brightness < 150:
            service_data = {'entity_id': entity_id, 'brightness': 200}
        else:
            service_data = {'entity_id': entity_id, 'brightness': 100}
            
    elif buttonState.endswith('double') and features % 32 >= 16:
        #change color on double press
        service = 'turn_on'
        rgb = light.attributes.get('rgb_color') or [0,0,0]
        colors = {
          "white":[255,254,254],
          "yellow":[255,255,128],
          "orange":[255,128,0],
          "red":[255,0,0]
          }
        if compare_color(rgb,colors["white"]):
            service_data = {'entity_id': entity_id, 'rgb_color': colors["yellow"]}
        elif compare_color(rgb,colors["yellow"]):
            service_data = {'entity_id': entity_id, 'rgb_color': colors["orange"]}
        elif compare_color(rgb,colors["orange"]):
            service_data = {'entity_id': entity_id, 'rgb_color': colors["red"]}
        elif compare_color(rgb,colors["red"]):
            service_data = {'entity_id': entity_id, 'rgb_color': colors["white"]}
        else:
            service_data = {'entity_id': entity_id, 'rgb_color': colors["yellow"]}
    else:
        service = 'toggle'
        service_data = {'entity_id': entity_id}
      
    hass.services.call('light', service, service_data, False)
1 Like