Previous month's energy usage

I’m working on my energy dashboard and one of the things I want there is previous month energy usage.
I am using energy meter so I am thinking about using a input number that is then switched to a sensor and using automation that will run at the last day of each month at 23;59 to copy that data (similar to what was achieved here: https://community.home-assistant.io/t/yesterday-energy-consumption-in-esphome/259765/2
How do I tell home assistant to run automation at the last day of each month at 23:59?
Any pointers are greatly appreciated.
Thanks

1 Like

Time trigger at 23:59 every day.

trigger:
  platform: time
  at: '23:59'

Template condition to check if tomorrow is the first day of the month.

condition:
  - "{{ (now() + timedelta(days=1)).strftime('%d') == 1 }}"

Action: store your value.

Alternatively if you want to skip the input_number intermediary and store the value directly in the sensor you can use the new triggered sensors. You just have to put the condition in the trigger (triggered sensors don’t support conditions).

template:
  - trigger:
      platform: template
      value_template: "{{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).strftime('%d') == 1 }}"
    sensor:
      - name: 'Energy Last Month`
        unit_of_measurement: 'kWh'
        icon: mdi:counter
        state: "{{ states('sensor.your_energy_meter') }}"

Just a minor correction.

.strftime('%d') == 1 

Should be:

.strftime('%-d') == 1 

Took me a while to figure this out, but strftime() returns a string, not an integer. Therefore the correct code should include ‘’ around the value, otherwise it always returns “false”:

"{{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).strftime('%-d') == '1' }}"

Batts

And a more elegant solution again for the last day of the month component, thanks to @Troon, is

"{{ (now() + timedelta(days=1)).day == 1 }}"

so

"{{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).day == 1 }}"

Hello sorry to ask this but where would I put this trigger template in the configuration files

configuration.yaml.

my entire configuration.yaml. throws errors when I paste your code into it

You already have a template: include defined in that file. You need to put it template.yaml, without the first line template: and move everything left to the page margin.

Also it is using old syntax (though still valid). Using the newly introduced syntax it would be:

template: # remove this line as you are using includes
  - trigger:
      trigger: template
      value_template: "{{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).strftime('%d') == 1 }}"
    sensor:
      - name: 'Energy Last Month`
        unit_of_measurement: 'kWh'
        icon: mdi:counter
        state: "{{ states('sensor.your_energy_meter') }}"

Also I can guarantee you don’t have a sensor called sensor.your_energy_meter. You need to change that to your own energy sensor entity id.

A ther we go thangs a bunch, this yaml stuff messes my brain so bad say i got a sensor I made in my currency that works out the cost already is it possible to change it to that sensor and take the unit of measurement out or change it to currency

Yes you can do that.

- trigger:
    trigger: template
    value_template: "{{ now().hour == 23 and now().minute == 59 and (now() + timedelta(days=1)).strftime('%d') == 1 }}"
  sensor:
    - name: 'Cost Last Month`
      unit_of_measurement: '$' # Or whatever but it must be something if you want to graph it.
      icon: mdi:counter
      state: "{{ states('sensor.your_currency_sensor') }}"