My monthly "internet usage" cycle resets on the 21st. Does anyone have a template for tracking a monthly cycle progress as a unit float or percentage? I.e. in my case, the 21st at 12am would be 0.00, and the 20th at midnight would be 1.00.
This template would do it, bit of a kludge though
template:
- sensor:
- name: Internet Usage Percent
variables:
reset_day: 21
# Do not change
_reset_offset: "{{ 31 - reset_day + 1 }}"
unit_of_measurement: %
state: >
{% set midnight = today_at() %}
{% set this_month = midnight.replace(day=reset_day) %}
{% if now() > this_month %}
{% set tspan = now() - this_month %}
{% set next_month = (midnight + timedelta(days=_reset_offset)).replace(day=reset_day) %}
{% set total = next_month - this_month %}
{% else %}
{% set last_month = (midnight.replace(day=1) - timedelta(hours=24)).replace(day=reset_day) %}
{% set tspan = now() - last_month %}
{% set total = this_month - last_month %}
{% endif %}
{{ tspan / total * 100 }}
Thanks for that!
I wasn't able to use the "variable" properties (fails with logged error "'variable' is an invalid option for 'template'"), but if I move them into the state script then it works.
It should be variables, Petro didn't eat his Wheaties today ![]()
Yep, good catch. ![]()