alias: Awtrix Light Status Display
description: Displays the current state (on/off, brightness, and color) of lights on the Awtrix display
mode: single

trigger:
  - platform: state
    entity_id:
      - light.my_light1
      - light.my_light2
      - light.my_light3
    # Add here the lights you want to monitor

conditions: []

action:
  # Small delay to ensure the light state and attributes are updated
  - delay:
      milliseconds: 500

  # Define variables based on the new light state
  - variables:
      is_light_off: "{{ trigger.to_state.state == 'off' }}"
      rgb_color: "{{ trigger.to_state.attributes.rgb_color | default(None) }}"
      brightness: "{{ trigger.to_state.attributes.brightness | default(None) }}"
      awtrix_color: |-
        {% if is_light_off %}
          444444  # Gray for OFF state
        {% elif rgb_color %}
          {{ '%02x%02x%02x' | format(rgb_color[0], rgb_color[1], rgb_color[2]) }}  # Use RGB color
        {% elif brightness is not none %}
          ffd700  # Yellow for dimmable white lights
        {% else %}
          00ff00  # Green for on/off-only lights
        {% endif %}
      brightness_percent: |-
        {% if is_light_off %}
          0
        {% elif brightness is not none %}
          {{ (brightness / 255 * 100) | int }}
        {% else %}
          100
        {% endif %}
      display_text: |-
        {% if is_light_off %}
          Off
        {% else %}
          {{ brightness_percent }}%
        {% endif %}

  # Send MQTT message to Awtrix display
  - service: mqtt.publish
    data:
      topic: awtrix_543584/notify  # Replace with your Awtrix topic
      payload: |-
        {
          "repeat": 1,
          "duration": 1,
          "icon": "17326",  // Light bulb icon
          "center": true,
          "stack": false,
          "color": "#{{ awtrix_color }}",
          "progressC": "#{{ awtrix_color }}",
          "progressBC": "#666666",
          "progress": {{ brightness_percent }},
          "pushIcon": 2,
          "scrollSpeed": 65,
          "text": "{{ display_text }}"
        }