Condition value_template not working, always true

Hello, I have a helper entity for the cost per kwh, but being on a time of use I need an automation to change for three periods per day and also based on Summer or Winter rates.
I can do this by creating 6 automations, but I would like to streamline by creating only 3 and putting the season logic inside the automation.
My problem is the condition logic does not seem to work, i.e. both the summer and winter version pass the condition as true and sets the value.

- id: "1683917535625"
  alias: kwh_cost_4pm_winter
  description: ""
  trigger:
    - platform: time
      at: "16:00:00"
  condition:
    - condition: template
      value_template: >
        {% set n = now() %}
        {{ n.month == 10 or n.month == 11 or n.month == 12 or n.month == 1 or n.month == 2 or n.month == 3 or n.month == 4 or n.month == 5}}
  action:
    - service: input_number.set_value
      data:
        value: 0.57
      target:
        entity_id: input_number.kwh_cost

- id: "1683922688113"
  alias: kwh_cost_4pm_summer
  description: ""
  trigger:
    - platform: time
      at: "16:00:00"
  condition:
    - condition: template
      value_template: >
        {% set n = now() %}
        {{ n.month == 6 or n.month == 7 or n.month == 8 or n.month == 9 }}
  action:
    - service: input_number.set_value
      data:
        value: 0.64
      target:
        entity_id: input_number.kwh_cost
  mode: single

Your second condition template evaluates as false on my system, as I’d expect. If you’re triggering it manually, the condition is ignored. How about one automation:

- id: "1683922688113"
  alias: kwh_cost_4pm
  trigger:
    - platform: time
      at: "16:00:00"
  action:
    - service: input_number.set_value
      data:
        value: "{{ 0.64 if (5 < now().month < 10) else 0.57 }}"
      target:
        entity_id: input_number.kwh_cost

Explain the full requirements and we may be able to advise a better solution still.

1 Like

@Troon, wow that was fast and very simple logic. Yes I did not realize that triggering it manually ignores the condition. If have changed my logic.
With your combining of summer and winter I now have One trigger for 4:PM and one for 9:PM. and then also another two for the weekend. I will use this added condition to separate weekend and weekdays.

  - condition: time
    weekday:
      - sat
      - sun

My use case is to switch the entity kwh_cost to the current rate based on these times/cost. Winter is easy as weekends and weekdays are the same.

winter_rates

Can I nest the if statement inside the value clause and then combine weekends?

Here you go — as an automation still, but I’d be inclined to turn it into a template sensor unless you need the manual editability of the input number:

- id: "1683922688113"
  alias: kwh_cost
  trigger:
    - platform: time
      at:
        - "08:00:00"
        - "16:00:00"
        - "21:00:00"
    - platform: homeassistant
      event: start
  action:
    - service: input_number.set_value
      data:
        value: >
          {% set tariff = (0.22, 0.57) if (now().month <= 5 or now().month >= 10)
                     else (0.24, 0.37) if now().weekday() >= 5
                     else (0.24, 0.62) %}
          {{ tariff[16 <= now().hour < 21] }}
      target:
        entity_id: input_number.kwh_cost

The only non-obvious bit (I hope) is the indexing in the final line of the template. It’s a bit of a shortcut where false evaluates to 0 and true to 1, so it’s equivalent to:

{{ tariff[1 if 16 <= now().hour < 21 else 0] }}

Here it is as a trigger-based template sensor (sensor.kwh_cost). You don’t need the trigger: it’s just to avoid it evaluating every minute unnecessarily:

template:
  - trigger:
      - platform: time
        at:
          - "08:00:00"
          - "16:00:00"
          - "21:00:00"
    sensor:
      - name: kwh_cost
        state: >
          {% set tariff = (0.22, 0.57) if (now().month <= 5 or now().month >= 10)
                     else (0.24, 0.37) if now().weekday() >= 5
                     else (0.24, 0.62) %}
          {{ tariff[16 <= now().hour < 21] }}
        unit_of_measurement: "$/kWh"

It also doesn’t really need the 08:00 trigger as none of the rates you gave changes at that time. What’s the green block for on winter 08:00 to 16:00? Is this special behaviour for the changeover from summer to winter? What’s the rate at 04:00 on the first day of October?

Again thanks, I moved it from an automation to a template sensor.

1 Like