History stats question

There used to be a way to do it using the History Stats integration, but it stopped working around June last year and, AFAIK, is still broken/bugged. Currrently, one way to do it is to use two template sensors.

template:
  - trigger:
      - platform: state
        entity_id: switch.shelly1_e8db84d73b52
        to: 'on'
        from: 'off'
    sensor:
      - name: Air Exchange Last On
        state: "{{ now() }}"
  - sensor:
      - name: Air Exchange On Time
        state: >
          {% if is_state('switch.shelly1_e8db84d73b52', 'off') %}
            0
          {% else %}
            {{ (now() - states('sensor.air_exchange_last_on') | as_datetime).total_seconds/3600 }}
          {% endif %}