alias: Battery Charge Session Tracker
description: Track when charging starts and ends, log it, and send a mobile notification.
triggers:
  - entity_id: sensor.victron_system_battery_state
    to: CHARGING
    trigger: state
  - entity_id: sensor.victron_system_battery_state
    from: CHARGING
    trigger: state
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.to_state is defined and trigger.to_state.state ==
              'CHARGING' }}
            alias: If charging has started
        sequence:
          - variables:
              start_ts: "{{ as_timestamp(now()) }}"
              start_soc: "{{ states('sensor.victron_battery_soc') | float }}"
          - data:
              name: Battery
              message: >
                Charge started at {{ start_ts | timestamp_custom('%H:%M') }}
                (SoC {{ start_soc }}%).
            action: logbook.log
          - data:
              notification_id: battery_charge_session_tracker_{{start_ts}}
              title: Battery Charging Started 🪫✅
              message: >
                Charge started at {{ start_ts | timestamp_custom('%H:%M') }}
                (SoC {{ start_soc }}%).
            action: persistent_notification.create
          - data:
              entity_id: input_text.current_battery_charge_session
              value: |
                {{ { 'start': start_ts,
                     'start_soc': start_soc } | to_json }}
            action: input_text.set_value
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.from_state is defined and trigger.from_state.state ==
              'CHARGING' }}
            alias: If charging has finished
        sequence:
          - variables:
              end_ts: "{{ as_timestamp(now()) }}"
              end_soc: "{{ states('sensor.victron_battery_soc') | float }}"
              session: >-
                {{ states('input_text.current_battery_charge_session') |
                from_json }}
              start_ts: "{{ session.start }}"
              start_soc: "{{ session.start_soc }}"
              notification_id: battery_charge_session_tracker_{{start_ts}}
              notification_message: >
                Charged from {{ start_ts | timestamp_custom('%H:%M') }} (SoC {{
                start_soc }}%) to {{ end_ts | timestamp_custom('%H:%M') }} (SoC
                {{ end_soc }}%).
          - choose:
              - conditions:
                  - condition: template
                    value_template: |
                      {{ (end_ts - start_ts) > 300 }}
                sequence:
                  - data:
                      name: Battery
                      message: >
                        Charge ended at {{ end_ts | timestamp_custom('%H:%M') }}
                        (SoC {{ end_soc }}%).
                    action: logbook.log
                  - data:
                      notification_id: "{{ notification_id }}"
                      title: Battery Charging Ended🔋🏁
                      message: "{{ notification_message }}"
                    action: persistent_notification.create
                  - data:
                      title: Battery Charging Ended🔋🏁
                      message: "{{ notification_message }}"
                    action: notify.mobile_app_phil_s24u
                alias: If charging lasted more than 5 minutes
            default:
              - data:
                  notification_id: "{{ notification_id }}"
                action: persistent_notification.dismiss
mode: single
