ApexCharts card - A highly customizable graph card

Hello,

Can anyone say why it doesn’t show values of the last two sensors?

Sensor ”Yhteensä / kk” is a collector of sensor ”Yhteensä” and is cleared monthly. Further, ”Yhteensä” is a template sensor that sums sensors ”Kotona / kk” and ”Muualla / kk”. All the sensor values are shown below the chart.

Sorry, working on phone and wasn’t able to copy the code (formatting errors), therefore the screenshot:

It seems to have something to do with the statistics option, because the values are visible if I remove the option. But I want to have monthly values instead.

Never mind. I gave up and show the series as stacked so the sum now visible in that way.

However, the problem still is that it takes long time to see the updated sensor value in the Apexchart. I have forced the chart to update every 10s (I can see the spinning icon), but the sensor values are not just updated. Currently I have waited 10 minutes it to update the new values. If I click the sensor value in the header, HA history graph pops up and shows the correct value. Why doesn’t the Apexchart?

edit. It took 40 minutes to update the value to the chart. What could be are a reason for this? So it would seem like the sensor values are updated only once per 40 minutes to the chart, not after every value update as I would interpret it from the docs.

In the last Days I made a bit Progress.
I have found the way to set the Data Label Position.
Unfortunately I can only set the Position for all.

Is there a way how I can set a different Label-Position for each Entity?
I want the Labels of the dark Blue Bars at the top.
Chart

How can i invert the values of the second y-axes?

Instead to go upwards 0 to 3.7 it should show 0 to -3.7

image

Ciao, can you share the complete code of your card?
I am using Tado too, so i want to check his status like you do…
Garzie in advance…

Versione 1 (originale)

Codice:

type: vertical-stack
cards:
  - type: custom:mushroom-title-card
    title: Temperatura e umidità
    alignment: center
  - type: custom:mushroom-chips-card
    chips:
      - type: template
        double_tap_action:
          action: none
        icon: mdi:minus
        tap_action:
          action: call-service
          service: counter.decrement
          data: {}
          target:
            entity_id: counter.grafici_temperatura_ore
        hold_action:
          action: call-service
          service: counter.reset
          data: {}
          target: {}
      - type: template
        icon: mdi:chart-box-outline
        content: >-
          {{ states('counter.grafici_temperatura_ore') }}

          {% if states('counter.grafici_temperatura_ore') | int == 1 %} ora nei
          grafici
            {% else %} ore nei grafici
            {% endif %}
        tap_action:
          action: more-info
        entity: counter.grafici_temperatura_ore
      - type: template
        icon: mdi:plus
        content: ''
        tap_action:
          action: call-service
          service: counter.increment
          data: {}
          target:
            entity_id: counter.grafici_temperatura_ore
        double_tap_action:
          action: none
        hold_action:
          action: call-service
          service: counter.increment
          data: {}
          target:
            entity_id: counter.grafici_temperatura_ore
    alignment: center
  - type: custom:config-template-card
    entities:
      - counter.grafici_temperatura_ore
    card:
      type: custom:apexcharts-card
      update_interval: 1m
      graph_span: ${{states['counter.grafici_temperatura_ore'].state+'h'}}
      header:
        show: true
        title: Temperatura soggiorno
        show_states: true
        colorize_states: false
      series:
        - entity: sensor.grafico_riscaldamento_soggiorno_sta_riscaldando
          name: Sta riscaldando
          stroke_width: 0
          type: column
          color: '#F5B041'
          opacity: 0.3
          show:
            in_header: false
            legend_value: false
          group_by:
            func: avg
            duration: 2m
        - entity: sensor.tado_riscaldamento_soggiorno_temperature
          name: Temp attuale
          stroke_width: 3
          type: area
          color: '#f8e07d'
          opacity: 0.25
          unit: °C
          show:
            extremas: true
            name_in_header: false
            in_header: true
          group_by:
            func: avg
            duration: 2m
        - entity: climate.tado_riscaldamento_soggiorno
          attribute: temperature
          name: Temp desiderata
          stroke_width: 1.5
          type: line
          color: '#CC3333'
          opacity: 1
          unit: °C
          show:
            extremas: false
            in_header: false
          group_by:
            func: avg
            duration: 2m
  - type: custom:config-template-card
    entities:
      - counter.grafici_temperatura_ore
    card:
      type: custom:apexcharts-card
      update_interval: 1m
      graph_span: ${{states['counter.grafici_temperatura_ore'].state+'h'}}
      header:
        show: true
        title: Umidità soggiorno
        show_states: true
        colorize_states: false
      series:
        - entity: sensor.tado_riscaldamento_soggiorno_humidity
          name: Umidità
          stroke_width: 3
          type: area
          color: '#7AB7FF'
          opacity: 0.15
          show:
            extremas: true
            name_in_header: false
            in_header: true

