History_stats for specific daily time window

I want to create a sensor that gives me the total time a binary_sensor was in the on state during a specific window each day.

For context, I initially want to use this as a reminder to turn on the dishwasher at bedtime if it wasn’t run since dinner that day. i.e. @bedtime: notify if (dishwasher usage == 0) since 5PM today

On the history_stats documentation page, it mentions these examples:

This morning (6AM - 11AM) : starts today at 6, lasts 5 hours.

start: '{{ now().replace(hour=6, minute=0, second=0) }}'
duration:
  hours: 5

Today : starts at 00:00 of the current day and ends right now.

start: '{{ now().replace(hour=0, minute=0, second=0) }}'
end: '{{ now() }}'

I’ve tried variants of both of those, and neither seems to work - I get a value of Unknown for the sensor, and that never changes.

Here’s what I’ve tried:

Daily Dishwasher Usage (works, but this is for the whole day)

- platform: history_stats
  name: Dishwasher Usage Today
  entity_id: binary_sensor.dishwasher_in_use
  state: 'on'
  type: time
  start: '{{ now().replace(hour=0, minute=0, second=0) }}'
  end: '{{ now() }}'

After-Dinner Dishwasher Usage (5PM today to Midnight today; doesn’t work)

- platform: history_stats
  name: Dishwasher Usage After Dinner 2
  entity_id: binary_sensor.dishwasher_in_use
  state: 'on'
  type: time
  start: '{{ now().replace(hour=17, minute=0, second=0) }}'
  end: '{{ now().replace(hour=23, minute=59, second=59) }}'

After-Dinner Dishwasher Usage (5PM today to now; doesn’t work)

- platform: history_stats
  name: Dishwasher Usage After Dinner 3
  entity_id: binary_sensor.dishwasher_in_use
  state: 'on'
  type: time
  start: '{{ now().replace(hour=17, minute=0, second=0) }}'
  end: '{{ now() }}'

After-Dinner Dishwasher Usage (7 hour duration, starting at 5PM today; doesn’t work)

- platform: history_stats
  name: Dishwasher Usage After Dinner 4
  entity_id: binary_sensor.dishwasher_in_use
  state: 'on'
  type: time
  start: '{{ now().replace(hour=17, minute=0, second=0) }}'
  duration:
    hours: 7

After-Dinner Dishwasher Usage (start at either now (if before 5PM) or 5PM today (if after 5PM) and end at now; seems to work so far?)

- platform: history_stats
  name: Dishwasher Usage After Dinner 5
  entity_id: binary_sensor.dishwasher_in_use
  state: 'on'
  type: time
  start: "{% if strptime(now(), '%H:%M:%S').strftime('%H')|int < 17 %} {{ now() }} {% else %} {{ now().replace(hour=17, minute=0, second=0) }}  {% endif %}"
  end: '{{ now() }}'

The last one gives me a value, and I’ll see later today if it updates correctly in the evening. It seems overly complicated though. Is there a better way?

Click on the sensor to show the more info pop-up. What do the from and to values show for the configurations you found not to work?

How do you get the to/from values to show up?

Here’s what I see for one of the non-working sensors:
image

And here’s what I see for one of the working sensors:
image

If I look at the sensor state in Developer Tools -> states, I don’t see any to/from values, either.

Well that wasn’t what I was expecting from this sentence at the bottom of the docs you linked to:

If you want to check if your period is right, just click on your component, the from and to attributes will show the start and end of the period, nicely formatted.

What you were doing looks correct to me. I’d open an issue:

So the day after I couldn’t get some of the above the history_stats sensors to work, they magically started working.

I think the key was that the in_use sensor had not yet turned on since the creationof the history_stats sensor, so the history_stats sensor stayed in the Unknown state. Once I actually used the dishwasher and the in_use sensor turned on, the history_stats sensor seemed to operate normally, even after transitioning to a new day where the appliance hasn’t yet been used.