#:########################################################################################:#
# Low Battery Monitoring Package                                                         #
#:########################################################################################:#
#
# TITLE:
#   Low Battery Monitoring
#
# FILE:
#   packages/low_battery_device_quantity.yaml
#
# VERSION:
#   V1.2 2026-07-23
#
# VERSION HISTORY:
#   V1.3 2026-07-24
#     - Truncated list sensor state to count-only summary; full list stays in attributes.
#
#   V1.2 2026-07-23
#     - Moved the weekly low-battery blueprint automation into this package.
#     - Preserved the Monday 21:00 schedule and Zorruno Pushover notification.
#
#   V1.1 2026-07-20
#     - Changed the low-battery threshold from 97% to 20%.
#     - Added structured documentation for scope, exclusions, and outputs.
#
#   V1.0
#     - Initial low-battery count and list sensors.
#
# PURPOSE:
#   Finds numeric sensor entities with device_class battery whose value is 20%
#   or lower, exposes count and list sensors, and runs a weekly notification.
#
# SCOPE AND EXCLUSIONS:
#   - Only entities in the sensor domain with device_class battery are checked.
#   - Unknown, unavailable, non-numeric, and "Ok" states are excluded.
#   - Battery entities in other domains are not included in the summaries.
#   - The notification blueprint also checks battery binary sensors.
#
# OUTPUTS:
#   - sensor.devices_with_low_battery reports the number of matching sensors.
#   - sensor.devices_with_low_battery_list reports names and percentages.
#   - The list sensor also exposes count, entities, and names attributes.
#   - A Pushover notification runs each Monday at 21:00 when batteries are low.
#
#:########################################################################################:#

template:
  - sensor:
      - name: "Devices with low battery"
        unique_id: devices_with_low_battery_count
        unit_of_measurement: "devices"
        state: >
          {% set ns = namespace(c=0) %}
          {% for s in states.sensor %}
            {% if s.attributes.device_class == 'battery' %}
              {% set st = s.state %}
              {% if st not in ['unknown', 'unavailable', 'Ok'] %}
                {% if st | int(-1) != -1 and (st | int(0)) <= 20 %}
                  {% set ns.c = ns.c + 1 %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ ns.c }}
        icon: >
          {% if this.state | int(0) == 0 %}
            mdi:check-circle
          {% else %}
            mdi:battery-alert
          {% endif %}

      - name: "Devices with low battery list"
        unique_id: devices_with_low_battery_list
        icon: >
          {% if states('sensor.devices_with_low_battery') | int(0) == 0 %}
            mdi:check-circle
          {% else %}
            mdi:battery-alert
          {% endif %}
        state: >
          {% set count = states('sensor.devices_with_low_battery') | int(0) %}
          {% if count == 0 %}
            None
          {% else %}
            {{ count }} devices
          {% endif %}
        attributes:
          count: "{{ states('sensor.devices_with_low_battery') | int(0) }}"
          entities: >
            {% set ns = namespace(e=[]) %}
            {% for s in states.sensor %}
              {% if s.attributes.device_class == 'battery' %}
                {% set st = s.state %}
                {% if st not in ['unknown', 'unavailable', 'Ok'] %}
                  {% if st | int(-1) != -1 and (st | int(0)) <= 20 %}
                    {% set ns.e = ns.e + [s.entity_id] %}
                  {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {{ ns.e }}
          names: >
            {% set ns = namespace(n=[]) %}
            {% for s in states.sensor %}
              {% if s.attributes.device_class == 'battery' %}
                {% set st = s.state %}
                {% if st not in ['unknown', 'unavailable', 'Ok'] %}
                  {% if st | int(-1) != -1 and (st | int(0)) <= 20 %}
                    {% set ns.n = ns.n + [s.name] %}
                  {% endif %}
                {% endif %}
              {% endif %}
            {% endfor %}
            {{ ns.n }}

automation:
  - id: "1635929281159"
    alias: "Weekly Low battery level detection"
    description: >
      Checks battery-class entities each Monday and notifies Zorruno when any
      are below the blueprint threshold.
    use_blueprint:
      path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
      input:
        day: 1
        time: "21:00:00"
        actions:
          - action: notify.pushover_zorruno
            data:
              title: "View Road Sensors"
              message: >
                Weekly Battery Check. Low Devices:
                {{ sensors }}
