rnovacek / homeassistant_cz_energy_spot_prices

Home Assistant integration that provides current Czech electricity spot prices based on OTE.
Apache License 2.0
91 stars 22 forks source link

template sensor "Find cheapest hours in selected interval" #33

Open Fallu1 opened 1 year ago

Fallu1 commented 1 year ago

Hi, I have problem with template sensor "Find cheapest hours in selected interval". Each day starts as unavailable and turns on during the day. Don't know where the problem could be? here my code :

hodinovka:
        value_template:
          "{# Define your intervals here as tuples (hour starting the interval, hour ending the interval (excluded)) #}
          {% set intervals = [
          (1, 8),
          (8, 16),
          (16, 24),
          ] %}

          {# We need to use namespace so we can write into it in inner cycle #}
          {% set min = namespace(price=None, dt=None, cheapest_hours=[]) %}
          {% set cheapest_hours = [] %}

          {% for interval in intervals %}
          {# Reset min price from previous runs #}
          {% set min.price = None %}

          {# Go through all the hours in the interval (end excluded) and find the hour with lowest price #}
          {% for i in range(interval[0], interval[1]) %}
          {# Get datetime of current hour in current interval #}
          {% set hour_dt = now().replace(hour=i, minute=0, second=0, microsecond=0) %}

          {# Get value for that hour #}
          {% set value = states.sensor.current_spot_electricity_hour_order.attributes.get(hour_dt.isoformat()) %}

          {# Skip if not found #}
          {% if value is not defined %}
          {% break %}
          {% endif %}

          {# value is tuple (order, price), we'll use the price #}
          {% set price = value[1] %}

          {# Min price is not set or is higher than price of current hour => store the min price and hour #}
          {% if min.price is none or price < min.price %}
          {% set min.price = price %}
          {% set min.dt = hour_dt %}
          {% endif %}
          {% endfor %}

          {# Store cheapest hour in current interval #}
          {% set min.cheapest_hours = min.cheapest_hours + [min.dt.hour] %}
          {% endfor %}

          {# use this to get the cheapest hours #}
          {# {{ min.cheapest_hours }} #}

          {# return True if current hour is in the cheapest hour of any interval #}
          {{ now().hour in min.cheapest_hours }}"

Snímek obrazovky 2023-09-10 213940

rnovacek commented 1 year ago

Could you please check that sensor.current_spot_electricity_hour_order has attributes with prices during the times when the template result is unavailable?

ssditt commented 1 year ago

I would kindly ask for new boolean sensor/switch "cheapest within defined interval". The purpose is to avoid "templating" for owners of photovoltaic power plants (not only). It will require three additional config parameters: "Begin" - a hour when production starts "End" - a hour when production ends "Hours" - count of hours with cheapest price, non consecutive. The sensor will help us to determine when to prefer consumption against export of electricity. I know it can be handled by suggested template, but having this sensor it will simplify related automation (and our life)

Fallu1 commented 1 year ago

Could you please check that sensor.current_spot_electricity_hour_order has attributes with prices during the times when the template result is unavailable?

sorry, problem on my end. the problem is, that the router is turned off at midnight. everything is fine now.