History stats

I would like to see how much time my condition is on. The problem is the it has 3 states.
heat-cool-dry. Can I have 1 sensor only depending on the modes?

I have the following code but this way I have to make 3 different sensors
Is there a way to combine them ?

  - platform: history_stats
    name: Climate Living Room
    entity_id: climate.living_room_ac
    state: 'dry'
    type: time
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      hours: 48 # Last 48 hours

Make a template binary sensor and use that in your history stats:

binary_sensor:
  - platform: template
    sensors:
      conditioner_on:
        friendly_name: "Conditioner On"
        value_template: >-
          {{ states('climate.living_room_ac') in ['dry', 'cool', 'heat'] }}

History stats sensor becomes:

  - platform: history_stats
    name: Climate Living Room
    entity_id: binary_sensor.conditioner_on
    state: 'on'
    type: time
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      hours: 48 # Last 48 hours

Thanks, I am testing it right now. Can you suggest what kind of card it would be better to use to see the results in hours-minutes eg 5h 15 min or 05:15?
I am using a history graph card but I don’t think it will be convenient.

After 3-4 hours, although the binary sensor is on the history stats shows 0 in history graph and I don’t know why

Post your config.

in the following path
config/sensors/statistics/Living R Air Con Stats.yaml

I have this

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

in config file I have this


  #binary_sensor:
  - platform: template
    sensors:
      conditioner_on:
        friendly_name: "Conditioner On"
        value_template: >-
          {{ states('climate.living_room_ac') in ['dry', 'cool', 'heat'] }}

I deleted the (#)binary_sensor because I have this line of code in front of other binary sensors

in config file I have
sensor: !include_dir_merge_list sensors/
this is working ok because I have a few sensors and they are all reporting their values correct

I hope that you don’t need my complete config file because I have to transfer a lot of passwords in secrets.yaml and I can’t right now

No that looks ok. And you have confirmed binary_sensor.conditioner_on is on in the developer tools states menu?

Are there any relevant errors in the developer tools / logs?

Sorry for the late reply. Yes the binary_sensor.conditioner_on is on in the developer tools states menu, and there are no errors in the logs.

Has it been on the whole time?

Maybe it needs to transition from off to on to start the history_stats sensor?

Other than that I’m out of ideas.

Yes, I have tried to turn it on-off but the result is the same.

I will try to delete the sensor and add it from scratch to see what happens.
Thanks in every case

I’ve just been reading about the hystory_stats sensor, it uses the recorder database to generate the state so my theory above about requiring a state change is incorrect.

Make sure the entity id in the dev tools states menu matches the one you used in the history stats sensor.

That would be probably the case! I have limited the recorder to what I need to see and keep the database small. I will added later when I return home and let you know.
(I would never thought of that)
thanks again!

Ah yeah. You need it included in the recorder for this to work.

After a few days I can report that it is working but not in the way I desired.
for example:
right now the device is on.
the time counter is showing the same value, that was valid before I turned on the device. eg 2h
if I turn off the device again the value is 2h. Sometime (I think at 00:00 ?) it will show the new value eg 6h
It makes sense I guess but I would prefer something like a stopwatch for the particular device.

That’s because you specified the end time to be 00:00 in the morning today. Try just:

end: '{{ now() }}'

This should give you a rolling update for the last 48 hours to now.

2 Likes