Advertisement
Guest User

Untitled

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