Monthly automation

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

OK thanks, but i want to copy a sensor linke this:

- 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: "kWh"
    state: "{{ iif (states('sensor.ccu3_webui_sv_stromzahler_verbrauch_gestern'), states('sensor.ccu3_webui_sv_stromzahler_verbrauch_gestern'), 0) }}"
    unique_id: XXXXX

what does it have to look like for a week and the last day of the week

You really don’t: there are several errors in that code.

You mean Sunday? This holds prior state until it’s triggered on a Sunday. There are no condition blocks in trigger-based template sensors. Did you dream that up, or is it a ChatGPT hallucination?

- trigger:
    - platform: time
      at: "23:59:59"
  sensor:
    - name: Updates at end of each Sunday
      state: >
        {% if now().weekday() == 6 %}
          INSERT NEW STATE TEMPLATE HERE
        {% else %}
          {{ this.state }}
        {% endif %}

Alternatively, and less efficiently, you could use a template trigger which will only trigger at 23:59, not at the very last second:

- trigger:
    - platform: template
      value_template: "{{ (now().weekday(), now().hour, now().minute) == (6,23,59) }}"
  sensor:
    - name: Updates at end of each Sunday
      state: >
        INSERT NEW STATE TEMPLATE HERE