Monthly automation

I’m really enjoying this misspelling. I’m imagining you (or your wife, or maybe actually you after she figures out it’s actually your turn) running away after giving the first med…

{{ now().month > 4 and now().month < 10 }}
2 Likes

Hi,
This is my auto restart yaml code that runs at 7:45 on the 15th of every month.

alias: Home Assistant - Auto Restart
description: ''
trigger:
  - platform: time_pattern
    minutes: /15
condition:
  - condition: template
    value_template: >-
      {{  now().strftime('%M')|int == 45 and now().strftime('%H')|int == 7 and now().strftime('%d')|int == 15 }}
action:
  - service: hassio.host_reboot
    data: {}
mode: single
1 Like

Given that you know the exact time when it should trigger, use a Time Trigger.

alias: Home Assistant - Auto Restart
description: ''
trigger:
  - platform: time
    at: '07:45:00'
condition:
  - condition: template
    value_template: "{{ now().day == 15 }}"
action:
  - service: hassio.host_reboot
    data: {}
mode: single
2 Likes

You can also create a local calendar with a reoccurring event. After this you can create an automation to trigger on these events to take the desired actions.

Thanks for all the above!

I do some calculations on my monthly power draw and would like to trigger an automation on the “last” day of each month.

I could envision 12 separate schedule helpers but was hoping for a cleaner solution. Even with 12 schedules I couldn’t handle leap years properly (and while I could live with this - it would always bother me that it wasn’t quite right :slight_smile:

I need to run this at 23:59 on the last day of the month, because I am using a utility helper with monthly reset in my calculation that will reset itself to zero at midnight. (Otherwise I’d run it at 00:01 on the first day of the month and use a solution such as those posted above)

Any thoughts?

you should try it like this:

(Trigger for the last day of the month - #30 by Troon)

1 Like

Thanks!! Nice and clean!

The “best” way to do that would be a time trigger and a template condition, thus:

trigger:
  - platform: time
    at: "23:59:00"
condition:
  - "{{ (now() + timedelta(days=1)).day == 1 }}"
action:
# etc

That triggers once per day, only running the action on the last day of the month.

If you were to use the longer template in a template trigger, that would be evaluated every minute, 1440 times more “wastefully” than the alternative above.

Agreed, that’s actually how I did set it up - and for that reason. Thanks for the confirmation!

1 Like

I’m new here, but was inspired by this and something I read about Trigger Ids and took it down a rabbit hole. I’ve got a bad habit of forgetting to give the dogs their meds, so this is a way to annoy me into submission.

Basically I set one of the leds on my Homeseer ZWave switch to red at 5AM. If it’s still red by 5PM that same day I get push notifications once per hour (in perpetuity) until it’s no longer red. Disabling the red led is in a different rule.

First stab at a rule this complex, let me know if there’s something I missed to make things easier!

alias: When it's the 11th of the month Then remind me to give the dogs their meds
description: ""
trigger:
  - platform: time
    at: "05:00:00"
    id: morning_alarm
  - platform: time_pattern
    hours: "*"
    id: annoy_me
condition:
  - condition: or
    conditions:
      - alias: For morning alarm
        condition: and
        conditions:
          - condition: trigger
            id:
              - morning_alarm
          - alias: Check that it's the 11th
            condition: template
            value_template: "{{ now().day == 11 }}"
      - alias: For annoy me
        condition: and
        conditions:
          - condition: trigger
            id:
              - annoy_me
          - alias: After 5 on the 11th or any time any other day
            condition: or
            conditions:
              - alias: After 5:00 PM on the 11th
                condition: and
                conditions:
                  - alias: On the 11th
                    condition: template
                    value_template: "{{ now().day == 11 }}"
                  - condition: time
                    after: "16:59:59"
              - alias: Not on the 11th
                condition: template
                value_template: "{{ now().day != 11 }}"
          - condition: device
            device_id: 4434b4609c62ab27f6a89fd296199cf5
            domain: zwave_js
            value_id: 40-112-0-13
            type: config_parameter
            subtype: 13 (Enable / Disable Custom LED Status Mode) on endpoint 0
            value: 1
          - condition: device
            device_id: 4434b4609c62ab27f6a89fd296199cf5
            domain: zwave_js
            value_id: 40-112-0-27
            type: config_parameter
            subtype: 27 (Status LED 7 Color) on endpoint 0
            value: 1
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - morning_alarm
        sequence:
          - service: notify.mobile_app_pixel_7_pro
            data:
              message: Give the dogs their meds!
          - device_id: 4434b4609c62ab27f6a89fd296199cf5
            domain: zwave_js
            type: set_config_parameter
            endpoint: 0
            parameter: 13
            bitmask: null
            subtype: 13 (Enable / Disable Custom LED Status Mode) on endpoint 0
            value: 1
          - device_id: 4434b4609c62ab27f6a89fd296199cf5
            domain: zwave_js
            type: set_config_parameter
            endpoint: 0
            parameter: 27
            bitmask: null
            subtype: 27 (Status LED 7 Color) on endpoint 0
            value: 1
        alias: Set the status light
      - conditions:
          - condition: trigger
            id:
              - annoy_me
        sequence:
          - service: notify.mobile_app_pixel_7_pro
            data:
              message: Give the dogs their meds!
        alias: Annoy Me

I want to save a value in an inputnumber_month at the end of a month

Can I make something so the result woud be input_number_opbrengst_JANUARI

{{ (mon[now().month-1])}}

entity_id input_number.electriciteit_opbrengst_{{ (mon[now().month-1])}}

On the assumption that entity_id can accept templates:

action:
  - service: input_number.set_value
    target:
      entity_id: input_number.electriciteit_opbrengst_{{ now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower }}
    data:
      value: YOUR_VALUE

That will only work if you have already created the 12 input_number helpers:

input_number.electriciteit_opbrengst_januari
...
input_number.electriciteit_opbrengst_december

I believe timestamp_custom('%B') will return the English month name regardless of locale, so the sequence of replaces turns those into the Dutch month names.

1 Like

I already did. thanks I will try that

alias: Electriciteit_verbruik_maandopslag
description: ""
trigger:
  - platform: template
    value_template: "{{(now() + timedelta(days=1)).strftime('%-d') == '1'}}"
  - platform: time
    at: "23:59:40"
action:
  - service: input_number.set_value
    target:
      entity_id: >-
        input_number.electriciteit_opbrengst_{{
        now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower
        }}
    data:
      value: >-
        {{ states("sensor.export_stroom_maand") | float
        }}    
mode: single

when I save this, I always get

    data:
      value: "{{ states(\"sensor.export_stroom_maand\") | float }}    "

what do I do wrong ?

Nothing: that should still work. It’s just the UI reformatting the template to an alternative layout.

If you manually run the automation, it should set the January input_number to the current value of that sensor. If not, post what happens here with any relevant log entries.

TemplateSyntaxError: unexpected char ‘\’ at 546

I think it has to be

    data:
      value: {{ states("sensor.export_stroom_maand") | float }}

that gives me
data:
value: 29.44

but when I save and reopen I get

    data:
      value:
        "[object Object]": null

Are you using the automation UI “edit as YAML” to save this? Try single quotes:

    data:
      value: "{{ states('sensor.export_stroom_maand') | float }}"

when I run it, it gives me the right value. strange.

Thanks for helping me

Hi all,

ist this correct for the last day of the month?

- trigger:
    platform: time
    at: "23:59:59"
    condition:
      {{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).strftime('%-d') == '1' }}
  sensor:
    name: "Strom_Verbrauch_vorgestern"
    unit_of_measurement: "m3"
    state: "{{ iif (states('sensor.ccu3_webui_sv_stromzahler_verbrauch_gestern'), states('sensor.ccu3_webui_sv_stromzahler_verbrauch_gestern'), 0) }}"
    unique_id:

No.

- trigger:
  - platform: template
    value_template: >
      {% set midnight = today_at() + timedelta(days=1) %}
      {% set t = now().replace(microsecond=0) %}
      {{ t.time() == midnight.time() and midnight.day == 1 }}

that’s exactly midnight on the last day of each month.

1 Like