
# ── Solar Pool Pump - Full Control ─────────────────────────────────────────
# Takes precedence over old "Pool Control" automation (disabled below).
# Logic:
#   1. Solar Start: export > 3.5kW → start pump (any time sunrise→5pm, battery > 20%)
#   2. Solar Stop: grid importing > 300W for 4 min → stop ONLY if daily runtime >= 2h
#      (keeps running on grid until minimum is met, then stops to avoid waste)
#   3. Minimum Guarantee: at 11am, if runtime < 2h and pump is off → force start
#      Runs until either: solar dropout (and >= 2h done), or 8pm hard stop
#   4. End of Day: 8pm hard stop regardless
#   5. Pool Light: replaces light logic from old Pool Control

- id: 'solar_pool_pump_start'
  alias: Solar Pool Pump - Solar Start
  description: >
    Starts pool pump when solar is exporting more than pump draw (3.5kW buffer).
    Runs any time from sunrise to 5pm. Minimum 20% battery SOC.
  triggers:
    - trigger: numeric_state
      entity_id: sensor.grid_boss_4434850618_grid_power
      below: -3500
      for:
        minutes: 3
  conditions:
    - condition: state
      entity_id: switch.pool_pump_socket_1
      state: 'off'
    - condition: sun
      after: sunrise
    - condition: time
      before: '17:00:00'
    - condition: numeric_state
      entity_id: sensor.eg4_battery_soc_voltage
      above: 20
  actions:
    - action: switch.turn_on
      target:
        entity_id: switch.pool_pump_socket_1
    - action: notify.mobile_app_chad_s_pixel_7_ii
      continue_on_error: true
      data:
        title: "☀️ Pool Pump - Solar Start"
        message: >
          Solar exporting {{ states('sensor.grid_boss_4434850618_grid_power') | float | abs | round(0) }}W.
          Pool pump started on solar power.
  mode: single

- id: 'solar_pool_pump_stop'
  alias: Solar Pool Pump - Solar Dropout Stop
  description: >
    Stops pump when solar drops and grid starts importing — but only after daily
    minimum runtime (2h) is satisfied. 20-min motor protection guard.
  triggers:
    - trigger: numeric_state
      entity_id: sensor.grid_boss_4434850618_grid_power
      above: 300
      for:
        minutes: 4
  conditions:
    - condition: state
      entity_id: switch.pool_pump_socket_1
      state: 'on'
    - condition: numeric_state
      entity_id: sensor.pool_pump_daily_runtime
      above: 2
    - condition: template
      value_template: >
        {{ (now() - states.switch.pool_pump_socket_1.last_changed).total_seconds() > 1200 }}
  actions:
    - action: switch.turn_off
      target:
        entity_id: switch.pool_pump_socket_1
    - action: notify.mobile_app_chad_s_pixel_7_ii
      continue_on_error: true
      data:
        title: "🌥️ Pool Pump - Solar Dropout"
        message: >
          Grid importing {{ states('sensor.grid_boss_4434850618_grid_power') | float | round(0) }}W,
          daily runtime {{ states('sensor.pool_pump_daily_runtime') | float | round(1) }}h.
          Pump stopped to avoid running on grid.
  mode: single

- id: 'solar_pool_pump_minimum_guarantee'
  alias: Solar Pool Pump - Minimum Runtime Guarantee
  description: >
    At 11am, if pool hasn't run 2 hours yet, force it on regardless of solar.
    Ensures pool chemistry / filtration minimum even on cloudy days.
  triggers:
    - trigger: time
      at: '11:00:00'
  conditions:
    - condition: numeric_state
      entity_id: sensor.pool_pump_daily_runtime
      below: 2
    - condition: state
      entity_id: switch.pool_pump_socket_1
      state: 'off'
  actions:
    - action: switch.turn_on
      target:
        entity_id: switch.pool_pump_socket_1
    - action: notify.mobile_app_chad_s_pixel_7_ii
      continue_on_error: true
      data:
        title: "🌀 Pool Pump - Minimum Runtime"
        message: >
          Only {{ states('sensor.pool_pump_daily_runtime') | float | round(1) }}h runtime today.
          Running pump to hit 2h minimum.
  mode: single

- id: 'solar_pool_pump_end_of_day'
  alias: Solar Pool Pump - End of Day Stop
  description: Hard stop at 8pm regardless of solar or runtime.
  triggers:
    - trigger: time
      at: '20:00:00'
  conditions:
    - condition: state
      entity_id: switch.pool_pump_socket_1
      state: 'on'
  actions:
    - action: switch.turn_off
      target:
        entity_id: switch.pool_pump_socket_1
  mode: single

- id: 'pool_light_pump_sync'
  alias: Pool Light - Follows Pump at Night
  description: >
    Turns pool light on when pump is on after sunset, off when pump turns off.
    Replaces light logic from the now-disabled Pool Control automation.
  triggers:
    - trigger: state
      entity_id: switch.pool_pump_socket_1
      to: 'on'
    - trigger: state
      entity_id: switch.pool_pump_socket_1
      to: 'off'
  actions:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on' }}"
            - condition: sun
              after: sunset
          sequence:
            - action: switch.turn_on
              target:
                entity_id: switch.sonoff_100136ea65
        - conditions:
            - condition: template
              value_template: "{{ trigger.to_state.state == 'off' }}"
          sequence:
            - action: switch.turn_off
              target:
                entity_id: switch.sonoff_100136ea65
  mode: single
