Automation using and & or condition with templates

I am trying to create an automation to change between peak and offpeak. I have 3 different peak/offpeak rates depending on what month it is, so I can’t use the simple tariff.next to manage this. The auomation I am trying to use is giving me an error when trying to save. The error is:

“Message malformed: Unexpected value for condition: ‘{% set n = now() %} {{ n.month == ‘5’ }}’. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone @ data[‘condition’][0][‘conditions’][1][‘conditions’][0]”

The syntax of the automation I am trying to do is:

alias: Energy Offpeak
description: ''
trigger:
  - platform: time
    at: '18:00:00'
condition:
  condition: and
  conditions:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: or
      conditions:
        - condition: template
            value_template: "{% set n = now() %} {{ n.month == '5' }}"
        - condition: template
            value_template: "{% set n = now() %} {{ n.month == '6' }}"
        - condition: template
            value_template: "{% set n = now() %} {{ n.month == '9' }}"
        - condition: template
            value_template: "{% set n = now() %} {{ n.month == '10' }}"
action:
  - service: utility_meter.select_tariff
    data:
      tariff: offpeak
    target:
      entity_id:
        - utility_meter.daily_energy
        - utility_meter.monthly_energy
mode: single

Does anyone have any ideas or suggestions for what the problem is, or a different/better way to do this?

Nevermind, I figured it out… I had to outdent the “value_template” by one to line up with the condition, like this:

condition:
  condition: and
  conditions:
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: or
      conditions:
        - condition: template
          value_template: '{% set n = now() %} {{ n.month == ''5'' }}'
        - condition: template
          value_template: '{% set n = now() %} {{ n.month == ''6'' }}'
        - condition: template
          value_template: '{% set n = now() %} {{ n.month == ''9'' }}'
        - condition: template
          value_template: '{% set n = now() %} {{ n.month == ''10'' }}'

Conditions are “and” by default, so there is no need for to use “and” in this case. Since you are going to use templates, you might as well compact it all into one…

alias: Energy Offpeak
description: ''
trigger:
  - platform: time
    at: '18:00:00'
condition:
  - condition: template
    value_template: '{{ (now().weekday() <= 4) and (now().month in [5, 6, 9, 10]) }}'
action:
  - service: utility_meter.select_tariff
    data:
      tariff: offpeak
    target:
      entity_id:
        - utility_meter.daily_energy
        - utility_meter.monthly_energy
mode: single

Reduce it even further by adopting shorthand notation and eliminating the parentheses. If you wish, use isoweekday() to adopt day numbering that begins at 1 (Monday) and ends at 7 (Sunday).

condition: '{{ now().isoweekday() <= 5 and now().month in [5, 6, 9, 10] }}'