Template outputting code as part of the string

I’m trying to return the current tariff based upon the day and time but I’m getting the opening part of the code too. What documents should I read to learn what’s up ?
I’ve used Helpers before but it’s my first crack at a template.
Thanks

  - platform: template 
    sensors: 
      current_tariff: 
        friendly_name: "Current Tariff" 
        value_template: > 
          {% set hour = now().hour %} 
          {% set weekday = now().weekday() %} 
          
          {% if weekday in [0, 1, 2, 3, 4] %}  # Monday to Friday 
            {% if hour >= 14 and hour < 20 %} 
              Peak 
            {% elif (hour >= 7 and hour < 14) or (hour >= 20 and hour < 22) %} 
              Shoulder 
            {% else %} 
              Offpeak 
            {% endif %} 
          {% else %}  # Saturday and Sunday 
            {% if hour >= 7 and hour < 22 %} 
              Shoulder 
            {% else %} 
              Offpeak 
            {% endif %} 
          {% endif %}

Result type: string

  • platform: template
    sensors:
    current_tariff:
    friendly_name: “Current Tariff”
    value_template: >

          # Monday to Friday 
           
            Offpeak
    

did you put that whole block into dev-tools->template?

if so, it’s expected that doing that will output the first part of your yaml. template only evaluates jinja templates and strings. it does not interpret template configurations and such.

1 Like

And for further clarification, a template is defined as anything that falls between the following delimiters (ref):

The default Jinja delimiters are configured as follows:

Any text you place in the template tool that is not between those delimiters will simply be spit back out at you without evaluation.

1 Like

I think he’s put the entire config in the helper state template box.

1 Like

Lol indeed I did. Didn’t know not to. Oops

Fixed. Thanks

          {% set t = "Offpeak" %}
          {% if weekday <5 %}
            {% if hour >= 14 and hour < 20 %} 
              {% set t = "Peak" %}           
            {% elif (hour >= 7 and hour < 14) or (hour >= 20 and hour < 22) %} 
              {% set t = "Shoulder" %} 
            {% endif %}
          {% endif %}
          {{ "Shoulder" if (weekday > 4 and (hour >=7 and hour <22)) else t }}

Just showing how the logic can be reduced a bit :wink:

1 Like

Thanks very much. That’s awesome.

1 Like

I like these games :wink: . This one is a one-liner (line breaks just for readability) with a tariff visualisation:

{{ {'O':'Offpeak','S':'Shoulder','P':'Peak'}
   [("OOOOOOOSSSSSSSPPPPPPSSOO",
     "OOOOOOOSSSSSSSSSSSSSSSOO")
    [now().weekday()>4][now().hour]] }}

Explanation:

('x','y')[now().weekday()>4]

returns 'x' Mon-Fri when the test returns false (0) and 'y' Sat & Sun when the test returns true (1). The ('x','y') is a tuple, like a list but can’t be modified.

Then, 'x' is replaced by the weekday tariff and 'y' by the weekend tariff, and we look up the character associated with the hour. So on a Wednesday:

[("OOOOOOOSSSSSSSPPPPPPSSOO",
  "OOOOOOOSSSSSSSSSSSSSSSOO")
 [now().weekday()>4][now().hour]]

reduces to:

"OOOOOOOSSSSSSSPPPPPPSSOO"[now().hour]

So at 16:00 on a Wednesday, we get a 'P' out of the whole [...] section, and that is the key lookup for the {...} dict, which returns 'Peak':

{{ {'O':'Offpeak','S':'Shoulder','P':'Peak'}['P'] }}

Even shorter version (94 characters) by deriving the tariff arithmetically:

{{('Offpeak','Shoulder','Peak')[min(1+(now().weekday()<5),now().hour//7,(23-now().hour)//2)]}}

This generates the tariff at any combination of hour and weekday by taking the minimum of three lines / slopes:

1+(now().weekday()<5)

is 2 for weekdays 0 to 4, 1 for 5 and 6 using false==0, true==1;

now().hour//7

rises from 0 by 1 every 7 hours (// is the integer division operator); and

(23-now().hour)//2

drops from 11 by 2 every 2 hours. The minimum of all three values (0, 1 or 2) is used to look up the tariff name. Graphical representation:

Definitely got obsessed with this one. Sorry.

3 Likes

Thank you for the breakdown.

Awesome work! We need a template code golf section on this forum.

2 Likes