Integrate high and low tarrif with template

Hi there

I’m currently stuck with this template, but it doesn’t work… I have this file placed in a template folder and referenced in the configuration.yaml file.

- sensor:
   - name: Tariff
     unit_of_measurement: 'CHF/kWh'
     state: >
       {% set tariff = {"Peak": 0.2845, "OffPeak": 0.2095} %}
       {% set time = {"hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) } %}
       {%if (time.weekday > 0) and (time.weekday < 5) %}
         {%if (time.hour > 7) and (time.hour < 20) %}
           {{ tariff.Peak }}
           {%endif%}
       {%elif (time.weekday == 5) %}
         {%if (time.hour > 7) and (time.hour < 13) %}
           {{ tariff.Peak }}
           {%endif%}
       {%else%}
         {{ tariff.OffPeak }}
       {%endif%} 

The Peak hours are Mo-Fr from 7:00 - 20:00 and on Saturday 7:00 - 13:00, else the Offpeak tariff applies. What am I doing wrong? Thank you for your feedback and have a nice day!