Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - 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 }}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement