# ============================================
# Spotprisoptimering - HUVUDKONFIGURATION
# ============================================

# REST-sensorer för alla API:er
rest:
  # Värmepump (signal)
  - resource: "https://www.spotprisoptimering.se/api/signal.php?api_key=DIN_API_NYCKEL"
    scan_interval: 300
    sensor:
      name: "Spotpris Signal"
      unique_id: spotpris_signal
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - price
        - level
        - thresholds
        - recommendation
        - api_version
        
  # Elbil - full information
  - resource: "https://www.spotprisoptimering.se/api/car.php?api_key=DIN_API_NYCKEL"
    scan_interval: 600
    sensor:
      name: "Spotpris Bil"
      unique_id: spotpris_car
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - vehicle
        - charging
        - predictive
        - alerts
        
  # Varmvatten
  - resource: "https://www.spotprisoptimering.se/api/water.php?api_key=DIN_API_NYCKEL&format=json"
    scan_interval: 600
    sensor:
      name: "Spotpris Varmvatten"
      unique_id: spotpris_water
      value_template: "{{ value_json.signal }}"
      json_attributes:
        - price
        - settings
        
  # Solceller & batteri
  - resource: "https://www.spotprisoptimering.se/api/solar.php?api_key=DIN_API_NYCKEL"
    scan_interval: 600
    sensor:
      name: "Spotpris Solceller"
      unique_id: spotpris_solar
      value_template: "{{ value_json.recommendation.action }}"
      json_attributes:
        - settings
        - forecast
        - recommendation
        - solar

# Template-sensorer för enklare användning
template:
  - sensor:
      # Aktuellt pris
      - 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
        
      # Prisnivå (LOW/NORMAL/HIGH)
      - 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 %}
          
      # Värmepumpsrekommendation
      - name: "Spotpris Värmepump"
        unique_id: spotpris_heatpump_action
        state: "{{ state_attr('sensor.spotpris_signal', 'recommendation').action }}"
        icon: mdi:heat-pump
        
      # Bilens laddningsstatus
      - 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
        
      # Bilens rekommendation
      - name: "Spotpris Bil Rekommendation"
        unique_id: spotpris_car_recommendation
        state: "{{ state_attr('sensor.spotpris_car', 'signal') }}"
        icon: mdi:car-electric
        
      # Varmvattenrekommendation
      - name: "Spotpris Varmvatten"
        unique_id: spotpris_water_action
        state: "{{ states('sensor.spotpris_water') }}"
        icon: mdi:water-boiler
        
      # Solcellsrekommendation
      - name: "Spotpris Solceller Action"
        unique_id: spotpris_solar_action
        state: "{{ state_attr('sensor.spotpris_solar', 'recommendation').action }}"
        icon: mdi:solar-power
        
      # Batteriets laddningsnivå (om tillgängligt)
      - 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