I’ve got a history stats sensor measuring screen time during night because you know… Little darn devils…
It works fine while I’m checking the sensor during the night but when day comes and the stop point is reached for the sensor, it won’t keep displaying the measured activity at my dashboard, instead it goes unknown which isn’t helpful. How can I prevent this?
Now all I miss is an automation disconnecting internet for the person in question when time goes over a certain amount
Sorry for image, file editor on mobile won’t let me copy.
So on the 18th January at 23:30 replace is doing the correct thing.
However after midnight - you are asking it to start at 11pm tonight, which is in the future.
The screenshot cropped some of the details of your example so I took a guess at the values of unique_id and entity_id. The main difference from your version is the use of the end option instead of start.
Both excellent answers from @123 and @mobile.andrew.jones
I totally missed the fact that now() includes a date
I tried the suggestion of @123 as it seemed more straight forward, will report back tomorrow when I see how it works. Both should theoretically work though.
Actually I got good use out of the answer from @123 as well in another similar thing. I hope I got it right:
- platform: history_stats
name: Ellinore daytime screen time ratio
unique_id: history_stats_ellinore_daytime_screen_time_ratio
entity_id: binary_sensor.ellinores_phone_interactive
state: "on"
type: ratio
start: "{{ now().replace(hour=7, minute=0, second=0) }}"
# end: "{{ now() }}"
end: >-
{% set n = now().replace(hour=23,minute=0,second=0) %}
{{ (now()) if now().hour < 23 else n }}
So it turned out that didn’t work unfortunately. Same issue: meter becomes unknown after the end of the metering period and remains unknown through the entire day, which is when I want to review it. It’s pretty clear that it goes unknown right when the measuring period end from the looks of this:
(assuming if it worked, that the line should stay at the top value until reset back to zero)
- platform: history_stats
name: Ellinore nightly screen time
unique_id: history_stats_ellinore_nightly_screen_time
entity_id: binary_sensor.ellinores_phone_interactive
state: "on"
type: time
end: "{{ (now().replace(minute=0, second=0, microsecond=0) + timedelta(hours=17)).replace(hour=7) }}" # end at 7, at right side of dateline
duration:
hours: 8
It works, but I get the unknown state as soon as the clock passes 7am. I guess my problem here is that I want to STOP measuring at 7am but still display the result in the sensor until it resets at 23 and starts counting again.
Maybe to find out if it is a bug or not, you should create a temporary sensor - and hardcode the start to be 11pm yesterday and end to be 7am today, and see if it gives you data or is still unknown?