Time based electricity price

Anyone find the best way to utilize a time and season based electricity price / kWh to keep track of billing?

I tried setting up the following sensor to use in the energy console, but could not get it to work. Any tips?

template:
  - trigger:
    - platform: time_pattern
      minutes: /1
    sensor:
      - name: energy_price_rs4
        friendly_name: Energy Tariff RhytmShift4
        unique_id: energy_price_rs4
        unit_of_measurement: 'USD/kWh'
        state: >
          {% set time = { "month": (now().strftime('%m') | int), "hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) } %}
          {% set tariff = { "Peak": 0.126, "SeasonalPeak": 0.054, "OffPeak": 0.025 } %}
          {%if ((time.month <= 3)) %} {# jan through April #}
              {%if ((time.hour >= 6) and (time.hour < 9)) %}
                {{ tariff.SeasonalPeak }}
              {%elif ((time.hour >= 18) and (time.hour < 22)) %}
                {{ tariff.Peak }}
              {%else%}
                {{ tariff.OffPeak }}
              {%endif%}
          {%else%} {# May June September October - Summer #}
              {%if ((time.hour >= 18) and (time.hour < 22)) %}
                {{ tariff.Peak }}
              {%else%}
                {{ tariff.OffPeak }}
              {%endif%}
          {%endif%}