Assigning a value via a template based on the time

I’m trying to set a value via a template based on the time of day, I’ve swapped to Octopus Flex and there are only 3 prices through-out the day.
I’d like to build new tiles for my dashboard but with the following template code I only get one value, no matter what the time.
#Octopus Flex Export prices

  • platform: template
    sensors:
    flex_export_price:
    friendly_name: “Flex Export Price”
    unit_of_measurement: “p”
    value_template: >
    {% if states(‘sensor.time’) > ‘02:01’ and states.sensor.time.state < ‘05:00’ %}
    8.44
    {% elif states(states.sensor.time.state) > ‘16:01’ and states.sensor.time.state < ‘19:00’%}
    32.36
    {% else %} 21.40
    {% endif %}
    It displays perfectly in the developer tools/template section.
    Thanks in advance.

You’re mixing/combining states() function and the state object method in your first elif. Using a variable can make your template easier to read and you only have to make sure to get the function/method correct once…

- platform: template
  sensors:
    flex_export_price:
      friendly_name: Flex Export Price
      unit_of_measurement: p
      value_template: >
        {%- set time = states('sensor.time') %}
        {%- if '02:01' < time < '05:00' %}
          8.44
        {%- elif '16:01' < time < '19:00' %}
          32.36
        {%- else %} 21.40
        {%- endif %}

Thankyou very much, I’ll give that a try.