Template States Misconfig

I am trying to create a couple of template sensors to track peak and off peak electricity usage.

I created a schedule helper [electricity_peak] which is on whenever peak rate applies.

I am then trying to create a template sensor to track grid usage, I will then use a reimann sensor to aggregate usage.

My template definition is:

- sensor:
  - name: "Peak Rate Usage"
    state_class: "measurement"
    device_class: "power"
    unit_of_measurement: "W"
    icon: mdi:battery-plus
    state: >
      {% if states('electricity_peak') in ('On')  %}
        {% if states('sensor.smart_meter_electricity_power')| float > 0 %}
          {{ states('sensor.smart_meter_electricity_power')| float }}
        {% else %}
        {% endif %}
      {% else %}
      {% endif %}    

For some reason [if states('electricity_peak') in ('On') ] is always false.

What am I missing?


You probably want


and it will be 'on' or 'off' in lower case.

Thanks, I was staring at this for way too long without spotting it!

Bit tidier:

    state: >
        {{ max(0, states('sensor.smart_meter_electricity_power')|float(0))
           if is_state('schedule.electricity_peak', 'on') else 0 }}