ApexCharts card - A highly customizable graph card

I’m trying to create an x-axis annotation based on a template sensor value.
I managed to inlude “now”

image

And I have a template sensor that puts out the correct unix timestamp sensor.lowest_future_price_timestamp, but I don’t seem to be able to access it in the apex config - how to do this?

apex_config:
  chart:
    height: 300px
  annotations:
    xaxis:
      - x: EVAL:new Date().getTime()
        label:
          text: Now
          borderWidth: 0
          style:
            background: "#0000"
      - x: 1733695200000.0  # <-- here I want the value of sensor.lowest_future_price_timestamp
        label:
          text: Cheapest
          borderWidth: 0
          style:
            background: "#0000"

thx,

Embed the card in here iantrich/config-template-card: :memo: Templatable Lovelace Configurations

Hi. Did you get this working? Here’s a card I made yesterday. I’ll need to tidy-up the code a bit as I have some unneeded elements, but hopefully you can see the relevant part.

type: custom:apexcharts-card
graph_span: 4d
apex_config:
  xaxis:
    labels:
      style:
        colors: grey
        fontSize: 15px
  legend:
    show: true
  grid:
    borderColor: black
    strokeDashArray: 0
    yaxis:
      lines:
        show: true
header:
  standard_format: true
  show: true
  title: Central Heating
  show_states: true
  colorize_states: true
yaxis:
  - id: target_temp
    show: false
    opposite: false
    max: ~30
    min: 15
    apex_config:
      decimalsInFloat: 0
      tickAmount: 15
      labels:
        style:
          colors: yellow
  - id: current_temp
    opposite: false
    max: ~30
    min: 15
    apex_config:
      decimalsInFloat: 0
      tickAmount: 15
      title:
        text: ""
        style:
          color: CornflowerBlue
      labels:
        style:
          colors: CornflowerBlue
  - id: humidity
    opposite: true
    max: 100
    min: 0
    apex_config:
      decimalsInFloat: 0
      tickAmount: 10
      title:
        text: ""
        style:
          color: DarkOliveGreen
      labels:
        style:
          colors: DarkOliveGreen
  - id: heating
    show: false
    opposite: true
    max: 100
    min: 0
series:
  - entity: binary_sensor.central_heating
    yaxis_id: heating
    name: Heating
    opacity: 0.2
    transform: "return x === \"on\" ? 100 : 0;"
    type: area
    color: orangered
    stroke_width: 0
    curve: stepline
    show:
      extremas: false
      in_header: false
      name_in_header: false
      legend_value: false
  - entity: sensor.attic_temperature
    yaxis_id: current_temp
    name: Current temp
    fill_raw: last
    type: line
    color: CornflowerBlue
    stroke_width: 2
    show:
      extremas: false
      legend_value: false
  - entity: climate.attic
    attribute: temperature
    yaxis_id: current_temp
    name: Target temp
    type: line
    color: DarkGoldenrod
    stroke_width: 1
    stroke_dash: 4
    curve: stepline
    show:
      header_color_threshold: true
      extremas: false
      in_header: true
      name_in_header: true
      legend_value: false
  - entity: sensor.attic_humidity
    yaxis_id: humidity
    name: RH
    fill_raw: last
    type: line
    color: DarkOliveGreen
    stroke_width: 1
    show:
      extremas: false
      legend_value: false
1 Like

@vingerha , thanks for the idea, unfortunately I couldn’t manage to install the “config-template-card” addon - I tried it via HACS (worked fine for apex-charts and custom-weather), THe folder structure looks fine

➜  community pwd
/config/www/community
➜  community l
total 20K
drwxr-xr-x 5 root root 4.0K Dec  7 17:58 .
drwxr-xr-x 3 root root 4.0K Dec  4 10:05 ..
drwxr-xr-x 2 root root 4.0K Dec  4 10:05 apexcharts-card
drwxr-xr-x 2 root root 4.0K Dec  7 17:58 config-template-card
drwxr-xr-x 4 root root 4.0K Dec  5 09:00 weather-chart-card

but it doesn’t even run a simple hello world example.

@silentnomad not sure if the reply was for me (just started with HA and this forum), but if so, I cannot make sense out of it :see_no_evil:

read the documentation… you need to add entities

EDIT: and 'markdown" is not the best of cards to test this as it accepts a lot of template stuff by default :slight_smile:

