## Connected ##
- sensor:
    - name: "PMG Lucy - Connected"
      state: |-
        {%- set sensor = 'sensor.lucys_mi_9_bluetooth_connection' %}
        {%- set paired = state_attr(sensor,'connected_paired_devices') %}
        {%- set not_paired = state_attr(sensor,'connected_not_paired_devices') %}
        {%- set devices = flatten([paired,not_paired]) %}
        {{ devices | regex_match('.*Pokemon GO Plus.*') }}
      icon: "{{iif(is_state('sensor.pmg_lucy_connected','true'),'mdi:bluetooth-connected','mdi:bluetooth-off')}}"
      unique_id: "25e91621-3a53-440b-8a1f-f4b28072a01e"

    - name: "PMG Nessie - Connected"
      state: |-
        {%- set sensor = 'sensor.lucy_s_phone_bluetooth_connection' %}
        {%- set paired = state_attr(sensor,'connected_paired_devices') %}
        {%- set not_paired = state_attr(sensor,'connected_not_paired_devices') %}
        {%- set devices = flatten([paired,not_paired]) %}
        {{ devices | regex_match('.*Pokemon GO Plus.*') }}
      icon: "{{iif(is_state('sensor.pmg_nessie_connected','true'),'mdi:bluetooth-connected','mdi:bluetooth-off')}}"
      unique_id: "fbfac025-e02e-4f64-9bdb-7124ab9df90b"
## Live Duration ##
- sensor:
    - name: "PMG Lucy - Live Duration"
      state: |-
        {% set sensor = 'sensor.pmg_current_lucy' %}
        {% set start = state_attr(sensor,'Start') %}
        {% if start is number %}
          {{ (now().timestamp() - start) | round(0) | int }}
        {% else %}
          0
        {% endif %}
      icon: "mdi:clock"
      unique_id: "0c88d146-08df-4cca-a36e-e8fa8a066425"
      attributes:
        pretty: |-
          {% set sensor = 'sensor.pmg_current_lucy' %}
          {% set start = state_attr(sensor,'Start') %}
          {% if start is number %}
            {% set dur = (now().timestamp() - start) | int %}
            {% set h = (dur // 3600) %}
            {% set m = ((dur % 3600) // 60) %}
            {% set s = (dur % 60) %}
            {% if h > 0 %}
              {{ '%02d:%02d' % (h, m) }} h
            {% elif m > 0 %}
              {{ '%02d:%02d' % (m, s) }} min.
            {% else %}
              {{ '%02d' % (s) }} s
            {% endif %}                  
          {% else %}
            "00:00"
          {% endif %}

    - name: "PMG Nessie - Live Duration"
      state: |-
        {%- set sensor = 'sensor.pmg_current_nessie' %}
        {% set start = state_attr(sensor,'Start') %}
        {% if start is number %}
          {{ (now().timestamp() - start) | round(0) | int }}
        {% else %}
          0
        {% endif %}
      icon: "mdi:clock"
      unique_id: "f3a355cc-6122-41e1-9fe4-01af15364df9"
      attributes:
        pretty: |-
          {% set sensor = 'sensor.pmg_current_nessie' %}
          {% set start = state_attr(sensor,'Start') %}
          {% if start is number %}
            {% set dur = (now().timestamp() - start) | int %}
            {% set h = (dur // 3600) %}
            {% set m = ((dur % 3600) // 60) %}
            {% set s = (dur % 60) %}
            {% if h > 0 %}
              {{ '%02d:%02d' % (h, m) }} h
            {% elif m > 0 %}
              {{ '%02d:%02d' % (m, s) }} min.
            {% else %}
              {{ '%02d' % (s) }} s
            {% endif %}                  
          {% else %}
            "00:00"
          {% endif %}
## Pause Duration ##
- sensor:
    - name: "PMG Lucy - Paused Duration"
      unique_id: "2a301e09-faa7-4dd6-b33a-76329d0fd0ce"
      icon: mdi:pause-octagon
      state: >
        {% set start = state_attr('sensor.pmg_current_lucy','Start') %}
        {% set pause_start = state_attr('sensor.pmg_current_lucy','PauseStart') %}
        {% if pause_start and pause_start > start %}
          {{ (now().timestamp() - pause_start) | int }}
        {% else %}
          0
        {% endif %}

    - name: "PMG Nessie - Paused Duration"
      unique_id: "63c76170-fa14-4e19-b4b0-7eeb930ef640"
      icon: mdi:pause-octagon
      state: >
        {% set start = state_attr('sensor.pmg_current_nessie','Start') %}
        {% set pause_start = state_attr('sensor.pmg_current_nessie','PauseStart') %}
        {% if pause_start and pause_start > start %}
          {{ (now().timestamp() - pause_start) | int }}
        {% else %}
          0
        {% endif %}
## Total Stats ##
- sensor:
    - name: "PMG Total Stats - Lucy"
      unique_id: "6eca9c9a-6bcf-4fa2-a84f-6b411e175c49"
      attributes:
        Sessions: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count }}"
        Duration: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum }}"
        AvgDuration: "{{ ((state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum) / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count)) | round(0) if (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count) > 0 else 0 }}"
        Catched: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Catched | sum }}"
        AvgCatched: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Catched | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count) > 0 else 0 }}"
        Escaped: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Escaped | sum }}"
        AvgEscaped: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Escaped | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count) > 0 else 0 }}"
        Items: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Items | sum }}"
        AvgItems: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Items | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count) > 0 else 0 }}"
        Spins: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session').Spins | sum }}"
        AvgSpins: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Spins | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Id | count) > 0 else 0 }}"
        CatchRate: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Catched | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum / 60)) | round(2) if state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum > 0 else 0 }}"
        SpinRate: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Spins | sum / (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum / 60)) | round(2) if state_attr('binary_sensor.pmg_stats_lucy', 'Session').Duration | sum > 0 else 0 }}"
        CatchRatio: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Catched | sum / state_attr('binary_sensor.pmg_stats_lucy', 'Session').Escaped | sum) | round(2) if state_attr('binary_sensor.pmg_stats_lucy', 'Session').Escaped | sum > 0 else state_attr('binary_sensor.pmg_stats_lucy', 'Session').Catched | sum }}"
        ItemRatio: "{{ (state_attr('binary_sensor.pmg_stats_lucy', 'Session').Items | sum / state_attr('binary_sensor.pmg_stats_lucy', 'Session').Spins | sum) | round(2) if state_attr('binary_sensor.pmg_stats_lucy', 'Session').Spins | sum > 0 else 0 }}"
      state: "1"
      icon: "mdi:calculator"
    - name: "PMG Total Stats - Nessie"
      unique_id: "232796b8-a92a-44c6-b72d-89504ceb2a26"
      attributes:
        Sessions: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count }}"
        Duration: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum }}"
        AvgDuration: "{{ ((state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum) / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count)) | round(0) if (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count) > 0 else 0 }}"
        Catched: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Catched | sum }}"
        AvgCatched: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Catched | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count) > 0 else 0 }}"
        Escaped: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Escaped | sum }}"
        AvgEscaped: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Escaped | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count) > 0 else 0 }}"
        Items: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Items | sum }}"
        AvgItems: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Items | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count) > 0 else 0 }}"
        Spins: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session').Spins | sum }}"
        AvgSpins: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Spins | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count)) | round(1) if (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Id | count) > 0 else 0 }}"
        CatchRate: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Catched | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum / 60)) | round(2) if state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum > 0 else 0 }}"
        SpinRate: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Spins | sum / (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum / 60)) | round(2) if state_attr('binary_sensor.pmg_stats_nessie', 'Session').Duration | sum > 0 else 0 }}"
        CatchRatio: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Catched | sum / state_attr('binary_sensor.pmg_stats_nessie', 'Session').Escaped | sum) | round(2) if state_attr('binary_sensor.pmg_stats_nessie', 'Session').Escaped | sum > 0 else state_attr('binary_sensor.pmg_stats_nessie', 'Session').Catched | sum }}"
        ItemRatio: "{{ (state_attr('binary_sensor.pmg_stats_nessie', 'Session').Items | sum / state_attr('binary_sensor.pmg_stats_nessie', 'Session').Spins | sum) | round(2) if state_attr('binary_sensor.pmg_stats_nessie', 'Session').Spins | sum > 0 else 0 }}"
      state: "1"
      icon: "mdi:calculator"
