Skip to content

Instantly share code, notes, and snippets.

@notherealmarco
Last active March 10, 2024 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save notherealmarco/6ce63099a744cf7da3ce3d3f552f9b3d to your computer and use it in GitHub Desktop.
Save notherealmarco/6ce63099a744cf7da3ce3d3f552f9b3d to your computer and use it in GitHub Desktop.
HA Smart Dimmer

Incorporation URL: https://gist.github.com/bstenborg/1731775b26541d607584aea102d53236#file-smart_dimmer-yaml

HA Smart Dimmer: a dimmer for everyone

A Blueprint for Home Assistant that allows every button to dimm every light

Note: Dimming non-Zigbee devices has known issues. ZHA and Zigbee2MQTT lights work perfectly.

This Blueprint creates a script for every group of light you want to control with a button. Then you can use automations (or another blueprint) to map the button to the script. (see below)

It works by providing functions for toggling and dimming the group.

Supported dimming modes

Timestamp: this mode should be used only if the lights are not ZHA devices compatible with the Zigbee Light Link standard. When you call the dimmming function (e.g. you hold down a button), the lights starts dimming to the highest/lowest brightness level with a 3s transition. When the button is released, the a new brightness will be calculated based on the time (in microseconds) the button has been pressed and sent to the lamp.

This mode has a known issue: sometimes the lights jumps to a completely different brightness when the button is released.

ZHA ZLL: this mode is optimized for Zigbee devices, when the button is released, a special Zigbee raw command is sent to the light, which stops the transition and answers with the current brightness. This command is not natively supported by Home Assistant, so I need to send a raw message, that's why I need the IEEE address.

