ApexCharts card - A highly customizable graph card

Maybe not a direct solution to your question but how I’ve done similar using a donut chart for my power. The total label is what I’ve used in the centre and then the javascript is to set the style and apend the units.

If your entity isn’t giving you the TB, you could use this area to ‘do maths’ on the % to give a TB value.

Alternatively, you could embed the apex chart in to a custom:config-template-card so that you can define your TB entity as a variable, then potentially use that in the label.

apex_config:
  plotOptions:
    pie:
      donut:
        labels:
          show: true
          total:
            show: true
            showAlways: true
            fontSize: 22px
            fontFamily: Helvetica, Arial, sans-serif
            fontWeight: 600
            formatter: |
              EVAL: function (w) {
                return (w.globals.seriesTotals.reduce((a, b) => {
                  return a + b
                }, 0) / 1000).toFixed(1) + " kW"
              }

Hello. I’m having an issue with Apex Charts in the way that it forgets some older data.
For example here.


Even though this chart should have January as start date, it only remembers the last few weeks.

Am I doing something wrong?
Thanks!

Hello. I’m trying to do exactly what you did here, I have the entity that gives me the total, I have several entities that shows instant power and I would like to create that “Other” that calculate Total - Individuals.
Can you share the complete code for this graph please?
Thanks!

Thanks for taking the time to reply @alexeiw123 ! I do have an entity “sensor.volume1_space_used” which does provide the TB.
Based on your suggestion I’ve tried the below, but it does not return anything

formatter: >-
          function (w) { return
          '${states['sensor.volume1_space_used']}' }

I’ve read through the docs and this thread, and see a few others have asked similiar questions, but no working examples - other than the “total” approach which you have shared.
…perhaps just not possible, and I may just need to change my approach.
thanks again!

Seems you are uing history values which are purged every x-days (default 10)…use statistics instead (search in this very long thread of posts)

1 Like

Thanks vingerha for the info. I checked the thread, but I could only find people asking for this feature. Maybe if they solved the issue, didn’t wrote back :slight_smile: (or I didn’t find the solution at least)
So @Lunkobelix @RomRider did you manage to solve the long-term statistics to show up in apex charts?
Thanks!

No worries - I use a template sensor to workout the ‘other’ power from my whole of house monitoring minus the other loads that I’m reporting on individually. Simplified the code for you here.

template:
  sensor:
    - name: unmonitored power
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {% set cons = states('sensor.consumption') | int(default=0) %}
        {% set load1 = states('sensor.load1') | int(default=0) %}
        {% set load2 = states('sensor.load2') | float(default=0) %}
        {% set load3 = states('sensor.load3') | float(default=0) %}
        {% set loadx = states('sensor.loadx') | float(default=0) %}
        {% set all = (load1 + load2 + load3 + loadx) | int(default=0) %}
        {{ max(cons - all,0) }}

Here’s my config for that donut chart exactly as I use it. You’d obviously need to change the sensors, sizes etc. to suit your dashboard

type: custom:apexcharts-card
chart_type: donut
update_interval: 2s
color_list:
  - '#1a936f'
  - '#c6dabf'
  - '#4071ed'
  - purple
  - orange
  - brown
  - pink
  - cyan
  - blue
  - '#6F4E37'
  - indigo
  - yellow
  - gold
  - lavender
  - '#71797E'
header:
  show: true
  title: Power Consumption Now
all_series_config:
  unit: W
  show:
    legend_value: true
    in_header: false
