Multiple states in history_stats

Can I use history_stats if I want to measure multiple states?
For example, my air conditioner also has Cool state in addition to Heat, I want to measure both.
how can i do this best?
create multiple history_stats or can I combine this smartly, if so in what way?

Iam not sure if this works (at the state line)

  - platform: history_stats
    name: Heating Today
    entity_id: sensor.hvac_activity
    state: ['heat, 'cool']
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

Beside I think I need multiple utility_meter right? one for Heat, one for Cool I guess?
Or can I combine these also in a smart way

The docs say you can use a string or list in the state field, so it should work. I can confirm that it works on my instance. However, in your example you are missing a closing single quote on “heat” in the list for the state values. That may be what’s causing your issue.

Another option would be to combine a template sensor with your history stats sensor.

template:
  - binary_sensor:
    - name: HVAC Active
      state: "{{ is_state('sensor.hvac_activity', ['heat', 'cool']) }}"

sensor:
  - platform: history_stats
    name: HVAC Activity Today
    entity_id: binary_sensor.hvac_active
    state: 'on'
    type: time
    start: '{{ today_at() }}'
    end: '{{ now() }}'

can be replaced by {{ today_at() }}, documentation is a bit old already :slight_smile:

{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}
→ 2023-01-28 00:00:00.087187+01:00

{{ today_at() }}
→ 2023-01-28 00:00:00+01:00

2 Likes

Is there a way to make sensor start at a weekday?

Basically i want to track my time at work. So i thought i can have 1 sensor for each day and display it in a card.

You would be better off just using a single History Stats sensor then having a trigger-based template sensor for each weekday.