# ============================================
# Spotprisoptimering - AUTOMATIONER
# ============================================

# 1. STYRNING AV VÄRMEPUMP (SHELLY)
- id: spotpris_heatpump_control
  alias: "🌡️ Styr värmepump efter elpris"
  description: "Startar/stoppar värmepump baserat på prisnivå"
  trigger:
    - platform: state
      entity_id: sensor.spotpris_level
    - platform: time_pattern
      minutes: "/15"
  condition: []
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_level
              state: "LOW"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.heatpump  # Ändra till din switch
            - service: persistent_notification.create
              data:
                title: "🌡️ Värmepump"
                message: "Lågt elpris - startar värmepump"
                notification_id: "heatpump_low"
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_level
              state: "HIGH"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.heatpump  # Ändra till din switch
            - service: persistent_notification.create
              data:
                title: "🌡️ Värmepump"
                message: "Högt elpris - stoppar värmepump (sparläge)"
                notification_id: "heatpump_high"
      default:
        - service: switch.turn_on
          target:
            entity_id: switch.heatpump  # Ändra till din switch

# 2. SMART BIL-LADDNING (PREDIKTIV)
- id: spotpris_car_charging
  alias: "🚗 Smart billaddning"
  description: "Laddar bilen vid låga priser eller akut behov"
  trigger:
    - platform: time_pattern
      minutes: "/10"
    - platform: state
      entity_id: sensor.spotpris_car_recommendation
  condition:
    - condition: state
      entity_id: binary_sensor.car_plugged_in  # Ändra till din sensor
      state: "on"
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_car_recommendation
              state: "CHARGE_NOW"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.car_charger  # Ändra till din switch
            - service: notify.mobile_app
              data:
                title: "🚗 Akut laddning"
                message: "Batteriet är kritiskt lågt - laddar OMEDELBART!"
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_car_recommendation
              state: "CHARGE"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.car_charger  # Ändra till din switch
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_car_recommendation
              state: "WAIT"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.car_charger  # Ändra till din switch
      default:
        - service: switch.turn_off
          target:
            entity_id: switch.car_charger  # Ändra till din switch

# 3. VARMVATTENSTYRNING
- id: spotpris_water_heater
  alias: "💧 Varmvattenstyrning"
  description: "Värmer vatten vid låga priser"
  trigger:
    - platform: time_pattern
      minutes: "/15"
    - platform: state
      entity_id: sensor.spotpris_water_action
  condition:
    - condition: state
      entity_id: input_boolean.water_heater_enabled  # Aktiveringsknapp
      state: "on"
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_water_action
              state: "START"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.water_heater  # Ändra till din switch
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_water_action
              state: "STOPP"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.water_heater  # Ändra till din switch

# 4. BATTERI-ARBITRAGE (SOLCELLER)
- id: spotpris_battery_arbitrage
  alias: "🔋 Smart batterihantering"
  description: "Laddar/säljer från batteri baserat på prisprognos"
  trigger:
    - platform: time_pattern
      minutes: "/15"
    - platform: state
      entity_id: sensor.spotpris_solar_action
  condition:
    - condition: state
      entity_id: input_boolean.battery_enabled  # Aktiveringsknapp
      state: "on"
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ state_attr('sensor.spotpris_solar', 'recommendation').action in ['charge', 'prepare_for_sale'] }}"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.battery_charger  # Ändra till din switch
            - service: notify.mobile_app
              data:
                title: "🔋 Laddar batteri"
                message: "{{ state_attr('sensor.spotpris_solar', 'recommendation').message }}"
        - conditions:
            - condition: template
              value_template: "{{ state_attr('sensor.spotpris_solar', 'recommendation').action == 'discharge' }}"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.battery_discharge  # Ändra till din switch
      default:
        - service: switch.turn_off
          target:
            entity_id: switch.battery_charger
        - service: switch.turn_off
          target:
            entity_id: switch.battery_discharge

# 5. PRISNOTISER (PUSH)
- id: spotpris_price_alerts
  alias: "🔔 Prislarm"
  description: "Skickar notis vid extrempriser"
  trigger:
    - platform: state
      entity_id: sensor.spotpris_level
  condition:
    - condition: template
      value_template: "{{ trigger.to_state.state in ['LOW', 'HIGH'] }}"
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_level
              state: "LOW"
          sequence:
            - service: notify.mobile_app
              data:
                title: "⚡ Lågt elpris!"
                message: "Priset är nu {{ states('sensor.spotpris_aktuellt') }} kr/kWh. Perfekt att värma/ladda!"
        - conditions:
            - condition: state
              entity_id: sensor.spotpris_level
              state: "HIGH"
          sequence:
            - service: notify.mobile_app
              data:
                title: "💰 Högt elpris"
                message: "Priset är nu {{ states('sensor.spotpris_aktuellt') }} kr/kWh. Vänta med förbrukning."
    - service: persistent_notification.create
      data:
        title: "⚡ Prisändring"
        message: "Elpriset är nu {{ states('sensor.spotpris_level') }} ({{ states('sensor.spotpris_aktuellt') }} kr/kWh)"