- select:
    - name: "PMG Select Session - Nessie"
      optimistic: true
      unique_id: "85a0d989-1ed5-4a54-94f5-721efb5fc0b9"
      icon: mdi:alpha-n-box
      options: |-
        [
          {% set sess = state_attr('binary_sensor.pmg_stats_nessie', 'Session') %}
          {% for session_id in range((sess.Start | count)-1,-1,-1) %}
            {% set start = sess.Start[session_id] | float(default=0) %}
            {% set end = sess.End[session_id] | float(default=0) %}
            {% set dur = (end - start) | int %}
            {% set h = (dur // 3600) %}
            {% set m = ((dur % 3600) // 60) %}
            {% set timespan=(now().timestamp()-start) %}
            {% if (timespan / 86400) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 86400) | round(0)}}d ago)"
            {% elif (timespan / 3600) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 3600) | round(0)}}h ago)"
            {% elif (timespan / 60) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 60) | round(0)}}min ago)"
            {% else %}
              "{{'{:02d.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{timespan | round(0)}}s ago)" 
            {% endif %}
                {{ ',' if not loop.last }}
          {% endfor %}
        ]
      select_option:
        - variables:
            index: |-
              {% set options = state_attr('select.pmg_session_nessie','options') %}
              {% set opt = option %}
              {% set result = namespace(value = -1) %}
              {% for i in range(options | count,-1,-1) %}
                {% if options[i] == opt %}
                  {% set result.value = (state_attr('binary_sensor.pmg_stats_nessie','Session').Start | count) - 1 - i %}
                {% endif %}
              {% endfor %}
              {{ result.value }}
            session: "{{ state_attr('binary_sensor.pmg_stats_nessie', 'Session') }}"
        - action: variable.update_sensor
          data:
            attributes:
              CatchRate: "{{ session.CatchRate[index] | float }}"
              CatchRatio: "{{ session.CatchRatio[index] | float }}"
              Catched: "{{ session.Catched[index] | int }}"
              Duration: >-
                {% set dur = session.Duration[index|int] | int(default=0) %} {{ (dur //
                3600)|string }}:{{ ((dur % 3600) // 60)|string }}
              End: >-
                {{ session.End[index] | float | timestamp_custom('%Y-%m-%d %H:%M') if
                index < (session.End | count) else 'Running…' }}
              Escaped: "{{ session.Escaped[index] | int }}"
              ItemRatio: "{{ session.ItemRatio[index] | float }}"
              Items: "{{ session.Items[index] | int }}"
              SpinRate: "{{ session.SpinRate[index] | float }}"
              Spins: "{{ session.Spins[index] | int }}"
              Start: "{{ session.Start[index] | float | timestamp_custom('%Y-%m-%d %H:%M') }}"
            replace_attributes: true
            value: "{{ index }}"
          metadata: {}
          target:
            entity_id: sensor.pmg_session_nessie
      state: |-
        {% set session = state_attr('binary_sensor.pmg_stats_nessie', 'Session') %}
        {% set index = states('sensor.pmg_session_nessie') | int %}
        {% set reversed_index = (session.Start | count) - 1 - (index|int) %}
        {{ state_attr('select.pmg_session_nessie','options')[reversed_index] }}
    - name: "PMG Select Session - Lucy"
      unique_id: "5c86720e-a81f-4d8e-97b8-3f02047a4bc8"
      icon: mdi:alpha-l-box
      optimistic: true
      options: |-
        [
          {% set sess = state_attr('binary_sensor.pmg_stats_lucy', 'Session') %}
          {% for session_id in range((sess.Start | count)-1,-1,-1) %}
            {% set start = sess.Start[session_id] | float(default=0) %}
            {% set end = sess.End[session_id] | float(default=0) %}
            {% set dur = (end - start) | int %}
            {% set h = (dur // 3600) %}
            {% set m = ((dur % 3600) // 60) %}
            {% set timespan=(now().timestamp()-start) %}
            {% if (timespan / 86400) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 86400) | round(0)}}d ago)"
            {% elif (timespan / 3600) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 3600) | round(0)}}h ago)"
            {% elif (timespan / 60) >= 1 %}
              "{{'{:02d}.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{(timespan / 60) | round(0)}}min ago)"
            {% else %}
              "{{'{:02d.'.format((session_id+1))}}⏱️: {{ '{:02d}:{:02d}'.format(h, m) }} (📅 {{timespan | round(0)}}s ago)" 
            {% endif %}
                {{ ',' if not loop.last }}
          {% endfor %}
        ]
      select_option:
        - variables:
            index: |-
              {% set options = state_attr('select.pmg_session_lucy','options') %}
              {% set opt = option %}
              {% set result = namespace(value = -1) %}
              {% for i in range(options | count,-1,-1) %}
                {% if options[i] == opt %}
                  {% set result.value = (state_attr('binary_sensor.pmg_stats_lucy','Session').Start | count) - 1 - i %}
                {% endif %}
              {% endfor %}
              {{ result.value }}
            session: "{{ state_attr('binary_sensor.pmg_stats_lucy', 'Session') }}"
        - action: variable.update_sensor
          data:
            attributes:
              CatchRate: "{{ session.CatchRate[index] | float }}"
              CatchRatio: "{{ session.CatchRatio[index] | float }}"
              Catched: "{{ session.Catched[index] | int }}"
              Duration: >-
                {% set dur = session.Duration[index|int] | int(default=0) %} {{ (dur //
                3600)|string }}:{{ ((dur % 3600) // 60)|string }}
              End: >-
                {{ session.End[index] | float | timestamp_custom('%Y-%m-%d %H:%M') if
                index < (session.End | count) else 'Running…' }}
              Escaped: "{{ session.Escaped[index] | int }}"
              ItemRatio: "{{ session.ItemRatio[index] | float }}"
              Items: "{{ session.Items[index] | int }}"
              SpinRate: "{{ session.SpinRate[index] | float }}"
              Spins: "{{ session.Spins[index] | int }}"
              Start: "{{ session.Start[index] | float | timestamp_custom('%Y-%m-%d %H:%M') }}"
            replace_attributes: true
            value: "{{ index }}"
          metadata: {}
          target:
            entity_id: sensor.pmg_session_lucy
      state: |-
        {% set session = state_attr('binary_sensor.pmg_stats_lucy', 'Session') %}
        {% set index = states('sensor.pmg_session_lucy') | int %}
        {% set reversed_index = (session.Start | count) - 1 - (index|int) %}
        {{ state_attr('select.pmg_session_lucy','options')[reversed_index] }}
