Two hours minimum template

Hello,
I need to get two consecutive hours with the lowest value of these attributes. Will you please help me to achieve this?
This does not work: {{state_attr('sensor.current_ote_energy_cost', 'min_value')}}

Something like this, but with value of hour and two hours.

{%set ote_price_today = state_attr('sensor.current_ote_energy_cost',  1),
state_attr('sensor.current_ote_energy_cost',  2),
state_attr('sensor.current_ote_energy_cost',  3),
state_attr('sensor.current_ote_energy_cost',  4),
state_attr('sensor.current_ote_energy_cost',  5),
state_attr('sensor.current_ote_energy_cost',  6),
state_attr('sensor.current_ote_energy_cost',  7),
state_attr('sensor.current_ote_energy_cost',  8),
state_attr('sensor.current_ote_energy_cost',  9),
state_attr('sensor.current_ote_energy_cost',  10),
state_attr('sensor.current_ote_energy_cost',  11),
state_attr('sensor.current_ote_energy_cost',  12),
state_attr('sensor.current_ote_energy_cost',  13),
state_attr('sensor.current_ote_energy_cost',  14),
state_attr('sensor.current_ote_energy_cost',  15),
state_attr('sensor.current_ote_energy_cost',  16),
state_attr('sensor.current_ote_energy_cost',  17),
state_attr('sensor.current_ote_energy_cost',  18),
state_attr('sensor.current_ote_energy_cost',  19),
state_attr('sensor.current_ote_energy_cost',  20),
state_attr('sensor.current_ote_energy_cost',  21),
state_attr('sensor.current_ote_energy_cost',  22),
state_attr('sensor.current_ote_energy_cost',  23),
state_attr('sensor.current_ote_energy_cost',  24) %}
{{ ote_price_today|max }}

Have anyone idea?

I don’t know if this is exactly what you meant by:

… two consecutive hours with the lowest value…

And there is likely a more efficient way to go about this, but the following will give you the start hour of the 2-hour period with the lowest cumulative cost.

{% set ns = namespace(attr_dict=[]) %}
{%- set f = ((states.sensor.current_ote_energy_cost.attributes)|list)[0:24] %}
{%- for e in f%}
  {%- set ns.attr_dict = ns.attr_dict + [(e, state_attr('sensor.current_ote_energy_cost', e))] %}
{%- endfor %}
{%- set price_map = dict(ns.attr_dict) %}
{%- set price_sort = price_map.values()|list %}
{%- set keys_list = price_map.keys()|list %}
{%- set ns = namespace(combo=[]) %}
{%- for p in keys_list %}
  {%- set p = p|int %}
  {%- if p < 24 %}
    {%- set ns.combo = ns.combo + [(p, ((price_sort)[p-1] + (price_sort)[p])|round(2))] %}
  {%- else %}
    {%- set ns.combo = ns.combo + [(24, ((price_sort)[23] + (price_sort)[0])|round(2))] %}
  {%- endif %}{%- endfor %}
{%- set mapper = dict(ns.combo) %}
{%- set key = mapper.keys()|list %}
{%- set val = mapper.values()|list %}
{%- set val_min = mapper.values()|min %}
{{ key[val.index(val_min)] }}

Wow, thank you, Works perfectly! :slight_smile:

1 Like

Hi, I have a new sensor and a new problem. I need to get 2-5 consecutive hours from an attribute that have the lowest cumulative value for the whole day.
The attribute is composed of a name (date and time), order by lowest price, the price itself.
I would need to set up a few templates that will determine the first hour of the start of the price accumulation.
For example:
I want 3 consecutive hours that are the cheapest on average. When I look, I see that the lowest price starts at 2:00, followed by the second one at 3:00 and then the third one at 1:00, so I need to start at 1:00 because that is the three consecutive cheapest hours.

Hello! i use your code to set find the best time to start my water-heating.
i can show it in the dashboard
but if i want to use it for a automation i cant find the helper because it is not a timestamp
if i change the helper to “date” or “time” i dont get any result - just “not available”

any ideas?

Use a Template trigger…

trigger: 
  - platform: template
    value_template: "{{ now().hour == states('sensor.EXAMPLE') | int }}"
1 Like