Dimmer script. Pick from value list

Here is my script for dimming a light using predefined values. Using linear steps gave too small steps in the high end, and too low in the low end. My scripts here will use the listed values only. If it starts out as a different number, first click will go to the nearest value.
Works perfect for me.

With my IKEA On/off switch, I use dimmer on the on/off events. 1 click = 1 step. hold-0 to turn off, hold-1 for max brightness. If you like this or not depends on your use case. turn on is fast, as that is just pressing 0 or 1. Turn off is a bit slow this way.

Requires Jinja 2.1 to get namespaces (global vars)


    dimmer_down:
      fields:
        entity_id:
          description: "entity_id to dim"
          example: "light.living_room"
      sequence:
        - service: light.turn_on
          data_template:
              entity_id: "{{ entity_id }}"
              brightness: >
                {% set brightvalues = [ 4, 8, 16, 32, 64, 128, 192, 255 ] %}
                {% set current = (state_attr(entity_id,"brightness")|int) %}
                {% set ns = namespace( result = current ) %}
                {% set ns.found = 0 %}
                {% for br in brightvalues %}
                  {% if (not loop.first) and (not ns.found) and current <= br  %}
                    {% set ns.result = (loop.previtem|int) %}
                    {% set ns.found = 1 %}
                  {% endif %}
                {% endfor %}
                {{ ns.result }}

    dimmer_up:
      fields:
        entity_id:
          description: "entity_id to dim"
          example: "light.living_room"
      sequence:
        - service: light.turn_on
          data_template:
              entity_id: "{{ entity_id }}"
              brightness: >
                {% set brightvalues = [ 4, 8, 16, 32, 64, 128, 192, 255 ] %}
                {% set current = (state_attr(entity_id,"brightness")|int) %}
                {% set ns = namespace( result = current ) %}
                {% set ns.found = 0 %}
                {% for br in brightvalues %}
                  {% if (not loop.last) and (not ns.found) and current <= br  %}
                    {% set ns.result = (loop.nextitem|int) %}
                    {% set ns.found = 1 %}
                {% endif %}
                {% endfor %}
                {{ ns.result }}

3 Likes