Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. - hodinovka:
  2. value_template:
  3. "{# Define your intervals here as tuples (hour starting the interval, hour ending the interval (excluded)) #}
  4. {% set intervals = [
  5. (1, 8),
  6. (8, 16),
  7. (16, 24),
  8. ] %}
  9.  
  10. {# We need to use namespace so we can write into it in inner cycle #}
  11. {% set min = namespace(price=None, dt=None, cheapest_hours=[]) %}
  12. {% set cheapest_hours = [] %}
  13.  
  14.  
  15. {% for interval in intervals %}
  16. {# Reset min price from previous runs #}
  17. {% set min.price = None %}
  18.  
  19. {# Go through all the hours in the interval (end excluded) and find the hour with lowest price #}
  20. {% for i in range(interval[0], interval[1]) %}
  21. {# Get datetime of current hour in current interval #}
  22. {% set hour_dt = now().replace(hour=i, minute=0, second=0, microsecond=0) %}
  23.  
  24. {# Get value for that hour #}
  25. {% set value = states.sensor.current_spot_electricity_hour_order.attributes.get(hour_dt.isoformat()) %}
  26.  
  27. {# Skip if not found #}
  28. {% if value is not defined %}
  29. {% break %}
  30. {% endif %}
  31.  
  32. {# value is tuple (order, price), we'll use the price #}
  33. {% set price = value[1] %}
  34.  
  35. {# Min price is not set or is higher than price of current hour => store the min price and hour #}
  36. {% if min.price is none or price < min.price %}
  37. {% set min.price = price %}
  38. {% set min.dt = hour_dt %}
  39. {% endif %}
  40. {% endfor %}
  41.  
  42. {# Store cheapest hour in current interval #}
  43. {% set min.cheapest_hours = min.cheapest_hours + [min.dt.hour] %}
  44. {% endfor %}
  45.  
  46. {# use this to get the cheapest hours #}
  47. {# {{ min.cheapest_hours }} #}
  48.  
  49. {# return True if current hour is in the cheapest hour of any interval #}
  50. {{ now().hour in min.cheapest_hours }}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement