My understanding that this would apply starting at 5 PM for 12 hours.
I didn’t see any errors in the log and I have the information in the recorder (purge_keep_days is 28)
Where I am confused is that the where as checking the history for binary_sensor.bar I see it was on from 8:58 PM until 5:12 AM but the history_stat (sensor.foo) the value reaches 3.03 hours at 11:59 PM. I am guessing the issue might be related to the day change or maybe I don’t understand the history_stat sensor correctly.
Yes. What you have will only measure from 5pm to midnight. As soon as the date changes at midnight it will no longer be collecting data as it is before 17:00 on that day.
Instead of a start and duration you could try this: