################################
##    Diningroom Thermostat   ##
################################

climate:
  - platform: generic_thermostat
    name: Diningroom floor heating
    unique_id: diningroomfloor61
    heater: switch.thermostatdiningroom
    target_sensor: sensor.thermostatdiningroom_temperature
    min_temp: 18
    max_temp: 28
    target_temp: 23
    away_temp: 18
    cold_tolerance: 0.5
    hot_tolerance: 0.5
    min_cycle_duration:
      minutes: 30

input_number:
  diningroom_rank_slider:
    name: diningroom
    min: 0
    max: 24
    step: 1

  diningroom_price_plan_slider:
    name: diningroom
    min: 0
    max: 100
    step: 0.1
    unit_of_measurement: ¢/kWh
    icon: mdi:cash-lock
    mode: box
    
  diningroom_price_avg_slider:
    name: Diningroom Average price hours
    min: 1
    max: 24
    step: 1
    unit_of_measurement: h
    icon: mdi:cash-clock

  diningroom_cheapest_period_slider:
    name: Diningroom Cheapest period hours
    min: 1
    max: 24
    step: 1
    unit_of_measurement: h
    icon: mdi:cash-clock


input_select:
  diningroom_plan_input:
    name: Diningroom heating plan
    options:
      - rank
      - price
      - price or rank
      - price and rank

template:
  - binary_sensor:
    - name: "Diningroom Rank acceptable"
      unique_id: diningroom_rank_acceptable
      device_class: power
      state: >
        {% set value = state_attr("sensor.shf_electricity_price", "data")[states("sensor.shf_idx")|int+now().hour] %}
        {{ value["Rank"] <= states("input_number.diningroom_rank_slider") | int }}

    - name: "Diningroom Price acceptable"
      unique_id: diningroom_price_acceptable
      device_class: power
      state: >
          {% set value = state_attr("sensor.shf_electricity_price", "data")[states("sensor.shf_idx")|int+now().hour] %}
          {{ value["PriceWithTax"] | float  <= states("input_number.diningroom_price_plan_slider") | float /100 }}

    - name: "Diningroom Price or Rank acceptable"
      unique_id: diningroom_price_or_rank_acceptable
      device_class: power
      state: '{{ is_state("binary_sensor.diningroom_rank_acceptable", "on") or is_state("binary_sensor.diningroom_price_acceptable", "on") }}'

    - name: "Diningroom Price and Rank acceptable"
      unique_id: diningroom_price_and_rank_acceptable
      device_class: power
      state: '{{ is_state("binary_sensor.diningroom_rank_acceptable", "on") and is_state("binary_sensor.diningroom_price_acceptable", "on") }}'

  - sensor:
    - name: diningroom
      unique_id: diningroom_plan
      state: >
        {% set mapper = {
            "price": "price_acceptable", 
            "rank": "rank_acceptable", 
            "price or rank": "price_or_rank_acceptable", 
            "price and rank": "price_and_rank_acceptable" } %}
        {% set state = states("input_select.diningroom_plan_input") %}
        {% set id = mapper[state] if state in mapper %}
        {{ id }}
      icon: mdi:map-marker-distance
      attributes:
        phase: "L3"
        power: 1600
