Getting history stats starting from last friday

Hi,
in the past HA was tracking the computer time of our son on a daily basis (and after reaching the limit, the computer is shutting down).

Now, we want to change that to a weekly budget. But it shouldn’t start on monday, but on every friday.

I saw the example for shifting to sunday in the documenation:

start: "{{ today_at('00:00') - timedelta(days=now().weekday()) }}"
end: "{{ now() }}"

That is easy, because weekday sunday is zero and the only thing needed is to subtract the weekday. But with friday the weekday will have a jump back to zero on sunday.

What is the easiest way to get the computer time since last friday?

Many thanks in advance!

I’ve tried to get last Friday date, but as it is Saturday, not sure if it will make the job.

{{ (today_at('00:00') - timedelta(days=(now().weekday() + 2) % 7 + 1) ) }}

That gives 2024-10-18 00:00:00+02:00 in my timezone

2 Likes

Monday is 0. Even says so in the example you are referring to.

The whole time I was doing this I thought there must be an easier way with modular arithmetic.

start: >
  {% set weekday = now().weekday() %}
  {% set offset = 3 if weekday < 5 else -4 %}
  {{ today_at('00:00') - timedelta(days = weekday - offset) }}

Looks like Olivier worked that out. I tested his formula in the template editor with all the integer days. It works as long as you want a whole week instead of 0 days on Friday.

Sorry, maybe my brain made monday to 1, because that was how I understood the start.

Let’s say it’s monday, that would mean weekday() is 0. But how does the example work?
Isn’t today_at(‘00:00’) - 0 the same as today and not yesterday?

Many thanks Olivier and Tom for your solution. I went through Oliviers solution with pen and paper. Here it is -1 for Saturday, which feels to be right. But for friday it will be -7 and shouldn’t that be -0?
I guess that is the same misunderstanding as already with the example :slight_smile:

Edit: friday will of course be -7. I forgot to add the +1

It will actually be -7, that’s why I said:

Mine will give you 0.

1 Like

Hi Tom,

is your idea really giving the correct offsets?

start: >
  {% set weekday = now().weekday() %}
  {% set offset = 3 if weekday < 5 else -4 %}
  {{ today_at('00:00') - timedelta(days = weekday - offset) }}

Today we have saturday which is weekday 5. Therefore, it isn’t smaller than 5 and the offset will be set to -4.
5 - (-4) is 9. and should be 1. Changing the timedelta to week + offset will correct it for saturday, but will be incorrect for the other days.

I now ended up with a pragmatic lookup table.

    start: >
      {% set offset = [3, 4, 5, 6, 0, 1, 2] %}
      {{ today_at('00:00') - timedelta(days = offset[now().weekday()]) }}

Thanks a lot! This discussion was extremely helpful for finding a solution.

Maybe not, it’s late here. I like your solution. Very neat.

This would be the modular arithmetic to get you the same answer:

{{ today_at() - timedelta(days=(now().weekday() + 3) % 7) }}
2 Likes