Trigger for the last day of the month

Is there a time template trigger for the last day of the month?
I would like to set up an automation that sends me a notification with the current status of a sensor every month at the end of the month. The sensor tracks the total amount of time a switch has been on for that month but if I forget to check it on the last of the month the sensor resets and there not way for me to view the previous data

Check if tomorrow is the first day of the month by adding 24hrs to today’s date.

Here’s a binary sensor that will be active on the last day of the month. You will also have to create a date sensor for this to work, if you don’t have one already. This is because now() is a function and will not update the template. By including the entity_id sensor.date it updates whenever the date changes.

For the same reason this template can not be used directly as a template trigger in an automation. Someone might have a way to do that, but this is the easiest way I’ve learned so far. Also sometimes it’s handy to have a sensor you can actually check the state of.

binary_sensor:
  - platform: template
    sensors:
      last_day_of_the_month:
        friendly_name: 'Last Day of the Month'
        entity_id: sensor.date
        icon_template: "mdi:calendar"
        value_template: "{{ (as_timestamp(now()) + 86400)|timestamp_custom('%d', true) == 01 }}"

For your automation use a time trigger for the time you would like to be sent the message (e.g. "09:00:00"). Then add a condition that binary_sensor.last_day_of_the_month must be 'on'.

8 Likes

I don’t know why this never notified me…
That is brilliant though.
I just set it up and it appears to be working but the last day of the month sensor wont update to on.
I am guessing that is because it is currently the last day of the month and this requires the date to change in order to update. So I manually set the state to on and built the automation and it works great. Sends me a mobile app notification with the current amount of hours that my Boiler has run for the month. I just set it for 10:00 PM because the likely hood my boiler will run much after 10PM is slim.

So what does : -

{{ (as_timestamp(now()) + 86400) | timestamp_custom('%d', true) }} 

Give you in the template editor today (the last day of this month ?
And how are you using the binary_sensor in your trigger ?
Post both ‘your’ sensor and ‘your’ automation.

Returns an “01”

Here is the Automation, I started in the GUI and finished the template for the boiler sensor iun YAML

- id: '1590936323616'
  alias: Notify Me of Boiler Run Time
  description: ''
  trigger:
  - at: '22:00'
    platform: time
  condition:
  - condition: state
    entity_id: binary_sensor.last_day_of_the_month
    state: 'on'
  action:
  - data: {}
    data_template:
      message: '{{states.sensor.boiler_time.state}}'
      title: Boiler Run Time
    service: notify.mobile_app_pixel_3

And here is the Binary Sensor, basically a copy and paste from above, I created the appropriate date sensor

#last Day of the month Sensor
  - platform: template
    sensors:
      last_day_of_the_month:
        friendly_name: 'Last Day of the Month'
        entity_id: sensor.date
        icon_template: "mdi:calendar"
        value_template: "{{ (as_timestamp(now()) + 86400)|timestamp_custom('%d', true) == 01 }}"

I think it probably would have worked if I configured it yesterday haha

1 Like

I got it, just had to single quote the 01 in the Binary sensor.

#last Day of the month Sensor
  - platform: template
    sensors:
      last_day_of_the_month:
        friendly_name: 'Last Day of the Month'
        entity_id: sensor.date
        icon_template: "mdi:calendar"
        value_template: "{{ (as_timestamp(now()) + 86400)|timestamp_custom('%d', true) == '01' }}"

Not actually working on this at the moment (give me a couple of hours) but…
Your sensor is wrong, look at : -


The first line is ‘sensor:’ (actually binary_sensor: for this sensor)
Then you list your platforms.

Just to be sure, you can only have one sensor: header unless you use packages (if you don’t know what they are, don’t worry about it)

Nope, I’ve come back to this and the missing binary_sensor: heading is the only thing that it could be, AND that could be that you just didn’t include it when you pasted it into the box above.
I set the sensor as you have it (except to now say == ‘02’ as today is the first) and it returns True.
Your icon template is a bit unusual but should work as Tom suggests, as it returns the string value resolving to an mdi Icon
Your automation checks every second for a time match to 22:00 when it actually triggers. The automation should fire when the day +1 == 01 and that should send your message.
All looks good to me (though I’d modify the trigger to reduce evaluations (by 60 fold) with : -

    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == '22:00' }}"

