How to execute monthly via my.yaml

Hello community,
I have a problem that I can’t solve. I would like to use my.yaml to write the values of two counters to a variable on the first day of every month. I’ve already managed to do this for daily, but now I have no idea how to implement it for monthly.
During my search, I came across the variable “value_template: ”{{ now().day == 1 }}" and have already tried a few things, but it always gets rejected or doesn’t work.


template:
# Daily
  - trigger:
      - trigger: time_pattern
        hours: 0
        minutes: 0
    sensor:
      - name: "netzbezugscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_imported_real_energy") }}
      - name: "netzeinspeisungscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_exported_real_energy") }}
# Monthly
      - trigger: time_pattern
        hours: 0
        minutes: 0
        value_template: ”{{ now().day == 1 }}"
    sensor:
      - name: "monatsnetzbezugscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_imported_real_energy") }}
      - name: "monatsnetzeinspeisungscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_exported_real_energy") }}

Thanks for your help.

The Daily one is mostly okay, but the Monthly’s trigger is a mess…

Issues:

  • No triggers key to delineate a new list item for the Monthly sensor
  • The trigger is an invalid mix of Time Pattern and Template
  • “Fancy” quotes

Be aware that the Monthly sensors’ states will be “unknown” until the trigger fires.

template:
# Daily
  - triggers:
      - trigger: time_pattern
        hours: 0
        minutes: 0
    sensor:
      - name: "netzbezugscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_imported_real_energy") }}
      - name: "netzeinspeisungscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_exported_real_energy") }}
# Monthly
  - triggers:
      - trigger: template
        value_template: "{{ now().day == 1 }}"
    sensor:
      - name: "monatsnetzbezugscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_imported_real_energy") }}
      - name: "monatsnetzeinspeisungscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_exported_real_energy") }}

Thank you very much for your support.
What would you change in the daily configuration?

Is the action only performed once at midnight on the 31st, or does it repeat throughout the day?

- triggers:
      - trigger: template
        value_template: "{{ now().day == 31 }}"

The contemporary syntax is:

template:
  - triggers:
      - trigger: ....

Note the s on triggers, which was missing in your original.

The trigger will only fire when the value of now().day changes from not 31 to 31.

If you are more comfortable using a Time Pattern or Time trigger, the same thing could be done by adding a condition to one of those as follows:

# Monthly
  - triggers:
      - trigger: time
        at: "00:00:00"
    conditions:
      - condition: template
        value_template: "{{ now().day == 1 }}"
    sensor:
      - name: "monatsnetzbezugscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_imported_real_energy") }}
      - name: "monatsnetzeinspeisungscounter_mitternacht"
        state: >
          {{ states("sensor.modbus_total_exported_real_energy") }}

Thank you very much, it works.