blueprint:
  name: "⚽️ AWTRIX - UEFA EURO2024 Scoreboard ⚽️"
  author: Hank_the_Tank
  description: '# UEFA EURO2024 Scoreboard for AWTRIX 3



    ## Screenshots

    ![](https://raw.githubusercontent.com/fettesb/Homeassistant_blueprints/main/AWTRIX%20-%20UEFA%20EURO2024%20Scoreboard/awtrix_uefa_euro_germany.png)
        




    ## Prerequisites

    **Before** using this blueprint, ensure that you have **HACS** installed. You
    also need the ha-teamtracker integration [here](https://github.com/vasqued2/ha-teamtracker).


    ## Credits
    
    to Ben from Discord for Code optimization.
    
    to Blueforcer for Awtrix 3

    If you want to support his hard work in developing Awtrix 3, please consider
    to buy your Ulanzi clock with his affiliate links

    

    [EU] [Ulanzi Pixel Smart Uhr](https://www.ulanzi.de/products/ulanzi-pixel-smart-uhr-2882?ref=28e02dxl)

    [US] [Ulanzi Pixel Smart Clock](https://www.ulanzi.com/products/ulanzi-pixel-smart-clock-2882?ref=28e02dxl)


    If you already have hundreds of clocks, there are more ways to support him.

    [Github Support Page](https://github.com/Blueforcer/awtrix3#support)

    wanna join the [Darkside/Discord](https://discord.gg/a3n8spsn)?

    ## Get the Awtrix Companion App for iOS/Android


    <a href=''https://play.google.com/store/apps/details?id=de.awtrix.light&pcampaignid=pcampaignidMKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1''>
    <img style="width: 20%; height: auto;" alt=''Get it on Google Play'' src=''https://raw.githubusercontent.com/fettesb/Homeassistant_blueprints/main/playstore_button.png''/>
    </a> <a href=''https://apps.apple.com/de/app/awtrix-light/id6459478110''> <img
    style="width: 20%; height: auto;" alt=''Get it on Google Play'' src=''https://raw.githubusercontent.com/fettesb/Homeassistant_blueprints/main/appstore_button.png''/>
    </a>




    **Thanks in advance**

    '
  domain: automation

  input:
    awtrix:
      name: AWTRIX Device
      description: Select the Awtrix 3
      selector:
        device:
          integration: mqtt
          manufacturer: Blueforcer
          model: AWTRIX 3
          multiple: true

    appname:
      name: Awtrix Application name
      description: >-
        This is the app name listed in the MQTT topic - it should be unique
      default: "uefa_euro"
      selector:
        text:

    message_duration:
      name: Message Duration ⏱️
      description: >-
        How long should the message remain on the screen (in seconds).
      selector:
        number:
          min: 0
          max: 300
          unit_of_measurement: "sec"
      default: 15

    my_sensor:
      name: Soccer Sensor
      description: Select your Team Sensor
      selector:
        entity:
          filter:
            - integration: teamtracker



variables:
  device_ids: !input awtrix
  devices: >-
    {% macro get_device(device_id) %}
      {{ states((device_entities(device_id) | select('search','device_topic') | list)[0] | default('unknown')) }}
    {% endmacro %}
    {% set ns = namespace(devices=[]) %}
    {% for device_id in device_ids %}
      {% set device=get_device(device_id)|replace(' ','')|replace('\n','') %}
      {% set ns.devices = ns.devices + [ device ] %}
    {% endfor %}
    {{ ns.devices | reject('match','unavailable|unknown') | list }}
  appname: !input appname
  my_sensor: !input my_sensor
  message_duration: !input message_duration

  homegame: >-
    {% if state_attr(my_sensor, 'team_homeaway') == 'home' -%} True {% else -%} False {% endif %}

  color_home_team_1: "{{ state_attr(my_sensor, 'team_colors')[0]  if homegame else state_attr(my_sensor, 'opponent_colors')[0]  }}"
  color_home_team_2: "{{ state_attr(my_sensor, 'team_colors')[1]  if homegame else state_attr(my_sensor, 'opponent_colors')[1]  }}"
  color_away_team_1: "{{ state_attr(my_sensor, 'opponent_colors')[0]  if homegame else state_attr(my_sensor, 'team_colors')[0]  }}"
  color_away_team_2: "{{ state_attr(my_sensor, 'opponent_colors')[1]  if homegame else state_attr(my_sensor, 'team_colors')[1]  }}"
  home_team_score: "{{ state_attr(my_sensor, 'team_score') if homegame else state_attr(my_sensor, 'opponent_score') }}"
  away_team_score: "{{ state_attr(my_sensor, 'opponent_score') if homegame else state_attr(my_sensor, 'team_score') }}"

  payload_switch: >-
    {
      "name" : "{{ appname }}"
    }
  scoreBoard: >-
    {
      "text": "{{ home_team_score }}:{{ away_team_score }}",
      "draw":[ 
        {"df":[0,1,8,2,"{{ color_home_team_1 }}"]},
        {"df":[1,3,6,5,"{{ color_home_team_1 }}"]},
        {"dl":[3,0,4,0,"{{ color_home_team_1 }}"]},
        {"dl":[2,2,2,7,"{{ color_home_team_2 }}"]},
        {"dl":[5,2,5,7,"{{ color_home_team_2 }}"]},
        {"df":[24,1,8,2,"{{ color_away_team_1 }}"]},
        {"df":[25,3,6,5,"{{ color_away_team_1 }}"]},
        {"dl":[27,0,28,0,"{{ color_away_team_1 }}"]},
        {"dl":[26,2,26,7,"{{ color_away_team_2 }}"]},
        {"dl":[29,2,29,7,"{{ color_away_team_2 }}"]}    
      ], 
      "duration": {{ message_duration }},
      "background": "#006c28"
    }

trigger:
  - platform: state
    entity_id: !input my_sensor
    to: IN
    id: gameStart
  - platform: state
    entity_id: !input my_sensor
    to: 
      - PRE
      - POST
      - NOT_FOUND
    id: noGame    
  - platform: state
    entity_id: !input my_sensor
    attribute: team_score
    id: scoreChangeHome
  - platform: state
    entity_id: !input my_sensor
    attribute: opponent_score
    id: scoreChangeOpponent

condition: []
action:
  - repeat:
      for_each: "{{ devices }}"
      sequence:
        - choose:
            - conditions:
                - condition: trigger
                  id:
                    - gameStart
                    - scoreChangeHome
                    - scoreChangeOpponent
                - condition: state
                  entity_id: !input my_sensor
                  state: IN
              sequence:
                - service: mqtt.publish
                  data:
                    qos: 0
                    retain: false
                    topic: "{{ repeat.item }}/custom/{{ appname }}"
                    payload: "{{ scoreBoard }}"
                - service: mqtt.publish
                  data:
                    qos: 0
                    retain: false
                    topic: "{{ repeat.item }}/switch"
                    payload: "{{ payload_switch }}"
            - conditions:
                - condition: trigger
                  id:
                    - noGame
              sequence:
                - service: mqtt.publish
                  data:
                    qos: 0
                    retain: false
                    topic: "{{ repeat.item }}/custom/{{ appname }}"
mode: restart