History_stats: start at beginning of week

I am searching for a way to get history_stats from this week so far. The documentation (History Stats - Home Assistant) shows an example how to start from today’s day with replacing hour, minute and second with a 0, the same works for this month, since i can also replace the day with a 1. However, the datetime object returned by the now() function does not have a weekday attribute which i could reset. It sure does expose the isoweekday, but this is apparently immutable.

There are some suggestions to use datetime methods (Python: give start and end of week data from a given date - Stack Overflow) but apparently the datetime module is not accessible from templates.

Has anybody a suggestion how i could set the start of the history_stats sensor to the beginning of the week (i.e. monday morning at 00:00)?

Isn’t this the day of the week?

now().strftime('%A')

Make the History Stats sensor report daily stats. Then use the Utility Meter integration to report weekly stats, using the data produced by the daily History Stats sensor.

The advantage is the data for sensors produced by the Utility Meter integration is stored long-term in the database (i.e. it’s not purged regularly). Plus you can also use the Utility Meter integration to create monthly and yearly sensors.

Very good idea. Thanks for that. Indeed i want to create daily, weekly and monthly stats.

1 Like