but it’s a trifling distinction.

I know this may be a bit old, but I trying to trigger a SMS message at 10:00 on the 1st day of every month that sends me the rainfall monthly and yearly totals. How can I do it? I am just getting started with home assistant and scripts and this seems to be a tricky thing, at least for me. I have a working trigger for 10:00, but I can’t seem to figure out how to get the date trigger right.

Thanks

You can add a Template Condition that checks if the current day is equal to 1.

  condition:
  - condition: template
    value_template: "{{ now().day == 1 }}"
4 Likes

THANK YOU!! I have spent all afternoon chasing this one.
It would seem to me though it would be better to check for the correct day first and then check the time second. Or does it matter?

What will happen is that moments after midnight, when one day changes to the next, the trigger will check the day. If it’s the first day of the month, it proceeds to the condition to check the time where it’s not 10:00 yet (and the action is not executed). The trigger won’t be triggered again until the next day (or if you restart Home Assistant or Reload Automations).

So even if the time is the trigger, it checks the date first? It seems that it would be checking for the time, and then go and check for the correct date.

  • id: ‘1608490217222’
    alias: Sens SMS message of total rain fall on a schedule
    description: ‘’
    trigger:
    • platform: time
      at: ‘10:00:00’
      condition:

    • condition: template
      value_template: “{{ now().day == 1 }}”
      action:

    • service: notify.mobile_app_sm_g950u
      data:
      title: Home Assistant Weather report!
      message: 'It has rained a total of {{ states (“sensor.monthly_rain”)}}
      inches last month. It has rained a total of {{ states (“sensor.yearly_rain”)}}
      inches for the year!

      '
      
    mode: single
1 Like

Originally you asked this question:

That’s what my explanation addressed (i.e. it would not be better to reverse the order of what gets checked first; leave it as originally discussed where the time is checked first).

Thanks Again!

Found this thread - it looks its still an issue to find exactly last day of a month.

I created a template sensor:

last_day_of_month:
      value_template: >-
        {{ 
        31 if now().month in (1,3,5,7,8,10,12) else 
          30 if now().month in (4,6,9,11) else 
            29 if now().month == 2 and now().year % 4 == 0 else 28
        }}

and then in trigger compare:

    condition:
      - condition: template
        value_template: "{{ now().day == states('sensor.last_day_of_month') }}"

one sensor value can be used for many triggers.

1 Like

No, this binary sensor worked, Trigger for the last day of the month - #6 by Arsenal10108

And it does not require a comparison template wherever you want to use it, just a state condition or trigger.

Like tom_I said, it was solved in post #6 a year ago.

A year is a long time in Home Assistant’s evolution. Here is the same binary_sensor but in the new ‘modern’ format with a refactored template that takes advantage of the timedelta function.

# Last Day of the Month Binary Sensor
template:
  - binary_sensor:
      - name: Last Day of the Month
        icon: mdi:calendar
        state: "{{ (now() + timedelta(days=1)).day == 1 }}"
11 Likes

This will break on 2100-02-29.

3 Likes

Right :slight_smile: For immortals it will be:

      value_template: >-
        {{ 
        31 if now().month in (1,3,5,7,8,10,12) else 
          30 if now().month in (4,6,9,11) else 
            29 if now().month == 2 and now().year % 4 == 0 and (now().year % 100 != 0 or now().year % 400 == 0) else 28
        }}

Expression {{ (now() + timedelta(days=1)).day == 1 }} looks clean and nice if you just need True or False. Thanks!