Time valve heating on

Hi all.

I wont show on my dashboard how long have my thermostatic valves been on (by day, month and year), like this
image

I have found only this solution, for each valve I have to create:

  • A sensor or binary sensor, in order to check the state of valve, my valves can have two conditions to be considered “on”

    • state
    • hvac_action

This is my sensor (the OR condition not work on binary_sensor)

- sensor:
    - name: Stato Valvola Studio
      unique_id: "valvolaStudio_0001"
      state: >
        {% if is_state('climate.studio', 'heat') or is_state_attr('climate.studio', 'hvac_action','heating') %}
            On
        {% else %}
            Off
        {% endif %}
  • 3 history_stats, for DAY, MONTH and YEAR
- platform: history_stats
  name: valvStudioDay
  entity_id: sensor.stato_valvola_studio
  state: "On"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

- platform: history_stats
  name: valvStudioMonth
  entity_id: sensor.stato_valvola_studio
  state: "On"
  type: time
  start: "{{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0 ) }}"
  end: "{{ now() }}"

- platform: history_stats
  name: valvStudioYear
  entity_id: sensor.stato_valvola_studio
  state: "On"
  type: time
  start: "{{ now().replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) }}"
  end: "{{ now() }}"
  • finally 3 new sensors to display the hours in HH:MM:SS
    - name: TimeValvolaStudioDay
      state: >
        {{ (timedelta(hours=states('sensor.valvStudioDay') | float(0)) | string)[:-3] }}

    - name: TimeValvolaStudioMonth
      state: >
        {{ (timedelta(hours=states('sensor.valvStudioMonth') | float(0)) | string)[:-3] }}

    - name: TimeValvolaStudioYear
      state: >
        {{ (timedelta(hours=states('sensor.valvStudioYear) | float(0)) | string)[:-3] }}

All this for each single valve.

Do you know of any faster method to get to the same result?

Thanks,
Marco

One Template Binary Sensor per valve.

- binary_sensor:
    - name: Stato Valvola Studio
      unique_id: "valvolaStudio_0001"
      state: "{{ is_state('climate.studio', 'heat') or is_state_attr('climate.studio', 'hvac_action','heating') }}"

One History Stats sensor per valve.

- platform: history_stats
  name: valvStudioDay
  entity_id: sensor.stato_valvola_studio
  state: "on"
  type: time
  start: "{{ today_at() }}"
  end: "{{ now() }}"

Three Utility Meter sensors per valve: daily, monthly, yearly.

utility_meter:
  valvola_studio_daily:
    source: sensor.valvstudioday
    cycle: daily
  valvola_studio_monthly:
    source: sensor.valvstudioday
    cycle: monthly
  valvola_studio_yearly:
    source: sensor.valvstudioday
    cycle: yearly
2 Likes

I don’t know if you are aware but the History Stats integration calculates its value based on the data found in Home Assistant’s database. You defined History Stats sensors to compute monthly and yearly values. However, the database is purged of old data on a regular basis (every 10 days by default). Therefore, unless you extend the purge interval (not recommended for as long as a month, let alone a year) the calculated value won’t be what you want.

In contrast, the Utility Meter integration works incrementally and stores its value in a table within the database that is not purged.

1 Like