apex_config:
  stroke:
    width: 1
  legend:
    show: false
  chart:
    height: 355px
    animations:
      enabled: true
  plotOptions:
    pie:
      dataLabels:
        minAngleToShowLabel: 5
      customScale: 1
      expandOnClick: true
      donut:
        size: 50%
        labels:
          show: true
          total:
            show: true
            showAlways: true
            fontSize: 22px
            fontFamily: Helvetica, Arial, sans-serif
            fontWeight: 600
            formatter: |
              EVAL: function (w) {
                return (w.globals.seriesTotals.reduce((a, b) => {
                  return a + b
                }, 0) / 1000).toFixed(1) + " kW"
              }
          name:
            show: true
          value:
            show: true
            fontSize: 16px
            fontFamily: Helvetica, Arial, sans-serif
            fontWeight: 400
  dataLabels:
    enabled: true
    formatter: |
      EVAL: function (value, opt) {
        if (opt.w.config.series[opt.seriesIndex] >= 1000) {
          return opt.w.config.labels[opt.seriesIndex] + ": " + (opt.w.config.series[opt.seriesIndex] / 1000).toFixed(2) + " " + " kW" ;
        } else if (opt.w.config.series[opt.seriesIndex] < 1000) {
          return opt.w.config.labels[opt.seriesIndex] + ": " + opt.w.config.series[opt.seriesIndex].toFixed(0) + " " + " W" ;
        }
      }
series:
  - entity: sensor.twc_charger_load_always_w
    name: EV Chargers
  - entity: sensor.hot_water_system_supply_switch_0_power
    name: Hot Water
  - entity: sensor.fridge_current_consumption
    name: Fridge
  - entity: sensor.smart_plug_washer_power
    name: Washer
  - entity: sensor.smart_plug_dryer_power
    name: Dryer
  - entity: sensor.sw2_tvmedia_power
    name: Media
  - entity: sensor.smart_plug_dishwasher_power
    name: Dishwasher
  - entity: sensor.study_desk_smart_plug_power
    name: Study
  - entity: sensor.ac_power_total_corrected
    name: Air Con
  - entity: sensor.smart_plug_coffee_machine_power
    name: Coffee Machine
  - entity: sensor.dehumid_smart_plug_power
    name: Wardrobe Dehumidifier
  - entity: sensor.smart_plug_thermomix_power
    name: Thermomix
  - entity: sensor.oven_and_cooktop_power_b
    name: Oven
  - entity: sensor.oven_and_cooktop_power_a
    name: Cooktop
  - entity: sensor.unmonitored_power
    name: Other

I’ve got no skills using javascript - I mostly found other examples then stumbled by my way through to got it to do what I want.

That said, I don’t think that you can call a HA sensor in the javascript, but I do think that javascript accepts variables. To set a javascript variable, you need to use custom:config-template-card, define the variable, then put your apex chart within that template card, and use the variable in the javascript part.

It’s probably easier if you just use the ‘total’ approach like in my example, and use entities that give the total view. You could use a template sensor to work out the unused percentage, or the used and unused storage capacity.

Other way is to manipulate the value in the javascipt to display as you want it. In my example, I use / 1000, to show kW rather than W. You could calculate the storage being used from the total volume and the percentage there, then append the appropriate units. I’ll leave it to you to figure out how, because I’d only be fumbling around to get to the end result anyway.

Could someone please help me with whats going on here?
I have two series that I’m charting, but when then second series is showing 0 values, it is being displayed incorrectly… any ideas?

type: custom:apexcharts-card
graph_span: 2hr
stacked: true
span:
  offset: +5m
series:
  - entity: sensor.solaredge_m1_exported_kwh
    type: line
    curve: stepline
    transform: return x * 1000
    group_by:
      func: diff
      duration: 5min
      start_with_last: true
  - entity: sensor.solaredge_m1_imported_kwh
    type: line
    curve: straight
    invert: true
    transform: return x * 1000
    group_by:
      func: diff
      duration: 5min
      start_with_last: true

try changing curve to stepline.

Also you’re using the function diff, whcih could be related: Will return the difference between the last and the first entry in the bucket. Do you need to be using group_by for this graph?

Thanks for the code and further explanation! :slight_smile:

1 Like

Even with Stepline the same thing happens.

Yes, this is an interval meter and I’m billed in 5minute increments. So I’m only really worried about the total usage for a 5 minute period.

Hi Everyone!

