Apexcharts: Nest heating minutes in hourly intervals

Really struggling to plot a column chart for the amount of minutes my heating is on in hourly intervals (over a 24 hour period).

To simulate this, ive created a template binary sensor which has an “on” and “off” state, triggered by the thermostat being set to 16 or below. (This binary sensor would be replaced by looking at whether the thermostat was “idle” or “heating”).

      nest_upstairs_setpoint_below_16:
        value_template: >
          {% if states('sensor.nest_upstairs_setpoint') is defined and
                int(states('sensor.nest_upstairs_setpoint')) <= 16 %}
          on
          {% else %}
          off
          {% endif %}

The sensor tracks on/off states:

This is what I am using in my chart template:

type: custom:apexcharts-card
graph_span: 24h
header:
  show: true
  title: Nest Upstairs Heating
update_interval: 60min
span:
  start: day
series:
  - entity: sensor.nest_upstairs_setpoint_below_16
    type: column
    transform: |
      return x === 'on' ? 1 : 0;
    stroke_width: 2
    color: red
    opacity: 1
    group_by:
      fill: last
      func: sum
      duration: 60m

I am not seeing the values as a sum of the on state in a 1 hour period. Eg. Between 10am and 11am, I’d expect to see a line showing 45 minutes duration:

(I cant post more than one image unfortunately, so included this one to show the yellow “on” period i am trying to chart with a value of 45 minutes).

Instead, I get a value of “1” for 10am and a maximum value of “4” at 11am.

Im obviously doing something completely wrong, so any pointers appreciated!

This is what my chart looks like:

As far as what you’ve currently got, the documentation is not really clear on what func: sum does in conjunction with fill: last, or whether or not duration is on boundaries or rolling. You’re expecting it to put a 1 or 0 in the ‘bucket’ for the hour every minute, so that at the end of 60 minutes you get the sum you want. I can’t see anything in the code that looks like this.

I’d be inclined to look at History Stats or Utility Meter to create a sensor that contains the data for the graph. But every time I’ve used these they give me results I didn’t expect.

Alternatively a quick-and-dirty approach would be to just increment a number every minute that power is on (and ensure you get long-term stats). Then graph the change every hour (using the statistics options).

Thanks for the reply. You’ve confirmed what took me almost 2 days to conclude - that the bucketing doesnt work in the way I need it to.

I did setup a history stat and it does seem to be working as expected. But occasionally, I have seen some odd results for the current hour, so will continue to have a play.

  - platform: history_stats
    name: history_stat_nest_upstairs_setpoint_below_16_on_1_hour
    unique_id: history_stat_nest_upstairs_setpoint_below_16_on_1_hour
    entity_id: sensor.nest_upstairs_setpoint_below_16
    state: "on"
    type: time
    #start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    duration:
      hours: 1