Templating days for utility sensor

Hi,

very new to templating and still struggling to get my head around where and how I can use it. I’ve setup a utility meter, but the offset changes monthly due to our different billing cycles. I’ve tried the following:

utility_meter:
  monthly_energy:
    source: sensor.energy_consumed
    cycle: monthly
    offset:
      days: >
              {% if now().month == 4 %}
                17
              {% elif now().month == 5 %}
                20
              {% elif now().month == 6 %}
                20
              {% elif now().month == 7 %}
                17
              {% elif now().month == 8 %}
                20
              {% elif now().month == 9 %}
                18
              {% elif now().month == 10 %}
                17
              {% elif now().month == 11 %}
                19
              {% elif now().month == 12 %}
                19
              {% elif now().month == 1 %}
                18
              {% elif now().month == 2 %}
                19
              {% elif now().month == 3 %}
                19
              {%- else -%}
                0
              {%- endif %}

I’m getting an error. Just want to know if what I’m trying to do is possible, and if yes what am I missing?

What’s the error message?


I think you may be making an assumption that days: supports templating. My guess is it doesn’t.

I assume so. I was under the impression I can use templates anywhere. I went ahead and programmed it in node red with a link to google calendar. Actually a lot easier and also better to maintain.

You can only template in template areas. Home assistant devs require the field to contain the word template.

No support for templates

In such complex use case it would be better to write an automation to trigger utility_meter.reset service.

That’s what I’m doing now. Is there a way to only reset the meter manually though? Currently I have it on an annual cycle, which means I still have to change the offset days at least once a year.

Currently no… but if you set the automatic yearly reset, lets say to january date, then your automation can just skip that month