Automation - Can not use "enabled" with some code

Hi! First of all this my first attempt to do something advanced on HASS! I’m totally newby, so please forgive my ignorance.

I’m trying to configure multiple tariffs in energy metering using “Utility Meter”.

First I created some helpers to enter the data.

Then I created a template to find if this time, peak or off-peak charges are used:

{{ state_attr('input_datetime.winter_off_peak_period_start', 'month') <= now().month <= state_attr('input_datetime.winter_off_peak_period_end', 'month') }}

It seems to working fine.

Then I created an automation to set the tariff:

alias: HEDNO Winter tariff
description: Winter Tariff Selection
trigger:
  - platform: time
    at: input_datetime.winter_off_peak_noon_start
    variables:
      tariff: offpeak
    enabled: is_state('binary_sensor.hedno_winter_season', 'on')
  - platform: time
    at: input_datetime.winter_off_peak_noon_end
    variables:
      tariff: peak
    enabled: is_state('binary_sensor.hedno_winter_season', 'on')
  - platform: time
    at: input_datetime.winter_off_peak_morning_start
    variables:
      tariff: offpeak
    enabled: is_state('binary_sensor.hedno_winter_season', 'on')
  - platform: time
    at: input_datetime.winter_off_peak_morning_end
    variables:
      tariff: peak
    enabled: is_state('binary_sensor.hedno_winter_season', 'on')
  - platform: time
    at: input_datetime.summer_off_peak_start
    variables:
      tariff: offpeak
    enabled: is_state('binary_sensor.hedno_winter_season', 'off')
  - platform: time
    at: input_datetime.summer_off_peak_end
    variables:
      tariff: peak
    enabled: is_state('binary_sensor.hedno_winter_season', 'off')
action:
  - service: notify.notify
    metadata: {}
    data:
      message: Tariff changed to {{ tariff }}
mode: single

The problem is that all of the above are like they have the value True on "enabled: " which is wrong.

is_state(‘binary_sensor.hedno_winter_season’, ‘on’) is returning False in Developer Tools / Template.

I have also used:

enabled: "{{ is_state('binary_sensor.hedno_winter_season', 'off') }}"

with the same result.

What am I missing? And how can we debug automation triggers in general?

Thank you

this is the right format:

but this is from the docs:

you could try this format:

alias: HEDNO Winter tariff
description: Winter Tariff Selection
trigger:
  - platform: time
    at: input_datetime.winter_off_peak_noon_start
    variables:
      tariff: offpeak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'on') }}
  - platform: time
    at: input_datetime.winter_off_peak_noon_end
    variables:
      tariff: peak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'on') }}
  - platform: time
    at: input_datetime.winter_off_peak_morning_start
    variables:
      tariff: offpeak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'on') }}
  - platform: time
    at: input_datetime.winter_off_peak_morning_end
    variables:
      tariff: peak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'on') }}
  - platform: time
    at: input_datetime.summer_off_peak_start
    variables:
      tariff: offpeak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'off') }}
  - platform: time
    at: input_datetime.summer_off_peak_end
    variables:
      tariff: peak
      should_enable: |
        {{ is_state('binary_sensor.hedno_winter_season', 'off') }}
condition:
  - condition: template
    value_template: "{{ should_enable }}"
action:
  - service: notify.notify
    metadata: {}
    data:
      message: Tariff changed to {{ tariff }}
mode: single

While my setup is different, the approach may be of interest. Instead of having a dozen different helpers, just put it all into a single template sensor, then you can just code up your various dates/times/rates as needed and the tariff will be whatever is current as of then. It’s worked perfectly for me, but I’m not sure if this is what you are looking for.

sensor:
  - name: "Current Energy Tariff"
    icon: mdi:currency-usd
    unit_of_measurement: "USD/kWh"
    state: >-
      {% set tariff = { "OFFPEAK": 0.11, "MIDPEAK": 0.19, "ONPEAK": 0.27 } %}
      {% set dow = now().strftime('%A') %}

      {% if dow == "Saturday" or dow == "Sunday" %}
        {{ tariff.OFFPEAK }}
      {% else %}
        {% if (now().hour >= 15) and (now().hour <= 19) %}
          {{ tariff.ONPEAK }}
        {% elif (now().hour >= 13) and (now().hour <= 15) %}
          {{ tariff.MIDPEAK }}
        {% else %}
          {{ tariff.OFFPEAK }}
        {% endif %}  
      {% endif %}

Then I use this sensor in my Energy dashboard to keep a running tab of my power bill.

Thank you all for your help, I will see it with the first opportunity!

Hi! Your suggestion didn’t work. It’s not running at all.

your code is different than mine… you put in quotes on all the templates. that wasn’t there in mine. so you effectively made every “enabled:” value to be true. to clarify the issue wrt the quotes. if you leave it on the same line as “enabled:” then you must have the quotes. if you use the | as i did and put it on it’s separate line, you MUST NOT quote it. annoying, but that’s what it is.

pls switch it back to my code format and give it a try. if that doesn’t work then please post the whole code using the code formatting (what you did in your original post) and also the download trace (you’ll find it on the upper right of the trace page) for when it didn’t work.

Right. I’m not sure how quotes got there. The value was false because it never ran. Thx!

Why not just put that binary sensor in a state condition?

condition:
  - condition: state
    entity_id: binary_sensor.WHATEVER
    state: 'off'

Please don’t post screenshots of code: use properly-formatted text.

EDIT: having looked more closely at your screenshot, I see it’s not quite that straightforward. You want to:

  • get rid of all the enabled lines
  • add a id called winter to all of the winter triggers
  • add this as the condition block:
condition:
  - "{{ is_state('binary_sensor.hedno_winter_season', 'on' if trigger.id == 'winter' else 'off') }}"

References: template condition shorthand notation.

I restarted HASS and in the core log I see the error:

2024-07-11 21:50:37.278 ERROR (MainThread) [homeassistant.components.automation.hedno_winter_tariff] Error rendering enabled template: TemplateError: Use of 'is_state' is not supported in limited templates HEDNO Winter tariff

repeated 6 times, the same number as the triggers… So is_state() can not be used here.

hmm… well that’s a new learning for me… you have the format right, but looks like the templates in triggers are a subset of normal templates. here’s more info about it:

i think the solution then is to move the check into the condition block as @Troon was saying and use a state condition.

Ok, I will try what Tron suggested and I will return with feedback. Thx!