@vingerha thx for the pointer - I’m still stuck though. I think I managed to integrate the apex chart with the config-template-card, but I have no clue how to access the value of the entity… (I’m just learning HA concepts and I’m really bad at JS)

The entity contains the correct value, checked it via the developer tools.
I tried the below Ideas, but only when I paste the value, I get the desired annotation

type: custom:config-template-card
entities:
  - sensor.lowest_future_price_timestamp
card:
  type: custom:apexcharts-card
  apex_config:
    annotations:
      xaxis:
        # - x: EVAL:entities['sensor.lowest_future_price_timestamp'].state
        # - x: sensor.lowest_future_price_timestamp
        # - x: {{sensor.lowest_future_price_timestamp}}
        - x: 1733709600000
          label:
            text: Warmwasser
            borderWidth: 0
            style:
              background: "#0000"
  header

any help appreciated.

If the sensor contains a unixtimestamp, it should be something like

  - x: "${states['sensor.lowest_future_price_timestamp'].state}"
```

EDIT: how to get values from sensors is nicely documented so rtfm please
1 Like

Thx! That worked :slight_smile:
tried to rtfm, but I couldn’t make any sense out of the examples and I have no clue how to debug this (tried EVAL console.log & F12 - but with limited success)

Hey!! Thanks for getting back to my query.

I actually managed to do it by creating a template sensor returning specific values.

image

Having a look at your solution though it seems more elegant, as you do the value definition directly in the card. I might have a go at doing it like that. Thanks for sharing your config, it will also come in handy as I wanted to add some secondary Y-axis as well at some point.

Thanks!

Hello
I need help to get attributes from an SQL entity to show custom tooltip on Apexcharts

Here my entity :

url: sqlite:////config/trips.db
column: price
request:
          SELECT ph.date, MIN(ph.price) as price, h.name as hotel_name, t.date as trip_date
          FROM price_history ph
          JOIN trips t ON ph.trip_id = t.id
          JOIN hotels h ON t.hotel_id = h.id
          JOIN hotel_locations hl ON h.id = hl.hotel_id
          JOIN locations l ON hl.location_id = l.id
          WHERE l.code = 'E1.MAH'
          GROUP BY ph.date
          ORDER BY ph.date;

The dev tools gives this response :

state: 653.0
attributes:
          state_class: measurement
          date: "2024-12-08 02:11:01.000000"
          price: 653
          hotel_name: Hotel Artiem Capri 4*
          trip_date: 24 juil. 2025
          unit_of_measurement: €
          device_class: monetary
          friendly_name: trip_prices trip_prices

And here is my Apexchart :

type: custom:apexcharts-card
graph_span: 4d
header:
  show: true
  title: Trip price
  show_states: true
  colorize_states: true
series:
  - entity: sensor.trip_prices
    name: Trip prices
    type: line
    show:
      legend_value: true
apex_config:
  tooltip:
    show: true
    shared: true
    intersect: false
    custom: |
      EVAL:function a({ series, seriesIndex, dataPointIndex, w }) {
        const data = w.globals.initialSeries[seriesIndex].data[dataPointIndex];
        const scrapeDate = new Date(data[0]); 
        const scrapeDateStr = scrapeDate.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit' });
        
        return `<ul>
            <li>Price : ${data[1]} €</li>            
            <li>Hotel : ${data.hotel_name}</li>
            <li>TripDate : ${data.trip_date}</li>
            <li>ScrapDate : ${scrapeDateStr}</li>
          </ul>`;
      }

image

But the tooltip show undefined for hotel and TripDate because I can’t get attributes, anyone have an idea ?

Not (!) the specialist but I believe this method does not allow to kick back attributes. You can see this when you use ‘data’ only, 0=stamp, 1=state.
A possible (?) workaround would be to have the sql push all params in the state and then you can use data[1][0], data[1][1] etc.

Oddly I only get the extrema:min+time / max+time showing on my charts when my phone time zone is GMT+1100.

I found this out by traveling to another timezone and being surprised when the extremas labels showed up.

When at my home location GMT+1000, the extrema labels disappear, which is now very annoying.

Any ideas what is going on and how can I get them to display at both locations?

Update:

This issue occurs on both my phone and laptop, when they are in one timezones thee extremes show in another timezone they don’t.

Here is the code card:

type: custom:apexcharts-card
apex_config:
  chart:
    height: 200px
    width: 100%
experimental:
  color_threshold: true
graph_span: 36h
span:
  start: minute
  offset: "-0h"
header:
  show: true
  title: Battery, Price & Cost Forecast
  show_states: true
  colorize_states: true
now:
  show: true
  label: Now
series:
  - entity: sensor.unit_load_cost
    name: Load Cost
    float_precision: 2
    yaxis_id: first
    curve: stepline
    extend_to: false
    show:
      in_header: before_now
      legend_value: false
      extremas: min+time
    stroke_width: 1
    color: red
    unit: $/kWh
    data_generator: |
      return entity.attributes.unit_load_cost_forecasts.map((entry) => {
        return [new Date(entry.date), entry.unit_load_cost];
      });
  - entity: sensor.unit_prod_price
    float_precision: 2
    yaxis_id: first
    curve: stepline
    unit: $/kWh
    extend_to: false
    show:
      in_header: before_now
      legend_value: false
      extremas: max+time
    color: PURPLE
    name: APF FeedIn
    stroke_width: 2
    data_generator: |
      return entity.attributes.unit_prod_price_forecasts.map((entry) => {
        return [new Date(entry.date), entry.unit_prod_price];
      });
  - entity: sensor.amberapi_command_line
    attribute: perKwhFeedIn
    name: AEMO Spot Forecast
    float_precision: 2
    yaxis_id: first
    curve: stepline
    extend_to: false
    show:
      in_header: before_now
      legend_value: false
    color: orange
    stroke_width: 1
    unit: $/kWh
    data_generator: |
      return entity.attributes.startTime.map((time, index) => {
        return [new Date(time), entity.attributes.perKwhFeedIn[index]];
      });       
  - entity: sensor.soc_batt_forecast
    float_precision: 0
    yaxis_id: second
    time_delta: +30m
    extend_to: false
    show:
      in_header: before_now
      legend_value: false
    color: green
    stroke_width: 1
    name: battery SOC
    data_generator: |
      return entity.attributes.battery_scheduled_soc.map((entry) => { 
        return [new Date(entry.date), entry.soc_batt_forecast]; });
yaxis:
  - id: first
    decimals: 1
    max: 1
    min: 0
    apex_config:
      tickAmount: 2
      logarithmic: false
  - id: second
    show: false
    opposite: false
    decimals: 1
    max: 100
    min: 0
    apex_config:
      tickAmount: 2
view_layout:
  position: main

Any code, card shown where, on the other-TZ-phone or laptop? Cannot guess…

Hi everyone,

I have a question regarding setting up a chart with Apex Charts:

Is it possible to configure the X-axis to always display the 24 hours of a day, starting with midnight on the left, noon in the center, and ending with midnight on the right? I’d like this axis to remain fixed regardless of the current time.

Additionally, I want to:

  1. Display a “Now” indicator that moves dynamically across the chart as time progresses.
  2. Show the stats of the previous day as dashed lines.
  3. Gradually replace the previous day’s stats with today’s stats as the hours go by.

The goal is to have a seamless transition where the current stats overwrite the previous day’s stats on the fixed X-axis.

Is this achievable in Apex Charts? If yes, could you point me in the right direction or share any examples?

Thanks in advance!
Solved No. 1

  graph_span: 24h
  span:
    start: day
    offset: 0

No.2 and 3 - How can I show the values of the previous day and also forcasts only on the right side of the red time indicator?
so everything left of it is past and everything right of it is predictions

1 Like

Cannot put my finger on it but there are more things ‘fishy’. When using graph_span: 11h it shows 24dec, with 10h and 12h all is fine. I suggest playing around with span and with 1 series first to see if one of them affects display

1 Like

I am trying to make a pie chart from attributes of sensor called: sensor.carbon_intensity_south_east_england_current_rating

here are the attributes:

from: '2024-12-12T12:30:00+00:00'
to: '2024-12-12T13:00:00+00:00'
intensity_forecast: 249
generation_mix:
  - fuel: biomass
    perc: 0
  - fuel: coal
    perc: 0
  - fuel: imports
    perc: 40.1
  - fuel: gas
    perc: 55.9
  - fuel: nuclear
    perc: 0
  - fuel: other
    perc: 0
  - fuel: hydro
    perc: 2.5
  - fuel: solar
    perc: 0.5
  - fuel: wind
    perc: 0.9

unit_of_measurement: gCO2/kWh
icon: mdi:leaf
friendly_name: Current Rating (South East England)

I came up with this:

type: custom:apexcharts-card
chart_type: pie
header:
  show: true
  show_states: true
  colorize_states: true
  title: Energy mix today
series:
  - entity: sensor.carbon_intensity_south_east_england_current_rating
    data_generator: |
      return entity.attributes.generation_mix.map((entry) => {
         return [entry.fuel, entry.perc];
       });

but that is just displaying empty graph saying Loading…
what is wrong with my config? Anyone can point me in the right direction?
thanks

I do not think this will work as is will require a ‘entity’ for each slice. There is very little documentation on pie-charts and none of them use the data_generator

Sorry - this might be a daft question:

RomRider’s apexcharts-card suits a need I have very well - except I’d like it to be half the width - which it would be quite comfortable in with my data and headings.

It has an explicit option to scale the chart width (and height) but i don’t see one for scaling the entire card.

Have I missed a more general approach to this - perhaps something to do with layouts?

Many thanks,

Hi. No problem. My code could do with a bit of a tidy-up as I was still experimenting at the time.

Hi,

I’m using an Apex card to plot the price of electricity per hour, by following a great tutorial from doe duurzaam. This works nice, and results in the following card:

On top of this, I would now like to plot my hourly energy consumption. This would allow me to see to which extend we are capable of not taking energy from the grid when the price is high.
I have the utility sensor set up in homeassistant, and thought of plotting that hourly energy consumption as a second bar, however on the second y-axis given the different units. Unfortunately, this is the result:


As you might appreciate, this is not really something useful :slight_smile: I’ve been playing with this for 2 days now, but can’t figure out what I’m doing wrong. Anyone here that could help me to get this working?

This is the code of the last graph:

      - type: custom:apexcharts-card                                                                                                                                                                              
        update_interval: 5m                                                                                                                                                                                       
        graph_span: 24h #48h                                                                                                                                                                                      
        span:                                                                                                                                                                                                     
          start: day                                                                                                                                                                                              
          #offset: -12h                                                                                                                                                                                           
        now:                                                                                                                                                                                                      
          show: true                                                                                                                                                                                              
          label: nu                                                                                                                                                                                               
        header:                                                                                                                                                                                                   
            show: true                                                                                                                                                                                            
            show_states: true                                                                                                                                                                                     
            colorize_states: true                                                                                                                                                                                 
            title: Stroomprijs vandaag                                                                                                                                                                            
        experimental:                                                                                                                                                                                             
            color_threshold: true                                                                                                                                                                                 
        series:                                                                                                                                                                                                   
          - entity: sensor.average_electricity_price                                                                                                                                                              
            yaxis_id: Prijs                                                                                                                                                                                       
            stroke_width: 2                                                                                                                                                                                       
            float_precision: 3                                                                                                                                                                                    
            type: column                                                                                                                                                                                          
            opacity: 1                                                                                                                                                                                            
            color_threshold:                                                                                                                                                                                      
              - value: -0.10                                                                                                                                                                                      
                color: '#000fff'                                                                                                                                                                                  
              - value: 0                                                                                                                                                                                          
                color: '#00ffd5'                                                                                                                                                                                  
              - value: 0.10                                                                                                                                                                                       
                color: '#ff8000'                                                                                                                                                                                  
              - value: 0.20                                                                                                                                                                                       
                color: '#0080ff'                                                                                                                                                                                  
              - value: 0.30                                                                                                                                                                                       
                color: '#ff2a00'                                                                                                                                                                                  
              - value: 1                                                                                                                                                                                          
                color: '#ff00d4'                                                                                                                                                                                  
            data_generator: |                                                                                                                                                                                     
              return entity.attributes.prices.map((record, index) => {                                                                                                                                            
                return [record.time, record.price];                                                                                                                                                               
              });                                                                                                                                                                                                 
                                                                                                                                                                                                                  
          - entity: sensor.hourly_energy_delivered                                                                                                                                                                
            type: column                                                                                                                                                                                          
            stroke_width: 2                                                                                                                                                                                       
            opacity: 1                                                                                                                                                                                            
            yaxis_id: second # this serie will be associated to the 'id: second' axis.                                                                                                                            
        yaxis:                                                                                                                                                                                                    
          - id: Prijs                                                                                                                                                                                             
            decimals: 2                                                                                                                                                                                           
          - id: second # identification name of the second y-axis                                                                                                                                                 
            opposite: true # make it show on the right side                                                                                                                                                       
                                                                                           

Thank you in advance,
Erwin