alias: ⏳AWTRIX Timer
description: ""
trigger:
  - platform: state
    entity_id:
      - timer.awtrix_timer
    to: active
    id: start
  - platform: state
    entity_id:
      - timer.awtrix_timer
    to: idle
    id: stop
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - start
        sequence:
          - parallel:
              - repeat:
                  sequence:
                    - service: mqtt.publish
                      data:
                        qos: 0
                        retain: false
                        topic: awtrix_a8ba90/custom/timer
                        payload: >
                          {% set duration =
                          as_timedelta(state_attr('timer.awtrix_timer',
                          'duration')).total_seconds() %} {% set remaining =
                          as_datetime(state_attr(trigger.entity_id,
                          'finishes_at')) - now().replace(microsecond=0) %} {%
                          set passed = duration - remaining.total_seconds() %}
                          {% set progress = ((passed/duration) * 100) | round(0)
                          | int %} {{
                            dict(
                                  icon = '42893',
                                  text = remaining | string,
                                  progress = progress,
                                  progressC = '#ff0000',
                                  progressBC = '#000000'
                                )
                          }}
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 1
                        milliseconds: 0
                  while:
                    - condition: state
                      entity_id: timer.awtrix_timer
                      state: active
              - service: mqtt.publish
                data:
                  qos: 0
                  retain: false
                  topic: awtrix_a8ba90/settings
                  payload: "{\"ATRANS\": false}"
              - repeat:
                  count: 1
                  sequence:
                    - service: mqtt.publish
                      data:
                        qos: 0
                        retain: false
                        topic: awtrix_a8ba90/switch
                        payload: "{\"name\":\"timer\"}"
      - conditions:
          - condition: trigger
            id:
              - stop
        sequence:
          - service: mqtt.publish
            data:
              qos: 0
              retain: false
              topic: awtrix_a8ba90/sound
              payload: "{\"sound\":\"short\"}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - parallel:
              - service: mqtt.publish
                data:
                  qos: 0
                  retain: false
                  topic: awtrix_a8ba90/custom/timer
              - service: mqtt.publish
                data:
                  qos: 0
                  retain: false
                  topic: awtrix_a8ba90/settings
                  payload: "{\"ATRANS\": true}"
mode: restart