Versione 2 (senza temperatura desiderata; non vedi il grafico colorato solo perché in questi giorni l’aria condizionata è spenta)

Codice:

type: vertical-stack
cards:
  - type: custom:mushroom-title-card
    title: Grafici
    alignment: center
  - type: custom:vertical-stack-in-card
    cards:
      - type: entities
        entities:
          - type: custom:slider-entity-row
            entity: >-
              input_number.ac_app_9_camera_ale_grafici_temperatura_e_umidita_numero_di_ore_da_mostrare
            full_row: false
            grow: true
            name: Ore da mostrare nei grafici
        state_color: false
        show_header_toggle: false
        card_mod:
          style: |
            ha-card {
              background: transparent;
              border-style: none;
              padding-top: 22px;
            }
      - type: custom:config-template-card
        entities:
          - >-
            input_number.ac_app_9_camera_ale_grafici_temperatura_e_umidita_numero_di_ore_da_mostrare
        card:
          type: custom:apexcharts-card
          update_interval: 1m
          graph_span: >-
            ${{states['input_number.ac_app_9_camera_ale_grafici_temperatura_e_umidita_numero_di_ore_da_mostrare'].state+'h'}}
          header:
            show: true
            title: Temperatura
            show_states: true
            colorize_states: false
          series:
            - entity: sensor.ac_app_9_camera_ale_grafico_sta_raffrescando
              name: Sta raffrescando
              stroke_width: 0
              type: area
              color: '#F5B041'
              opacity: 0.3
              show:
                in_header: false
                legend_value: false
            - entity: climate.ac_app_9_camera_ale_condizionatore
              attribute: current_temperature
              name: Temperatura
              stroke_width: 2
              type: area
              color: '#f8e07d'
              opacity: 0.25
              unit: °C
              fill_raw: last
              show:
                extremas: true
                name_in_header: false
                in_header: false
          card_mod:
            style: |
              ha-card {
                background: transparent;
                border-style: none;
              }
      - type: custom:config-template-card
        entities:
          - >-
            input_number.ac_app_9_camera_ale_grafici_temperatura_e_umidita_numero_di_ore_da_mostrare
        card:
          type: custom:apexcharts-card
          update_interval: 1m
          graph_span: >-
            ${{states['input_number.ac_app_9_camera_ale_grafici_temperatura_e_umidita_numero_di_ore_da_mostrare'].state+'h'}}
          header:
            show: true
            title: Umidità
            show_states: true
            colorize_states: false
          series:
            - entity: climate.ac_app_9_camera_ale_condizionatore
              attribute: current_humidity
              name: Umidità
              stroke_width: 2
              type: area
              color: '#7AB7FF'
              unit: '%'
              opacity: 0.15
              fill_raw: last
              show:
                extremas: true
                name_in_header: false
                in_header: false
          card_mod:
            style: |
              ha-card {
                background: transparent;
                border-style: none;
              }

Versione 3 (quella che mi piace di più, ma ti sconsiglio di utilizzare perché purtroppo il creatore del tipo di grafico utilizzato non porterà avanti il progetto; non vedi il grafico colorato di verde solo perché l’aria condizionata è spenta in questi giorni)

