Hello all,
I would like to create the following two sensors:
- Average hour
- Average minutes
The idea is as follows,
Everytime a binary switch is turned off the hour and minute are recorded and devided by the amount of recorded input from the binary switch.
Doing this gives me an average time at which the binary state is switched off, this lets me control the lights to dim at the desired time.
But i don’t know how to accomplish this how the two sensors hold there values or to count them up.
Hope somebody is willing to help me with this.
Regard,
Jordy
This is something that seems like it should be simple to do, but I haven’t seen any examples of how to do it easily. I cobbled together a method to do something like this, but it’s pretty convoluted and involved to set up… it doesn’t exactly match what you have proposed and it’s not something I would suggest for most use cases.
Hi Drew,
Thanks for you’re suggestion, as it turns out I had some free time today at work and came up with what i think is a good solution but I defentily would love you input on what you think of it.
So for starters I’ve made 3 types of template sensors:
- platform: template
sensors:
jordy_goto_sleep_hour:
friendly_name: Average hour when going to bed jordy
value_template: >
{% if states('input_select.jordy_sleepcycle') == 'Sleeping' %}
{% if 18 < now().hour|int %}
{{now().hour|int}}
{% else %}
{% set base_hour = 24 + now().hour %}
{{base_hour|int}}
{% endif %}
{% endif %}
jordy_goto_sleep_minute:
friendly_name: Average minutes when going to bed jordy
value_template: >
{% if states('input_select.jordy_sleepcycle') == 'Sleeping' %}
{% set initial_minute = now().minute %}
{{initial_minute}}
{% endif %}
jordy_goto_sleep_time:
friendly_name: Average time to bed for Jordy
value_template: >
{% if states('sensor.average_time_to_bed_hours_jordy')|int > 23 %}
{% set hours = states('sensor.average_time_to_bed_hours_jordy')|int - 24 %}
{% else %}
{% set hours = states('sensor.average_time_to_bed_hours_jordy')|int %}
{% endif %}
{% set minutes = states('sensor.average_time_to_bed_hours_jordy')|int %}
{{hours|string + ":" + minutes|string }}
icon_template: mdi:clock
So everytime the input boolean changes state to “Sleeping” the hour and minute template sensor gets the value of the current hour and minute. For the hours I’ve its necessary to do some adjustement because a value of 00 for hours (which indicates midnight) is ignored when taking the average of all measurements. that’s why I count up so midnight becomes 24 and 01 becomes 25 and so on.
With the minutes I didn’t find it that interesting, the hours are more important.
To keep track of all changes I’ve made a statistics sensor that uses the hour and minute template sensors.
- platform: statistics
name: Average Time to Bed Hours Jordy
entity_id: sensor.jordy_goto_sleep_hour
state_characteristic: median
sampling_size: 50
precision: 0
max_age:
days: 30
- platform: statistics
name: Average Time to Bed Minutes Jordy
entity_id: sensor.jordy_goto_sleep_minute
state_characteristic: median
sampling_size: 50
precision: 0
max_age:
days: 30
this gives me i pretty good indication of what the average time to bed is.
The last sensor displays the average time hh:mm on the dashboard, then if the average value for the hours is above 23 I subtract 24 to get the actual hour so values past midnight would also work.
What do you think about this approach, would love you’re thoughts on this.
Regards Jordy