Apexcharts-card 1 year history grouped by month

Hey guys,

im trying to create a bar graph with a span of a year and monthly summary. Im using a custom state measurement entity wthich count our laundry. My problem is when I set “type to state” under the statistics series no value is available and wont show. If I remove this setting, it shows me correctly the last value of the month, but not the current value during the month:

image

here is my code:

type: custom:apexcharts-card
apex_config:
  chart:
    height: 300px
experimental:
  color_threshold: true
graph_span: 1year
locale: de
span:
  start: year
show:
  last_updated: false
header:
  show: true
  show_states: true
  colorize_states: true
  title: Statistik
  floating: false
now:
  show: true
  label: heute
  color: red
yaxis:
  - min: 0
    max: ~15
    decimals: 0
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.custom_wasche_counter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Wäsche
    statistics:
      period: month
      align: start
      type: state
  - entity: sensor.custom_trockner_counter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Trockner
    statistics:
      period: month
      align: start
      type: state

The entity looks good to me:

image

I want to display the sum of the previous months and also the current value of the current month until the end.
Can anyone assist?

thx alot

Alex

No one who can help? :frowning:

I’m watching your topic.
You can follow my journey here

That ended in a sad statistics-graph card.
If someone is helping you one day, I’d like to know it as well.

Hi Oliver,
thanks for your reply, that was really helping a lot. I think in my case I did the mistake that I didnt worked with a “utility meter” helper. I just created a custom sensor with measurement state class and tried to use that. Your thread bring me to the right point :slight_smile: I just have to wait for the end of the month to see if the apexchart will display my values correctly like I want :grimacing:

My current chart code looks like:

type: custom:apexcharts-card
apex_config:
  chart:
    height: 300px
experimental:
  color_threshold: true
graph_span: 6month
span:
  end: month
show:
  last_updated: false
header:
  show: true
  show_states: true
  colorize_states: true
  title: Statistik
yaxis:
  - min: 0
    max: ~15
    decimals: 0
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.custom_wasche_meter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Wäsche
    group_by:
      func: last
      duration: 1month
  - entity: sensor.custom_trockner_meter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Trockner
    group_by:
      func: last
      duration: 1month

currently it shows me the correct values for the current month:

image

But it should also show me the value of the last month. It did, but after a reboot the value was lost.

The development tools shows me that the value of the last period is available, why its lost on the graph?

thanks in advanced!

regards

pLeX

Hello pLeX

Did you ever resolve this ? I have the same problem, the apex-chart, only shows results for the current month, but all results for the year is in the database.

/MichaelJ

I think you need to use LTS (long term statistics) if you want to show data that far back. HA will purge data from the recorder after 10 days (default). You can change this but it impacts performance so its not advised.

You can combine group_by and statistics in apexcharts and that works well for daily data grouped by day or week (7 days). I am working on same approach for monthly (daily stats grouped by month) but I am having trouble.

I am also working on a new solution and that is to create a LTS monthly sensor (refer to the History Statistics integration to see how to do this).

below are my weekly and monthly views + example yaml (using daily statistics grouped).

For this to work, you need to reset the statistics sensor at 12am each day. like this…

image

image

type: custom:apexcharts-card
all_series_config:
  stroke_width: 1
  show:
    offset_in_name: false
    legend_value: false
    extremas: false
    name_in_header: false
graph_span: 4month
span:
  end: month
apex_config:
  legend:
    show: false
  dataLabels:
    style:
      fontSize: 9px
    background:
      enabled: true
      padding: 2
      borderRadius: 1
yaxis:
  - id: hours
    min: 0
    max: ~60
    decimals: 0
    apex_config:
      tickAmount: 4
      title:
        rotate: -90
        text: Hrs
        style:
          fontWeight: 400
          fontSize: 11
    show: true
    opposite: false
header:
  show: true
  title: Gas Heater Monthly (4 mths)
  show_states: false
  colorize_states: true
series:
  - entity: sensor.gas_heater_daily_usage
    name: Heat on Hrs/day
    yaxis_id: hours
    color: blue
    type: column
    opacity: 0.2
    statistics:
      type: max
      period: day
    group_by:
      func: sum
      duration: 1month
  - entity: sensor.gas_heater_daily_usage
    name: Heat on line
    stroke_width: 2
    yaxis_id: hours
    color: green
    type: line
    opacity: 1
    show:
      datalabels: true
    statistics:
      type: max
      period: day
    group_by:
      func: sum
      duration: 1month

1 Like

Hi Michael,

Im currently using this code for the apex chart:

type: custom:apexcharts-card
apex_config:
  chart:
    height: 300px
experimental:
  color_threshold: true
graph_span: 3month
span:
  end: month
show:
  last_updated: false
header:
  show: true
  show_states: true
  colorize_states: true
  title: Statistik
yaxis:
  - min: 0
    max: ~15
    decimals: 0
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.custom_wasche_meter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Wäsche
    group_by:
      func: last
      duration: 1month
  - entity: sensor.custom_trockner_meter
    show:
      header_color_threshold: true
      extremas: true
    type: column
    name: Trockner
    group_by:
      func: last
      duration: 1month

The result looks like:

image

This is exactly the view I want, but the whole configuration drops statistics and I dont understand why. You can see theres no data for june. Im using utility meter helpers as sensor for this graph. When anybody would tell me how I can create LTS for a utility helper entitiy, I would appreciate your help :slight_smile:

Okay so when I understand the LTS feature correctly, I only have to set a unit of measurement and the data should be available in the long term stats.

This is definitely true for my case. Both sensors I am using are custom utility meters entities from helpers created from the gui and both show me under the dev tools the unit of measurement is count.

So why it’s dropping or not display the stats it’s the question?!

Yesterday I was updating home assistant to the latest version and now the stats from July will not be displayed any more. When I look under the dev tools and pick up the utility meter I see that there is the last period still available as value.

What I’m missing here?

Hello pLeX

I’m sorry that I’m not any help here, as I struggle with the same issue, and my database only purges every 6 months. I’ll keen on trying to figure this out.

/Micahel

Does not work for me… it report 0. I’m using daily energy counters. Long terms statistics seems to work because i can see them in default HA statistics graph.
Any idea?

It looks to me that this was fixed in any release. I have now the full statistics available for the last 3 month and it does not drop it anymore. The values also survived several home assistant updates, so maybe this was in any release fixed. :slight_smile:

@MichaelJ2600, how its about yours?

Hello pLeX

I don’t know yet, as I did a fatal upgrade from 2022.06 to 2023.12 in december. let’s just say with a Top Gun saying - it crashed and burned… LOL

So ealy january I started to reconfigure from scratch, whitch is now finally working as intended - always a fun time spend…

I therefore only have two months worth of data, but can confirm that these are showing. I’m exited to see if this continues and I get the full 6 months of data eventually, but from what you’re stating, it looks like it.

Michael J