Automation with if/else service selection

My electrical utility provider has time-of-use pricing that changes based on the season. I am trying to write an automation to capture this, but cannot get it to work. Would anyone be able to offer some guidance?

- trigger:
    - platform: time
      at: '07:00:00'
  condition:
    condition: state
    entity_id: 'binary_sensor.workday_sensor'
    state: 'on'
  action:
    - service: >
        utility_meter.select_tariff
        data:
          entity_id: utility_meter.daily_energy
            {% set month = now().month %}
            {% if month >= 5 and month <= 10 %}
          tariff: midpeak
            {% else %}
          tariff: onpeak
            {% endif %}
trigger:
  - platform: time
    at: '07:00'
condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
action:
  - service: utility_meter.select_tariff
    target:
      entity_id: utility_meter.daily_energy
    data:
      tariff: >-
        {{'midpeak' if 5 <= now().month <= 10 else 'onpeak'}}
1 Like

Very elegant. Thank you!

Don’t forget to mark ardysusilo’s helpful post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.