# ============================================
# Spotprisoptimering - KOMPLETT PAKET
# ============================================
# Kopiera hela denna fil till /config/packages/spotpris.yaml
# och lägg till "packages: !include_dir_named packages" i configuration.yaml

homeassistant:
  customize:
    sensor.spotpris_signal:
      friendly_name: "Spotpris Signal"
      icon: mdi:flash
    sensor.spotpris_aktuellt:
      friendly_name: "Aktuellt pris"
      icon: mdi:currency-sek
    sensor.spotpris_nivå:
      friendly_name: "Prisnivå"
      icon: mdi:signal

rest:
  - resource: "https://www.spotprisoptimering.se/api/signal.php?api_key=YOUR_API_KEY_HERE"
    scan_interval: 300
    sensor:
      name: "Spotpris Signal"
      unique_id: spotpris_signal
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - price
        - level
        - thresholds
        - recommendation
        - api_version
        
  - resource: "https://www.spotprisoptimering.se/api/car.php?api_key=YOUR_API_KEY_HERE"
    scan_interval: 600
    sensor:
      name: "Spotpris Bil"
      unique_id: spotpris_car
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - vehicle
        - charging
        - predictive
        - alerts
        
  - resource: "https://www.spotprisoptimering.se/api/water.php?api_key=YOUR_API_KEY_HERE&format=json"
    scan_interval: 600
    sensor:
      name: "Spotpris Varmvatten"
      unique_id: spotpris_water
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - price
        - settings
        
  - resource: "https://www.spotprisoptimering.se/api/solar.php?api_key=YOUR_API_KEY_HERE"
    scan_interval: 600
    sensor:
      name: "Spotpris Solceller"
      unique_id: spotpris_solar
      value_template: "{{ value_json.recommendation.action }}"
      json_attributes:
        - settings
        - forecast
        - recommendation
        - solar

template:
  - sensor:
      - name: "Spotpris Aktuellt"
        unique_id: spotpris_current_price
        state: "{{ state_attr('sensor.spotpris_signal', 'price').current }}"
        unit_of_measurement: "kr/kWh"
        icon: mdi:currency-sek
        
      - name: "Spotpris Nivå"
        unique_id: spotpris_level
        state: "{{ state_attr('sensor.spotpris_signal', 'level') }}"
        icon: >
          {% if state_attr('sensor.spotpris_signal', 'level') == 'LOW' %}
            mdi:arrow-down-bold
          {% elif state_attr('sensor.spotpris_signal', 'level') == 'HIGH' %}
            mdi:arrow-up-bold
          {% else %}
            mdi:minus
          {% endif %}
          
      - name: "Spotpris Värmepump"
        unique_id: spotpris_heatpump_action
        state: "{{ state_attr('sensor.spotpris_signal', 'recommendation').action }}"
        icon: mdi:heat-pump
        
      - name: "Spotpris Bil Status"
        unique_id: spotpris_car_status
        state: "{{ state_attr('sensor.spotpris_car', 'vehicle').current_charge }}"
        unit_of_measurement: "%"
        icon: mdi:car-electric
        
      - name: "Spotpris Bil Rekommendation"
        unique_id: spotpris_car_recommendation
        state: "{{ state_attr('sensor.spotpris_car', 'signal') }}"
        icon: mdi:car-electric
        
      - name: "Spotpris Varmvatten"
        unique_id: spotpris_water_action
        state: "{{ states('sensor.spotpris_water') }}"
        icon: mdi:water-boiler
        
      - name: "Spotpris Solceller Action"
        unique_id: spotpris_solar_action
        state: "{{ state_attr('sensor.spotpris_solar', 'recommendation').action }}"
        icon: mdi:solar-power
        
      - name: "Spotpris Batteri SOC"
        unique_id: spotpris_battery_soc
        state: "{{ state_attr('sensor.spotpris_solar', 'settings').current_soc | default(0) }}"
        unit_of_measurement: "%"
        icon: mdi:battery

input_boolean:
  water_heater_enabled:
    name: "💧 Aktivera varmvattenstyrning"
    icon: mdi:water-boiler
    initial: off
    
  battery_enabled:
    name: "🔋 Aktivera smart batteri"
    icon: mdi:battery
    initial: off
    
  holiday_mode:
    name: "🏖️ Semesterläge"
    icon: mdi:beach
    initial: off

automation:
  # Värmepump
  - id: spotpris_heatpump_control
    alias: "🌡️ Styr värmepump efter elpris"
    trigger:
      - platform: state
        entity_id: sensor.spotpris_level
      - platform: time_pattern
        minutes: "/15"
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: sensor.spotpris_level
                state: "LOW"
            sequence:
              - service: switch.turn_on
                target:
                  entity_id: switch.heatpump
          - conditions:
              - condition: state
                entity_id: sensor.spotpris_level
                state: "HIGH"
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.heatpump
          default:
            - service: switch.turn_on
              target:
                entity_id: switch.heatpump
    mode: single
                
  # Bil
  - id: spotpris_car_charging
    alias: "🚗 Smart billaddning"
    trigger:
      - platform: time_pattern
        minutes: "/10"
      - platform: state
        entity_id: sensor.spotpris_car_recommendation
    condition:
      - condition: state
        entity_id: binary_sensor.car_plugged_in
        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
              - service: notify.mobile_app_phone
                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
          - conditions:
              - condition: state
                entity_id: sensor.spotpris_car_recommendation
                state: "WAIT"
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.car_charger
          default:
            - service: switch.turn_off
              target:
                entity_id: switch.car_charger
    mode: single
                
  # Varmvatten
  - id: spotpris_water_heater
    alias: "💧 Varmvattenstyrning"
    trigger:
      - platform: time_pattern
        minutes: "/15"
      - platform: state
        entity_id: sensor.spotpris_water_action
    condition:
      - condition: state
        entity_id: input_boolean.water_heater_enabled
        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
          - conditions:
              - condition: state
                entity_id: sensor.spotpris_water_action
                state: "STOPP"
            sequence:
              - service: switch.turn_off
                target:
                  entity_id: switch.water_heater
    mode: single
                
  # Batteri
  - id: spotpris_battery_arbitrage
    alias: "🔋 Smart batterihantering"
    trigger:
      - platform: time_pattern
        minutes: "/15"
      - platform: state
        entity_id: sensor.spotpris_solar_action
    condition:
      - condition: state
        entity_id: input_boolean.battery_enabled
        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
          - conditions:
              - condition: template
                value_template: "{{ state_attr('sensor.spotpris_solar', 'recommendation').action == 'discharge' }}"
            sequence:
              - service: switch.turn_on
                target:
                  entity_id: switch.battery_discharge
          default:
            - service: switch.turn_off
              target:
                entity_id: switch.battery_charger
            - service: switch.turn_off
              target:
                entity_id: switch.battery_discharge
    mode: single
                
  # Prislarm
  - id: spotpris_price_alerts
    alias: "🔔 Prislarm"
    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_phone
                data:
                  title: "⚡ Lågt elpris!"
                  message: "Priset är nu {{ states('sensor.spotpris_aktuellt') }} kr/kWh"
          - conditions:
              - condition: state
                entity_id: sensor.spotpris_level
                state: "HIGH"
            sequence:
              - service: notify.mobile_app_phone
                data:
                  title: "💰 Högt elpris"
                  message: "Priset är nu {{ states('sensor.spotpris_aktuellt') }} kr/kWh"
    mode: single