[Automation] Zigbee2mqtt and Hue Dimmer Switch

Hi there, I could not find an flexible way to use my hue dimmer switch with zigbee2mqtt so I smashed this automation together. I hope somebody finds it useful. I would appreciate proposals to enhance the automation :slight_smile: - its not optimized at all - but it works.

Also: how do you handle your switch routines?

- alias: Hue Remote Switch 1 
  trigger:
    - platform: mqtt
      topic: zigbee2mqtt/HueDimmerS1
  condition:
      condition: and
      conditions: 
        - condition: template 
          value_template: '{{ trigger.payload_json.action|length > 0 }}'
        - condition: template
          value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.hue_remote_switch_1', 'last_triggered') | default(0)) | int > 1)}}"      
  action:
#possible states
# on |up |down| off
# on-press
# on-hold
# on-hold-release    
    - service_template: >
          {% set button = trigger.payload_json.action %}
          {% if "hold" in button %}
          {% set holding = true %}
          {% else %}
          {% set holding = false %}
          {% endif %}
          {% if "release" in button %}
          {% set released = true %}
          {% else %}
          {% set released = false %}
          {% endif %}                    
          {% if "on" in button %}
          {% set button = 1 %}
          {% elif "up" in button %}
          {% set button = 2 %}
          {% elif "down" in button %}
          {% set button = 3 %}
          {% elif "off" in button %}
          {% set button = 4 %}
          {% else%}
          {% set button = 5 %}
          {% endif %}
          {% set duration = trigger.payload_json.duration|float %}    

          {% if button == 1 and not holding %}
            media_player.media_play_pause                 
          {% elif button == 2  and not holding %}
            media_player.volume_up
          {% elif button == 3  and not holding %}
            media_player.volume_down           
          {% elif button == 4  and not holding %}
            media_player.media_next_track 
          {% elif button == 1  and holding and released %}
            input_select.select_next
          {% elif button == 2  and holding and released %}
            media_player.media_previous_track           
          {% elif button == 3  and holding and released %}
            input_select.select_previous
          {% elif button == 4  and holding and released %}
            homeassistant.turn_off
          {% else %}
          {% endif %}                
      data_template:
        entity_id: >  
          {% set button = trigger.payload_json.action %}
          {% if "hold" in button %}
          {% set holding = true %}
          {% else %}
          {% set holding = false %}
          {% endif %}
          {% if "release" in button %}
          {% set released = true %}
          {% else %}
          {% set released = false %}
          {% endif %}          
          {% if "on" in button %}
          {% set button = 1 %}
          {% elif "up" in button %}
          {% set button = 2 %}
          {% elif "down" in button %}
          {% set button = 3 %}
          {% elif "off" in button %}
          {% set button = 4 %}
          {% else%}
          {% set button = 5 %}
          {% endif %}
          {% set duration = trigger.payload_json.duration|float %}    
          {% if button == 1 and not holding %}
            media_player.spotify
          {% elif button == 2 and not holding %}
            media_player.spotify              
          {% elif button == 3 and not holding %} 
            media_player.spotify
          {% elif button == 4 and not holding %} 
            media_player.spotify
          {% elif button == 1 and holding and released %}
            input_select.atmo             
          {% elif button == 2 and holding and released %}
            media_player.spotify
          {% elif button == 3 and holding and released %}
            input_select.atmo               
          {% elif button == 4 and holding and released %}
            group.all_lights                    
          {% else %}     
          {% endif %}
1 Like