type: vertical-stack
cards:
  - type: custom:mushroom-title-card
    title: Grafici
    alignment: center
  - type: custom:history-explorer-card
    cardName: Appartamento 9
    graphs:
      - type: line
        title: Temperatura (verde = sta raffrescando)
        entities:
          - entity: sensor.ac_app_9_camera_andrea_grafico_temperatura_sta_raffrescando
            name: sta raffrescando
            color: RGBA(60,179,113,0.5)
            fill: RGBA(60,179,113,0.5)
            width: 0
            process: |
              ( state > 0 ) ? state = Math.round(state*10)/10 : "unavailable"
          - entity: sensor.temperatura_app_9_camera_andrea
            name: temperatura
            color: rgba(244, 209, 96, 1)
            width: 2
            fill: rgba(244, 209, 96, 0.2)
            process: |
              state = Math.round(state*10)/10
        options:
          showSamples: false
      - type: line
        title: Umidità (verde = sta raffrescando)
        entities:
          - entity: sensor.ac_app_9_camera_andrea_grafico_umidita_sta_raffrescando
            name: sta raffrescando
            color: RGBA(60,179,113,0.5)
            fill: RGBA(60,179,113,0.5)
            width: 0
            process: |
              ( state > 0 ) ? state : "unavailable"
          - entity: sensor.umidita_app_9_camera_andrea
            name: umidità
            color: rgba(122, 183, 255, 1)
            width: 2
            fill: rgba(122, 183, 255, 0.15)
            process: |
              state = Math.round(state)
        options:
          showSamples: false
    showCurrentValues: true
    combineSameUnits: true
    refresh:
      interval: 60
    defaultTimeRange: 12h
    defaultTimeOffset: 15m
    header: hide
    uiLayout:
      toolbar: bottom
      selector: hide
    labelsVisible: false
    labelAreaWidth: 35
    tooltip:
      showDuration: true
    showUnavailable: true
    legendVisible: false
    lineMode: curves
    uiColors:
      buttons: rgba(0,0,0,0.05)

Le soluzioni 1 e 2 richiedono di creare un template sensor che ho dettagliato a lungo nei miei precedenti messaggi e che ti scrivo qua:

template:
  #### GRAFICI RISCALDAMENTO (codice aggiunto il 10 gennaio 2023) E RAFFRESCAMENTO (codice aggiunto il 1 giugno 2023)
  #### Sensori creati per avere la serie "Sta riscaldando / sta raffrescando" nei grafici relativi alla temperatura delle stanze. La logica è la seguente:
  #### Ho creato un apex-chart card volto a mostrare il grafico del riscaldamento / raffrescamento in ogni stanza.
  #### Il grafico ha tre serie:
  #### 1) serie uno: temperatura attuale
  #### 2) serie due: temperatura desiderata
  #### 3) serie tre: sta riscaldando / sta raffrescando -> serire che colorara il grafico se tado sta riscaldando / l'aria condizionata sta raffrescando.
  #### Per la serie tre ho dovuto creare dei sensori fittizi (i vari template sensor qui sotto creati) tali per cui
  #### - quando il riscaldamento / aria condizionata è 0 -> il sensore non fornisce alcun valore (risultato ottenuto con la funzione availability, la quale disattiva il sensore [availabiliy = 0] quando tado non sta riscaldando / l'aria condizionata non sta raffrescando)
  #### - quando il riscaldamento / aria condizionata è diverso da 0 -> fornisce il medesimo valore della temperatura attuale, cosicchè ho una serie che si sovrappone perfettamente alla temperatura attuale

  - sensor:
      - name: Grafico riscaldamento soggiorno - sta riscaldando
        state: "{{ states('sensor.tado_riscaldamento_soggiorno_temperature') | float }}"
        availability: "{{ states('sensor.tado_riscaldamento_soggiorno_heating') | int}}"

La soluzione 3 richiede il seguente template sensor:

template:
  - sensor:
      - name: ac - app 9 - camera Andrea - grafico temperatura - sta raffrescando
        state: >
          {% if is_state("climate.ac_app_9_camera_andrea_condizionatore","cool") -%}
            {{ states("sensor.temperatura_app_9_camera_andrea") }}
          {%- else -%}
            0
          {%- endif %}
  - sensor:
      - name: ac - app 9 - camera Andrea - grafico umidità - sta raffrescando
        state: >
          {% if is_state("climate.ac_app_9_camera_andrea_condizionatore","cool") -%}
            {{ states("sensor.umidita_app_9_camera_andrea") }}
          {%- else -%}
            0
          {%- endif %}

:warning: Naturalmente devi scaricare tutte le card HACS che vedi utilizzate nel codice

Ciao!

2 Likes

Grazie, can you tell me how you configure the counter?

Seems like a common want, but I haven’t found anything. Is there a way (seems like it would be possible in JS if nothing else, but way beyond my ability) to add thousands separator commas to an axis label? So 1000 2000 3000 4000 would show as 1,000 2,000 3,000 4,000, etc. on the axis.

1 Like

It’s a banal helper! You can create it through UI or through yaml… You can use an input number or a counter. It’s the same!

UI example:

Yaml example:

input_number:
  # GRAFICO (numero di ore da mostrare nei grafici di temperatura e umidità)
  riscaldamento_app_9_camera_andrea_ore_da_mostrare_nei_grafici:
    name: riscaldamento - app 9 - camera Andrea - ore da mostrare nei grafici
    icon: mdi:chart-areaspline
    min: 1.0
    max: 24.0
    step: 1.0
    unit_of_measurement: h
    mode: slider
1 Like

I’m sorry if I missed it but, how do you make the chart as wide as the screen? No matter what I do it only takes up the same width.

Click the “pencil” in top right corner, the click pencil to the left, edit view, chose Panel view

If you want more cards in your view, you should either use Grid, or start with a vertical-stack

You use most likely Masonry-View

1 Like

Thank you very much.

1 Like

I’m struggling with setting dynamically axis min and max.

I haved used custom:config-template-card successfully for dynamic graph span, but cannot do it for min/max, because it’'s a numeric value that confic template card gives to apex chart. Numeric values are given with ‘-7384.97’ meaning number between singlequotes, but Apex does not accept single quotes. For span add the “h” character to the numeric value and then confic template card gives the result without singlequotes and it works.

here is my code:

type: custom:config-template-card
variables:
  hours: '`${states[''input_number.total_nordpool_hours_available''].state -1}h`'
  maxY: '`${states[''sensor.maximum_forecast_value''].state}`'
  minY: '`${states[''sensor.minimum_forecast_value''].state}`'
entities:
  - input_number.nordpool_available_hours
  - sensor.maximum_forecast_value
  - sensor.minimum_forecast_value
card:
  type: custom:apexcharts-card
  graph_span: ${hours}
  span:
    start: minute
  header:
    show: true
    title: EMHASS  Optimization Plan
    show_states: true
    colorize_states: true
  now:
    show: false
    label: now
  yaxis:
    - min: ${minY}
      max: ${maxY}
      decimals: 2
      apex_config:
        forceNiceScale: true
        tick_amount: 4
  series:
    - entity: sensor.p_pv_forecast
      curve: stepline
      stroke_width: 2
      color: '#FFD966'
      show:
        in_header: false
        legend_value: false
      data_generator: |
        return entity.attributes.forecasts.map((entry) => {
          return [new Date(entry.date), entry.p_pv_forecast];
        });
    - entity: sensor.p_load_forecast
      curve: stepline
      type: line
      color: '#5AB4DE'
      show:
        in_header: false
        legend_value: false
      stroke_width: 2
      data_generator: |
        return entity.attributes.forecasts.map((entry) => {
          return [new Date(entry.date), entry.p_load_forecast];
        });
    - entity: sensor.p_grid_forecast
      curve: stepline
      color: '#FF7E7E'
      type: area
      show:
        in_header: false
        legend_value: false
      stroke_width: 1
      data_generator: |
        return entity.attributes.forecasts.map((entry) => {
          return [new Date(entry.date), entry.p_grid_forecast];
        });
    - entity: sensor.p_batt_forecast
      curve: stepline
      color: '#8CC63F'
      type: area
      show:
        in_header: false
        legend_value: false
      stroke_width: 1
      data_generator: |
        return entity.attributes.battery_scheduled_power.map((entry) => {
          return [new Date(entry.date), entry.p_batt_forecast];
        });
    - entity: sensor.total_cost_fun_value
      unit: €
      name: Plan Value
      show:
        legend_value: false
        in_chart: false
    - entity: sensor.solcast_pv_forecast_forecast_today
      unit: kWh
      name: Solar Production Forecast
      show:
        legend_value: false
        in_chart: false
  view_layout:
    position: main

And error:

/// apexcharts-card version 2.0.4 /// Bad yaxis min/max format: -7375.97
type: custom:apexcharts-card
graph_span: 11h
span:
  start: minute
header:
  show: true
  title: EMHASS  Optimization Plan
  show_states: true
  colorize_states: true
now:
  show: false
  label: now
yaxis:
  - min: '-7375.97'
    max: '6255.39'
    decimals: 2
    apex_config:
      forceNiceScale: true
      tick_amount: 4
