Yesterday, last month, last year energy usage with history_stats

Hey, i have a hard time figuring out how to use history_stats for my kwh energy usage.

My smart plugs report current watt usage and all-time kwh usage, i created a helper that combines the all-time kwh usage of 3 smart plugs into one entity.

With this combined kwh entity i create utility_meters for today, current month and current year. No problem so far.

The problem is with history_stats, as you can see in the title i want yesterdays, last months and last years energy usage in kwh, i tried creating something like this:

- platform: history_stats
  name: aquarium_rack_power_usage_kwh_yesterday
  entity_id: sensor.combined_kwh_aquarium_rack
  state: '???'
  type: time
  start: '{{ now().replace(hour=0, minute=0, second=0) - timedelta(days=1) }}'
  end: '{{ now().replace(hour=0, minute=0, second=0) }}'

As the state of sensor.combined_kwh_aquarium_rack is the kwh number that is always increasing i do not know what to put in the history_stats state variable…

If you have utility meters you will note there’s already an attribute for the last period. So, for a daily cycle utility meter you’ll have values for today and yesterday.

No my utility meter has only one entity and this is the daily one, so i don’t really know what you mean.

You said this:

Do you not then have these utility meters?

If not, make a utility meter for each cycle that you want (daily, monthly, etc.).

Note what I said:

Or using a template:

{{ state_attr('sensor.daily_home_energy_consumption', 'last_period') }}