blueprint:
  name: AWTRIX now playing
  description: Show the current playing track
  domain: automation
  input:
    media_player:
      name: Media player
      description: Media player what you want to control.
      selector:
        entity:
          filter:
            - domain: media_player
          multiple: false

    awtrix_device:
      name: AWTRIX device
      description: The AWTRIX device for show playing status.
      selector:
        device:
          integration: mqtt
          manufacturer: Blueforcer
          model: AWTRIX Light
          multiple: false

trigger:
  - platform: state
    entity_id: !input media_player

variables:
  awtrix: !input awtrix_device
  awtrix_device_name: "{{ iif( device_attr(awtrix, 'name_by_user') != none, device_attr(awtrix, 'name_by_user'), device_attr(awtrix, 'name') ) }}"

action:
  - variables:
      playing_state: "{{ states(trigger.entity_id) }}"
      title_template: "{{ state_attr(trigger.entity_id, 'media_title') }}"
      album_template: "{{ state_attr(trigger.entity_id, 'media_album_name') }}"
      artist_template: "{{ state_attr(trigger.entity_id, 'media_artist') }}"
  - if:
      - condition: template
        value_template: "{{ playing_state != \"playing\"  }}"
    then:
      - service: awtrix.push_app_data
        data:
          device: "{{awtrix_device_name}}"
          name: sonos
          data: {}
    else:
      - service: awtrix.push_app_data
        data:
          device: "{{awtrix_device_name}}"
          name: sonos
          data:
            text: "{{ playing_state }}: {{ artist_template }} - {{ title_template }}"
            rainbow: true
            duration: 10
            icon: >-
              /9j/4AAQSkZJRgABAgEASABIAAD/4QDKRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAEAAAEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAAZgAAAAAAAABIAAAAAQAAAEgAAAABAAeQAAAHAAAABDAyMjGRAQAHAAAABAECAwCgAAAHAAAABDAxMDCgAQADAAAAAQABAACgAgAEAAAAAQAAAAigAwAEAAAAAQAAAAikBgADAAAAAQAAAAAAAAAAAAD/wAARCAAIAAgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhM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