series:
  - entity: sensor.p_pv_forecast
    curve: stepline
    stroke_width: 2
    color: '#FFD966'
    show:
      in_header: false
      legend_value: false
    data_generator: |
      return entity.attributes.forecasts.map((entry) => {
        return [new Date(entry.date), entry.p_pv_forecast];
      });
  - entity: sensor.p_load_forecast
    curve: stepline
    type: line
    color: '#5AB4DE'
    show:
      in_header: false
      legend_value: false
    stroke_width: 2
    data_generator: |
      return entity.attributes.forecasts.map((entry) => {
        return [new Date(entry.date), entry.p_load_forecast];
      });
  - entity: sensor.p_grid_forecast
    curve: stepline
    color: '#FF7E7E'
    type: area
    show:
      in_header: false
      legend_value: false
    stroke_width: 1
    data_generator: |
      return entity.attributes.forecasts.map((entry) => {
        return [new Date(entry.date), entry.p_grid_forecast];
      });
  - entity: sensor.p_batt_forecast
    curve: stepline
    color: '#8CC63F'
    type: area
    show:
      in_header: false
      legend_value: false
    stroke_width: 1
    data_generator: |
      return entity.attributes.battery_scheduled_power.map((entry) => {
        return [new Date(entry.date), entry.p_batt_forecast];
      });
  - entity: sensor.total_cost_fun_value
    unit: €
    name: Plan Value
    show:
      legend_value: false
      in_chart: false
  - entity: sensor.solcast_pv_forecast_forecast_today
    unit: kWh
    name: Solar Production Forecast
    show:
      legend_value: false
      in_chart: false
view_layout:
  position: main

Problem is here. Should be without single quotes to work:

yaxis:
  - min: '-7375.97'
    max: '6255.39'

Would be nice if apex chart accept the values in single quotes. If anyone have some ideas how to overcome this, would be great

Try this to see if it solves your issue:

type: custom:config-template-card
variables:
  hours: '`${states[''input_number.total_nordpool_hours_available''].state -1}h`'
  maxY: '`${states[''sensor.maximum_forecast_value''].state *1}`'
  minY: '`${states[''sensor.minimum_forecast_value''].state *1}`'

Thanks for your reply, but nope, still has single quotes:

/// apexcharts-card version 2.0.4 /// Bad yaxis min/max format: -12000
yaxis:

  • min: ‘-12000’
    max: ‘13804.11’

It seems thay every numeric value from config-template-card will be handled in apex chart with single quotes. Or to precise, config-template-card will serve numeric values with single quotes.

EDIT: This did the trick:

 yaxis:
    - min: '${parseFloat(minY)}'
      max: '${parseFloat(maxY)}'
1 Like

How possible is it to get the additional Apexchart types available in this - especially the polar chart would be uniquely useful as there’s no other chart like this for home assistant?

hi,
i have to apex charts below each other using the vertical stack.
but how can i align them properly?
the second chart (not the axes) starts a little bit more to the left, than the first one.
can i adjust this in any way?
Thanks

Anyone can advise how can I display entity attribute instead of entity value?

type: custom:apexcharts-card
span:
  offset: '-1sec'
apex_config:
  chart:
    type: area
    height: 250
  stroke:
    show: true
    width: 1
    curve: smooth
  legend:
    show: false
  dataLabels:
    enabled: true
  fill:
    type: gradient
    gradient:
      shadeIntensity: 0.1
      opacityFrom: 0.25
      opacityTo: 1
      inverseColors: true
      stops:
        - 0
        - 90
        - 100
header:
  show: true
  show_states: true
  colorize_states: true
graph_span: 12h
update_interval: 1h
series:
  - color: rgb(138,43,226)
    entity: sensor.home_pm2_5
    attribute: percent
    type: column
  - color: rgb(235,10,104)
    entity: sensor.home_pm1
    attribute: percent
    type: column
  - color: rgb(19,173,24)
    entity: sensor.home_pm10
    attribute: percent
    type: column
all_series_config:
  curve: smooth
  stroke_width: 30
  statistics:
    type: mean
    period: hour
  show:
    extremas: true

When I’m trying like above:

`entity: sensor.home_pm10
attribute: percent`

it doesn’t work.

I would like to separate power and temperature into 2 different axis on the left and right side. How can I do that?

I would like to draw a graph of daily usage of electric power. I am drawing a sensor sensor.emporiavue2_total_daily_energy and graph looks like this.

Now, the problem. I am drawing the value of sensor.emporiavue2_total_daily_energy and graph says today’s total daily power consumption is 4580 Wh. However, current reading of this value on other card says that the correct value is 4815 Wh. Why?

Another thing: I would like that labels on a bar chart have unit also (so 4580 Wh and not just number). And maybe a legend under the graph?

And the third question. I am showing daily energy consumption on a header (left). I would like additionally show current total power (emporiavue2_total_power), but just in header, not in the graph. How to do that?