Binary Number on a Group of Lights

Above my bathroom mirror I have ten light bulbs, and it got me thinking to use them as a binary display.

The script below takes a number as an input, and displays it in binary on the group light.light_group. It is set to work with bulbs in ascending order from right to left, but removing “reverse” from the lights variable will accommodate a descending order.

When calling it, pass a number:

action: script.binary_number_display
data:
  number: 42

If you were to have a light timer, this action would display the current elapsed time in seconds:

action: script.binary_number_display
data:
  number: >-
    {{ ((state_attr('timer.light_timer', 'finishes_at') | as_datetime |
    as_local - now()).total_seconds() | int ) }}

It is quite niche, but that’s the point of it, isn’t it? :smiley:

alias: Binary Number Display
description: Binary Number Display on Light Group
fields:
  number:
    description: Number to display
sequence:
  - variables:
      lights: >-
        {{ expand('light.light_group') | map(attribute='entity_id') | sort |
        reverse | list }}
      bin: "{{ '{:0{}b}'.format(number, lights | length ) | list }}"
  - repeat:
      for_each: "{{ lights }}"
      sequence:
        - variables:
            bit: "{{ bin[repeat.index - 1] }}"
            entity: "{{ repeat.item }}"
        - choose:
            - conditions:
                - condition: template
                  value_template: "{{ bit == 1 }}"
              sequence:
                - target:
                    entity_id: "{{ entity }}"
                  action: light.turn_on
                  data:
                    transition: 1
            - conditions:
                - condition: template
                  value_template: "{{ bit == 0 }}"
              sequence:
                - target:
                    entity_id: "{{ entity }}"
                  action: light.turn_off
2 Likes