History Stats droping down time counting

Hello, how are you ?
I’m trying to do a sensor that counts the time of a switch “on” since a specific date determinated by a input_datetime.

After every update, or if the switch is unavailable and without change the start, the totalizer does not continue totalizing the quantity and drop down the counting.

Here is my code:

  • Sensor 1
platform: history_stats
unique_id: tempo_refil_repelente
name: Tempo Refil Repelente
entity_id: switch.tomada_repelente
state: "on"
type: time
start: "{{ as_timestamp(states('input_datetime.data_troca_refil')) }}"
end: '{{ now() }}'
  • Sensor 2 (trying other way)
platform: history_stats
unique_id: tempo_refil_repelente_troca
name: Tempo Refil Repelente Troca
entity_id: switch.tomada_repelente
state: "on"
type: time
start: "{{ states('input_datetime.data_troca_refil') | as_timestamp() }}"
end: '{{ now() }}'

Both does not work.

And the template works fine.

If anyone can help me. Thanks.

How long ago is the date in your helper?

https://www.home-assistant.io/integrations/history_stats/

Purge keep days is only 10 days by default.