###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   Batteries
#   @description    :   Status about various baterries (iphones, sensors...etc)
###############################################################################
homeassistant:

###############################################################################
#                _                        _   _
#     /\        | |                      | | (_)
#    /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#   / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#  / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
# /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
#
###############################################################################

automation:
  ###############################################################################
  # Automation: Notify of iPhone Low Battery
  ###############################################################################
  - alias: Notify Low battery
    initial_state: true
    trigger:
      platform: numeric_state
      entity_id:
        - device_tracker.life360_suresh
        - device_tracker.life360_mallika
        - device_tracker.life360_srinika
        - device_tracker.life360_hasika
      value_template: "{{ state.attributes.battery_level }}"
      below: 25
    condition:
      - condition: template
        value_template: "{{ states('input_boolean.battery_alerts') == 'on' }}"
    action:
      - service: script.notify_family
        data_template:
          message: "{{ trigger.to_state.attributes.friendly_name | title }}'s phone battery is : {{ trigger.to_state.attributes.battery }}%."
          notify_options:
            - telegram
            - led
            - voice
            - tv

  - alias: Alert Low Battery of Sensors
    trigger:
      platform: time
      at: "10:10:10"
    condition:
      - condition: template
        value_template: >
          {% set ns = namespace(lowBattery=false) %}
          {%- for x in states if "battery_level" in x["attributes"] and x.attributes.battery_level |int <= 25 %}
            {% set ns.lowBattery = true %}{% endfor %}
          {{ ns.lowBattery }}
      - condition: template
        value_template: "{{ states('input_boolean.battery_alerts') == 'on' }}"
    action:
      - service: script.notify_family
        data_template:
          message: >
            Sensors with low batteries (<25):
            {% set ns = namespace(lowBattery="") -%}
            {%- for x in states if "battery_level" in x["attributes"] and x.attributes["battery_level"] |int <= 25 -%}
              {%- set ns.lowBattery = ns.lowBattery + x.name + "- " + x.attributes["battery_level"]|round|string + "\n" -%}
            {%- endfor -%}
            {{- ns.lowBattery -}}
          notify_options:
            - telegram
            - tv
            - led
