# By Darius Jula 
# dariusjula.ro
# https://www.youtube.com/@DariusJula
alias: "Deye: Management Baterie Unificat"
description: >-
  Gestionează limita de curent de încărcare cu priorități clare: Siguranță >
  Reset > Protecție 90% > Export 50% > Încărcare normală.
triggers:
  - entity_id: sensor.deye_inverter_battery
    trigger: state
  - at: "06:00:00"
    trigger: time
  - at: "15:00:00"
    trigger: time
  - entity_id: input_boolean.battery_peak_charge
    trigger: state
  - entity_id: input_boolean.battery_charge_limit
    trigger: state
  - entity_id: binary_sensor.deye_inverter_grid
    to: "off"
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "off"
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Mod OFF-GRID"
                  message: Reteaua a cazut! Incarcare setata la 100A pentru siguranta.
                action: notify.darius_all_devices
      - conditions:
          - condition: template
            value_template: |-
              {{ is_state('input_boolean.battery_peak_charge', 'off') or
                 is_state('input_boolean.battery_charge_limit', 'off') }}
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Protectie Dezactivata"
                  message: Un buton de protectie este OFF. Limita ridicata la 100A.
                action: notify.darius_all_devices
      - conditions:
          - condition: template
            value_template: "{{ trigger.platform == 'time' and trigger.now.hour == 6 }}"
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Reset Dimineata"
                  message: Ora 06:00 - curent resetat la 100A pentru noua zi.
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: input_boolean.battery_charge_limit
            state: "on"
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: template
            value_template: "{{ now().weekday() == 6 }}"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            above: 91
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 10 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 10
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Calibrare Duminica"
                  message: >-
                    Bateria la 91%+ - continuam lent pana la 100% pentru
                    calibrare.
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: input_boolean.battery_charge_limit
            state: "on"
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: template
            value_template: "{{ now().weekday() != 6 }}"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            above: 91
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 2 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 2
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Protectie Baterie 90%"
                  message: Bateria a depasit 91%. Curent redus la 2A.
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: input_boolean.battery_charge_limit
            state: "on"
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: template
            value_template: "{{ now().weekday() != 6 }}"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            below: 84
          - condition: template
            value_template: |-
              {{ is_state('input_boolean.battery_peak_charge', 'off') or
                 now().hour >= 15 }}
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Reincarcare Baterie"
                  message: Bateria a scazut sub 84%. Incarcare la 100A.
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: input_boolean.battery_peak_charge
            state: "on"
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: time
            before: "15:00:00"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            above: 51
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 2 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 2
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Limitare Export"
                  message: Bateria a atins 51%. Export fortat activ (2A).
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            below: 49
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Incarcare Normala 100A"
                  message: Bateria sub 49%. Incarcare la 100A.
                action: notify.darius_all_devices
      - conditions:
          - condition: state
            entity_id: input_boolean.battery_peak_charge
            state: "on"
          - condition: state
            entity_id: binary_sensor.deye_inverter_grid
            state: "on"
          - condition: time
            after: "15:00:00"
          - condition: numeric_state
            entity_id: sensor.deye_inverter_battery
            below: 85
          - condition: template
            value_template: >-
              {{ states('number.deye_inverter_battery_max_charging_current') |
              float != 100 }}
        sequence:
          - target:
              entity_id: number.deye_inverter_battery_max_charging_current
            data:
              value: 100
            action: number.set_value
          - if:
              - condition: state
                entity_id: input_boolean.inverter_notification_enabled
                state: "on"
            then:
              - data:
                  title: "SOLAR: Incarcare Maxima Dupa 15:00"
                  message: Este dupa ora 15:00. Limita ridicata la 100A.
                action: notify.darius_all_devices
mode: restart