Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Fallu1 opened this issue Sep 10, 2023 · 3 comments
Open

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

Fallu1 opened this issue Sep 10, 2023 · 3 comments

Comments

@Fallu1
Copy link

Fallu1 commented Sep 10, 2023

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
Copy link
Owner

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
Copy link

ssditt commented Oct 11, 2023

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
Copy link
Author

Fallu1 commented Oct 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants