History stats for current year

Hi,
I need to make a sensor that gives me the history stats for how long my aircondition is on for the current year. I made some attempts (the last is the below) but can not figure it out.
Can someone help?

  - platform: history_stats
    name: AirCon Living Room time on
    entity_id: binary_sensor.conditioner_on
    state: 'on'
    type: time
    #end: '{{ now() }}'
    #end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now().year }}'
    duration:
      days: 365 
      

Try this (not tested)

  - platform: history_stats
    name: AirCon Living Room time on
    entity_id: binary_sensor.conditioner_on
    state: 'on'
    type: time
    start: '{{ strptime("1-1-" ~ now().year, "%d-%m-%Y") }}'
    end: '{{ strptime("31-12-" ~ now().year, "%d-%m-%Y") }}'

Thanks for the help
Just added and restarted HA. I turned on the air condition, and for the time being is showing “Unknown”

I haven’t looked at your start and end templates yet because I suspect there is a bigger problem. Do you actually keep a year’s worth of data in your recorder?

The default is 10 days.

1 Like

No need for crazy templates… You want the the end to be now.

  - platform: history_stats
    name: AirCon Living Room time on
    entity_id: binary_sensor.conditioner_on
    state: 'on'
    type: time
    end: '{{ now() }}'
    duration:
      days: 365

Also, as @tom_l said, this will only work if you maintain a years worth of history though the recorder integration.

Thanks a lot. I had already tried what petro suggested, and I am not sure that this is what I want. for example next November I would like to see the time the aircondition was on for the 2021. If I understand correct this sensor will show me the time from October 2020 until November 2021.

However Tom is correct because either I have to keep records for a year in recorder which I am not.

It will show you time from 11/30/2019 to 11/30/2020, not sure where you’re getting October from or the year 2021 because this integration only looks in the past. You have a miss understanding on how this integration works.

Yes, that is what I tried to say. but I need the numbers only for the current year. now only for 2020 and next year only for 2021.

If you want the current year then it would look like this.

  - platform: history_stats
    name: AirCon Living Room time on
    entity_id: binary_sensor.conditioner_on
    state: 'on'
    type: time
    start: '{{ now().replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) }}'
    end: '{{ now() }}'
1 Like

just test it and it seems to working ok.

Probably I will have to add it in grafana, but I have many problems to arrange the charts (limited knowledge)

Hey

have some Problems with stats of current year and last Month, values jumps to somewhere
mostly it happens after new Start of HA,
what can it be

grafik

red mark was 31 hours
grafik

  name: TV Schlafzimmer (Stunden aktuelles Jahr)
  entity_id: switch.tv_schalfzimmer
  state: "on"
  type: time
  start: '{{ now().replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) }}'
  end: '{{ now() }}'

Is there a fix for this? When creating a new time-counter with

    start: '{{ 0 }}'

I get a wrong value that is huge.