I’ve the following apexchart card
image

Here is the code:

type: custom:apexcharts-card
apex_config:
  chart:
    height: 200px
update_interval: 15s
graph_span: 24h
header:
  show: true
  title: Stato Inverter
  show_states: true
  colorize_states: true
all_series_config:
  stroke_width: 2
series:
  - entity: sensor.growatt_grid_voltage
    type: line
    name: Voltaggio di rete
  - entity: sensor.growatt_temperature
    type: line
    name: Temperatura inverter
  - entity: sensor.growatt_status_state
    name: Stato inverter
    show:
      in_header: raw
      in_chart: false
  - entity: sensor.growatt_fault_code
    name: Codice errore inverter
    show:
      in_header: raw
      in_chart: false

As you can see the fault_code and status_state entities are showing as NaN. I guess this is because the entity value is not numeric… Indeed that values are strings like “all good” or “AC voltage too high”

Now my question is, is it possible to show in the card any string entity to not be projected into the chart itself?

Asking about color_threshold in a radialBar chart, knowing full well it’s an experimental feature. The outer bar shows temp, and the inner bar shows humidity:

apex

- type: custom:apexcharts-card
  chart_type: radialBar
  apex_config:
    plotOptions:
      radialBar:
        offsetY: 0
        startAngle: -108
        endAngle: 108
        hollow:
          size: 70%
        dataLabels:
          name:
            show: false
          value:
            show: false
        track:
          strokeWidth: 100%
          margin: 0
    legend:
      show: false
    chart:
      height: 140
  experimental:
    color_threshold: true
  series:
    - entity: sensor.current_temperature_basement
      color_threshold:
        - value: 50
          color: blue
        - value: 60
          color: cyan
        - value: 68
          color: green
        - value: 72
          color: orange
        - value: 80
          color: red
      min: 50
      max: 90
      header_actions:
        tap_action:
          action: none
      show:
        legend_value: false
    - entity: sensor.baseboards_basement_humidity
      color_threshold:
        - value: 0
          color: green
        - value: 50
          color: orange
        - value: 80
          color: red
      min: 0
      max: 100
      header_actions:
        tap_action:
          action: none
      show:
        legend_value: false
  card_mod:
    style: |
      ha-card {
        --card-primary-font-size: 16px;
        --card-secondary-font-size: 14px;
        --ha-card-border-width: 0px;
      }

The value of sensor.current_temperature_basement is 71. The value of sensor.baseboards_basement_humidity is 49. I would expect the color of the both outer and inner bars to be green. Am I understanding this incorrectly, is it configured incorrectly, or should I chalk this up to the experimental feature just working incorrectly?

EDIT: I had the idea to turn on dataLabels value to see what it’s displaying. The inner bar shows correctly at 49, but the outer bar shows “52.5%”? Ahh… it looks like the value it’s displaying is actually the % value of the sensor between the min and the max. If the min is 50 and the max is 90, the value it’s displaying at 71 is 52.5%. That’s why the humidity bar shows correctly (min = 0, max = 100).

So… it’s the chart, not me. I guess I could fix it with some template sensors created specifically for the colors I want to see in the chart if I wanted to…

EDIT 2: Meh… I just fudged the values specified under color_thresholds by converting them to percentages between my min & max. I don’t like it, but it looks the way I want it to in the front end.

nice cards.
Could you please share the yaml code?

thx

I want to setup the following:

image

Any ideas how to realize in ApexChart?

Don’t know, if this is possible with Apex, but you do know, that there is a standard card for this? :slight_smile:

Thanks for your answere.For sure with the history graph card it is possible:

But scaling of the different x-axis do not really fit together.

1 Like

Why my chart header show wrong state?

image

Hi!
Can you help me how to align the header to the center (I tried but it doesn’t worked).
And if it is possible, how to

  • hide the row marked with 3 on the picture
  • maybe align to the right row 2
  • and mark a windows sensor opened/closed state with a vertical line.

Thanks!