Help Setting up History Stats Sensor

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.

Here is the config I am trying to work with

#Oil Boiler Sensor History
  - platform: history_stats
    name: Boiler time
    entity_id: sensor.boiler_switch
    state: "ON"
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      days: 30
#Oil Boiler Switch Sensor
  - platform: mqtt
    state_topic: stat/Boiler/POWER
    name: boiler switch

Try it with state: 'on'. Entities and states (on/off/home/…) are always lowercase.
Check the state in Dev tools/states.

Looks like its a caps ON on dev tools

I just tried it with a lower case ‘on’ and no go either

all of the other switches are lowercase though…

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! :slightly_smiling_face:

1 Like

Confirmed, for “today” I use:

    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'

    end: '{{ now() }}'

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?

This:

start: '{{ now().replace(day=1).replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'

will show the stats for the current month.

This:

    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      days: 30

shows the stats for the last 30 days.

You Guys are great, is there a place I can read up on the formatting of those time stamps?

Try and experiment them in Dev tools/templates and you see what you get.
You will get a feeling for them.

Welcome to the clup! :sunglasses:

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)}}