I am trying to set up a history states sensor to simply count the total time a switch is set to the on position. Should be pretty simple but its kicking my butt
I don’t have any special configs for History or Recorder, for recorder I just have Purge_keep_days: 30
Basically its a D1 Mini(tasmota) that will be tied to the contacts of a relay. So when the relay contacts close it sets the switch to on, when they open: off. The coil side of the relay will be tied to my Oil Boiler pump to get actual run time of the burner.
The sensor for the tasmota topic works as it updates HA with on or off perfectly but the history stats just shows as unknown.
The measure of your states ends at 00:00:00 today for a duration of 30 days.
So if you setup the sensor today, there will be no records until 00:00:00 tomorrow.
I think!
That was it! So if I had just waited it would have updated at 00:00:00 tonight.
@eXtatic
That works!
Now is there anyway I can tell it to start at the beggining of the month or on day 1 of the month and end now
Or could I also just do show all history and purge the recorder every 30 days?
great.
Is there any way to have by month? or by day?
I would like to compare like in a bar graph, month by month or day by day
is this possible with history stats?
Possible for sure but you need to keep your history as long as you want to do that.
Personnaly I only keep 30 days of history for performance reasons.
Didn’t try to raise it for the past year or so.
Comparing two past month plus the current one would mean you have to store the history of 3 month.
If you want to proceed, here is an untested snipped adding 2 sensors, one for the past month and one before that:
- platform: history_stats
name: Heating month -1
entity_id: binary_sensor.state_heating
state: 'on'
type: ratio
start: >-
{% set time = now() %}
{% set previous_month = time.month - 1 if time.month - 1 > 0 else 1 %}
{{time.replace(month=previous_month).replace(day=1).replace(hour=0).replace(minute=0).replace(second=0)}}
end: >-
{% set time = now() %}
{% set previous_month = time.month - 1 if time.month - 1 > 0 else 1 %}
{% set days_of_previous_month = time.replace(day=1) - time.replace(month=previous_month).replace(day=1) %}
{{time.replace(month=previous_month).replace(day=days_of_previous_month.days).replace(hour=0).replace(minute=0).replace(second=0)}}
- platform: history_stats
name: Heating month -2
entity_id: binary_sensor.state_heating
state: 'on'
type: ratio
start: >-
{% set time = now() %}
{% set previous_month = time.month - 2 if time.month - 2 > 0 else 1 %}
{{time.replace(month=previous_month).replace(day=1).replace(hour=0).replace(minute=0).replace(second=0)}}
end: >-
{% set time = now() %}
{% set previous_month = time.month - 2 if time.month - 2 > 0 else 1 %}
{% set days_of_previous_month = time.replace(day=1) - time.replace(month=previous_month).replace(day=1) %}
{{time.replace(month=previous_month).replace(day=days_of_previous_month.days).replace(hour=0).replace(minute=0).replace(second=0)}}