ZHA Group: same as ZHA ZLL but for Zigbee groups (native Zigbee groups managed through the ZHA integration, doesn't work with software defined groups like the light_group component).

Zigbee2MQTT: If using Zigbee2MQTT instead of ZHA. Only works with lights that support the brightness_move command.

Note: You may need to click on "reconfigure device" (ZHA device page) if the light doesn't push back it's current brightness

Configuration

Input Value Notes
Light All the lights you want to turn on/off and dimm At least one entity is needed, do not add only areas or devices
Dimmer helper (input_boolean) An input_boolean helper You can use the same entity for every instance of this blueprint
Dimmer helper (input_text) An input_text helper You can use the same entity for every instance of this blueprint
Dimming mode The algorithm to use, see above Timestamp may be unstable for now, I need to find a way to address the issue
IEEE or Group ID Light's Zigbee IEEE address if you selected "ZHA ZLL light" or "Zigbee2MQTT" as dimming mode, Zigbee group ID if you selected "ZHA Group" (GROUP IDs IN DECIMAL NOTATION) This value doesn't matter if you're using Timestamp

Usage

Once you created an instance of the blueprint, you can start adding automations for your buttons:

If you have a blueprint for your button which support custom actions, use it! Otherwise you can manually create the automations.

When a button is pressed once, add an action, edit the action as yaml and paste:

service: script.<your_script_entity_id>
data:
  action: toggle

Note: <your_script_entity_id> needs to be replaced with the entity_id of the script you previously created with this blueprint.

On the button hold event, add:

service: script.<your_script_entity_id>
data:
  action: dimm_auto

On the button hold release event, add:

service: script.<your_script_entity_id>
data:
  action: stop

If your remote has separate buttons to dimm up / down the light, you can use the dimm_up and dimm_down actions, and the short_up and short_down actions for short presses.

Known working blueprints for remotes

Please, let me know down below if you find any working blueprint for your remote

IKEA E1743: for this remote, you can directly use the all-in-one blueprint: https://gist.github.com/notherealmarco/6c7313e0411341d9dd6be40162dd67a9

(Alternative) IKEA E1743: (the one with two buttons) https://github.com/EPMatt/awesome-ha-blueprints/blob/main/blueprints/controllers/ikea_e1743/ikea_e1743.yaml

IKEA E1524/E1810: (the one with five buttons) https://github.com/EPMatt/awesome-ha-blueprints/blob/main/blueprints/controllers/ikea_e1524_e1810/ikea_e1524_e1810.yaml

Xiaomi WXKG01LM: (the round Mijia one) https://gist.github.com/notherealmarco/9b7218264f40ff934119870d57b2b21b

blueprint:
name: Smart Dimmer
description: A dimmer for everyone!
domain: script
input:
light:
name: Light
description: Select the light entity you wish to control. At least an entity needs to be added (do not use only areas or devices).
selector:
target:
entity:
domain: light
dimmer:
name: Dimmer helper (input_boolean)
description: An input_boolean helper that can be used to store temporary data.
selector:
entity:
domain: input_boolean
cts:
name: Dimmer helper (input_text)
description: An input_text helper that can be used to store temporary data.
selector:
entity:
domain: input_text
l1mode:
name: Light 1 dimming mode
default: "Timestamp"
selector:
select:
options:
- Timestamp
- ZHA ZLL light
- ZHA Group of ZLL lights
- Zigbee2MQTT
l1ieee:
name: Light 1's IEEE or Group ID
default: "0"
selector:
text:
mode: restart
sequence:
- choose:
- conditions:
- condition: template
value_template: '{{ action == "toggle" }}'
- condition: template
value_template: '{{ is_state(main_light_eid, "off") }}'
sequence:
- service: light.turn_on
target: !input light
- conditions:
- condition: template
value_template: '{{ action == "toggle" }}'
- condition: template
value_template: '{{ is_state(main_light_eid, "on") }}'
sequence:
- service: light.turn_off
target: !input light
- conditions:
- condition: template
value_template: '{{ action == "short_up" }}'
sequence:
- service: light.turn_on
target: !input light
data:
brightness_step_pct: 20
- conditions:
- condition: template
value_template: '{{ action == "short_down" }}'
sequence:
- service: light.turn_on
target: !input light
data:
brightness_step_pct: -20
- conditions:
- condition: template
value_template: '{{ action == "stop" }}'
sequence:
- choose:
- conditions:
- condition: template
value_template: '{{ l1mode == "Timestamp" }}'
sequence:
- service: light.turn_on
target: !input light
data:
brightness: '{% set delta = (now().timestamp() - (states(cts_entity).split(" ")[1]|float)) %}{% set start = (((states(cts_entity).split(" ")[2]|int) / 3)|round) %}{% set up = states(dimmer_entity) == "off" %}{{ (((255 - start) / 3) * (delta + 0.2)) + start if up else 240 - (((255 - start) / 3) * (delta + 0.2)) }}'
transition: 0
- conditions:
- condition: template
value_template: '{{ l1mode == "ZHA ZLL light" }}'
sequence:
- service: zha.issue_zigbee_cluster_command
data:
ieee: !input l1ieee
cluster_id: 8
endpoint_id: 1
command: 3
command_type: server
args: []
- conditions:
- condition: template
value_template: '{{ l1mode == "ZHA Group of ZLL lights" }}'
sequence:
- service: zha.issue_zigbee_group_command
data:
group: !input l1ieee
cluster_id: 8
command: 3
args: []
- conditions:
- condition: template
value_template: '{{ l1mode == "Zigbee2MQTT" }}'
sequence:
- service: mqtt.publish
data:
qos: 0
retain: false
payload: "{\"brightness_move\":0}"
topic: '{{"zigbee2mqtt/" + l1ieee + "/set"}}'
default: []
- conditions:
- condition: template
value_template: '{{ action != "dimm_down" and ((is_state(main_light_eid, "off")) or (state_attr(main_light_eid, "brightness") < 200) or action == "dimm_up") }}'
- condition: or
conditions:
- condition: template
value_template: '{{ action == "dimm_up" }}'
- condition: template
value_template: '{{ is_state(main_light_eid, "off") }}'
- condition: template
value_template: '{{ state_attr(main_light_eid, "brightness") < 30 }}'
- condition: state
entity_id: !input dimmer
state: "on"
sequence:
- service: input_boolean.turn_off
target:
entity_id: !input dimmer
- service: input_text.set_value
data:
entity_id: !input cts
value: "1 {{ now().timestamp() }} {{ state_attr(main_light_eid, 'brightness') if states(main_light_eid) == 'on' else 1 }}"
- service: light.turn_on
target: !input light
data:
brightness: 255
transition: 3
- conditions:
- condition: template
value_template: '{{ is_state(main_light_eid, "on") or action == "dimm_down" }}'
- condition: template
value_template: '{{ (state_attr(main_light_eid, "brightness") > 30) or action == "dimm_down" }}'
- condition: or
conditions:
- condition: template
value_template: '{{ action == "dimm_down" }}'
- condition: template
value_template: '{{ state_attr(main_light_eid, "brightness") > 200 }}'
- condition: state
entity_id: !input dimmer
state: "off"
sequence:
- service: input_boolean.turn_on
target:
entity_id: !input dimmer
- service: input_text.set_value
data:
entity_id: !input cts
value: "1 {{ now().timestamp() }} {{ state_attr(main_light_eid, 'brightness') if states(main_light_eid) == 'on' else 1 }}"
- service: light.turn_on
target: !input light
data:
brightness: 1
transition: 3
default: []
variables:
action: "toggle"
main_light: !input light
dimmer_entity: !input dimmer
cts_entity: !input cts
l1mode: !input l1mode
l1ieee: !input l1ieee
main_light_eid: '{% if main_light["entity_id"] is string %}{{ main_light["entity_id"] }}{% else %}{{ main_light["entity_id"][0] }}{% endif %}'
@HansSGitHub
Copy link

Smart Dimmer: A dimmer for everyone

I am super happy with this blueprint!! :slight_smile: , but there is one thing I would like to see (preferably) differently.
The lights do not go fluently from one state to another state (not smooth/no transition time).
So, it’s more like a flickering lights when the lux values changes, especially during the winter and autumn.

Is this something that can be added to this blueprint (not using additional scripts and so on/no added complexity)?

Thank you in advance!!

@bstenborg
Copy link

I made a hacky fork at https://gist.github.com/bstenborg/1731775b26541d607584aea102d53236 to add support for Zigbee2MQTT as well as ZHA. Feel free to upstream or use as-is. 😃

@notherealmarco
Copy link
Author

notherealmarco commented Oct 27, 2023

I made a hacky fork at https://gist.github.com/bstenborg/1731775b26541d607584aea102d53236 to add support for Zigbee2MQTT as well as ZHA. Feel free to upstream or use as-is. 😃

Awesome work! I can't test it as I use ZHA, but I'll definitely merge your changes in the next few days, thanks!

Edit: merge completed, thanks!

@jenseo
Copy link

jenseo commented Feb 15, 2024

@notherealmarco These kind of blueprints have been flaky at best, but this is the best variant of a toggle that I've found, well done! The only thing not working for me is the stop event. It does not seem to stop the dimming progress when releasing the button.

The ZHA event that is fired when releasing after long press is "stop_with_on_off", but I'm not sure what the config should be for the ZHA event response that you are sending from the script. I see that you send a command: 3, not sure if I need to change that to "stop_with_on_off" or something else?

And this is towards a group of lights that I have created inside the ZHA interface

@jenseo
Copy link

jenseo commented Feb 15, 2024

Ok, I have managed to find out that it works if I select a single light and enter its device IEEE, but when I select a ZHA group of lights and add the group id, the stop command is not working

@bstenborg
Copy link

Hm, don't think I can help you there I'm afraid. Fallback, control each light individually?

@jenseo
Copy link

jenseo commented Feb 16, 2024

@bstenborg @notherealmarco I found the solution, I didn't understand I had to convert the hexadecimal id to decimal, it seems at least that this was the issue because now I am able to stop the transition also on the groups.

@notherealmarco
Copy link
Author

notherealmarco commented Feb 16, 2024

@bstenborg @notherealmarco I found the solution, I didn't understand I had to convert the hexadecimal id to decimal, it seems at least that this was the issue because now I am able to stop the transition also on the groups.

I'm glad you found the issue! I made the documentation clearer about it, thanks for reporting it! I'm sorry for the time you wasted 😢

@bstenborg
Copy link

Perfect! Thanks for contributing. 🙂

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