History stats shows reduction of time the entity was in state

I made a History Stats entity to count time the heater was “On” during the heating season starting August of the previous year. What I see - is the negative changes of time the entity was in state “1”. How it is possible?! What went wrong?

  - platform: history_stats
    name: Heater running hours
    entity_id: sensor.input3_state
    state: '1'
    type: time
    start: '{{ now().replace(year=(now().year-1)).replace(month=8).replace(day=1) }}'
    end: '{{ now() }}'

I changed formula adding replacement for hours, minutes and seconds… let’s see how it will behave.

New formula:

start: '{{ now().replace(year=(now().year-1)).replace(month=8).replace(day=1).replace(hour=0).replace(minute=0).replace(second=0) }}'

It does not help.

Any ideas?

Do you still have the problem?
I have the same problem with start: '{{ 0 }}'

I have not found solution. And… i didn’t check it for a while.

Personally, I have the impression that the value of my variable decreases/reduction around 4 am. For you too ?
So, I was thinking of creating a variable that will register before the value changes and adding with the old value.

@Dimster, try this:

start: '{{ now().replace(year=now().year-1, month=8, day=1, hour=0, minute=0, second=0) }}'

Do you really want to start at 00:00:00 1 January 1970 UTC ?

Because that is what you have.

Thanks tom_l
I will try to change for start in “year-1” and finish in “year”. And work for the next year :slight_smile:

Dimster I think I know where the problem comes from, but I haven’t looked at the code in detail to validate my point.
Currently I am testing a workaround that seems to be working well. We must make a sensor with the values that we want in our history which take two values for example ‘on’ and ‘off’ example:

  - platform: template
    sensors:
      ravelli_template:
        friendly_name: 'friendly_name'
        value_template: >-
          {% if (states('sensor.ravelli_etat') | int == 4) or (states('sensor.ravelli_etat') | int == 5) %}
            on
          {% else %}
            off
          {% endif %}

And rewrite your history_stats like this :

  - platform: history_stats
    name: name_history_stats
    entity_id: sensor.ravelli_template
    state: 'on'
    type: time
    start: '{{ now().replace(year=2020, month=10, day=1, hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

Hi every one
I still have this issue, ullrik you said you thought you knew what was going one. Do you maybe have a solution. I tried to use you r latest idea, but I still see reduction in som of my history stats. Seems it drops down on the same time at night but not every night.

My Home assistant is updated and running Maria DB for history stats.
Any idea is appriciated
/Lasse

Just FYI you can simplify this template:

To this if you use a template binary sensor:

binary_sensor:
  - platform: template
    sensors:
      ravelli_template:
        friendly_name: 'friendly_name'
        value_template: "{{ (states('sensor.ravelli_etat') | int == 4) or (states('sensor.ravelli_etat') | int == 5) }}"

Hi tom_I, thanks for the reply. Always nice to simplify things.
My problem is though, that I have a sensor for how long time my tv channels is on, one for each tv channel.
When I look at history stats, I have a drop in the value without any reason. Only at some channels not on all channels, it looks like it’s the last three nights at precise 04:12 at night

My sensor is configured like this

# 7_tv2_news_stue
platform: history_stats
name: tv2_news_stue
entity_id: sensor.tv_program_stue
state: "TV 2 NEWS"
type: time
start: "{{ now().replace(year=2022, month=1, day=23, hour=0, minute=0, second=0) }}"
end: "{{ now() }}"

This is what it looks like in history, it worked for a couple of week before I get the drops. History stats is recorded by Maria DB.

I have no clue what is coursing this

Hi ullrik i see the same in my history stats around 4 am as well, it happens after HA has been running for 10-11 days, and then every night at 4 am…
Did you find a solution…
/Lasse

I was replying Mickael.

How many days do you keep in your recorder database?

7 days?

That is all this sensor can report on, what is in the database. It looks like your history stats starts dropping after 7 days.

Hi

I found this thread, i think this is my problem. Btw its MariaDB, it default i dont jnow how many days the record is.
https://community.home-assistant.io/t/strange-behavior-of-history-stats-uptime-counting/346322/2

10 days if you havn’t changed it.

1 Like