History Stats sensor combining multiple entities?

Hello!! I’ve tried searching for a solution to this but couldn’t come up with anything, so I’ll explain my problem here!

I want a card on my dashboard that tracks the amount of time in a day that my EV is in a state of “Charging” - the Tesla Custom Integration provides a binary_sensor for this which is in a state of “charging” or “not charging” - so easy enough for me to write some yaml for that history_stats sensor.

The problem is that I want the history stats to only count time IF the car is also at home. Essentially I’m looking to track the length of time the car’s charging at the house, as opposed to at Superchargers, etc. Again, there is a useful device_tracker entity for this that switches between “Home” and “Away” state.

I’m wondering, is it possible to create a history stats sensor, which only counts time when charging = true and location = home, please? Would I need to make a binary_sensor to track this first and then make a history_stats sensor to track when the binary_sensor is set to “On”/true?

Thanks

History Stats only handles very simple criteria for firing/tracking. That is, it only checks if a single entity’s state is equal to a single specific value. For more complex criteria, you need to combine your entities’ values in a template sensor or binary sensor, then track that entity with History Stats.

FWIW: Keep in mind that there will be no historical data for the new template sensor, so the value of the first day of the History Stats sensor may be inaccurate.

1 Like

So I have combined my data into a binary sensor, and I’ve come back just to show my working and the results of my history stats (they are very wrong… but is it “expected” wrong, or is my yaml wrong, please?)




binary_sensor:
  - platform: template
    sensors:
      tesla_charging_at_home:
        friendly_name: "Tesla Charging at Home"
        value_template: >-
          {{ is_state('binary_sensor.ghost_charging', 'on') 
            and is_state('device_tracker.ghost_location_tracker', 'home') }}
  - platform: history_stats
    name: Tesla Home Charging Time
    unique_id: tesla_charging_at_home_history
    entity_id: binary_sensor.tesla_charging_at_home
    state: 'on'
    type: time
    start: '{{ today_at() }}'
    end: '{{ now() }}'

From my binary sensor combining the data, I should only expect to see that tiny portion of time tracked (about 2 mins really before an automation switched off the charging due to electricity rates) - but the history stats are showing 23 hours :confused:

Both of your sensor configs look correct. That isn’t what it should do, but I have seen threads where other users have shared something similar where it quickly rises from 0 to the number of hours that have elapsed since midnight. IIRC, it usually sorts itself out after a restart or two.

1 Like

For doing this kind of thing, I like using the Riemann sum and Utility Meter integrations. While these are typically used to accumulate and track daily, weekly, monthly usage of electricity or gas; they can also be used to accumulate time. As an example I use it to track the number of hours on my hvac filter - which in the same as tracking the number of hours your car is charging at home, and the number it’s is charging at not home.