Anyone have an ApexChart with a templated Span Offset?

Hi.
I have an Apexchart for energy usage. I wanted to add buttons to be able to dynamically scroll back through historical data by changing the Offset using a template that picks up a helper value. I’ve used the Custom:Card_Templater to allow the Span Offset to become Span Offset_Template.

The good news is that it works. My Helper Buttons allow me to wind back through historical days of energy usage.

The problem is that when the dashboard is shown, for about 2 seconds ApexCharts complains that Offset value is not valid. Its as if card-templater takes 2 seconds to work out that we’re trying to pull offset from a helper via a template.

Anyone have this working seamlessly without an error being thrown for 2 seconds every tim the graph is shown?

thanks

type: custom:card-templater
entities:
  - input_text.graph_helper_offset_days
  - input_text.graph_helper_offset_title

card:
  type: custom:apexcharts-card
  graph_span: 24h
  update_interval: 10m
  span:
    start: day
    offset_template: '{{ states(''input_text.graph_helper_offset_days'') }}'

This is mine, offset is on series-level

 - type: vertical-stack
          cards:
            - type: custom:card-templater
              card:
                type: custom:apexcharts-card
                graph_span: 30h
                header:
                  show: true
                  title: Conso ELEC temps réel
                  show_states: true
                  colorize_states: true
                series:
                  - entity: sensor.linky_daily
                    curve: smooth
                    color: '#df6366'
                    offset_template: '-{{ states(''input_number.offset_days'') }}d'
                    type: column
                    opacity: 0.6
                    stroke_width: 1
                    statistics:
                      type: state
                  - entity: sensor.linky_daily
                    curve: smooth
                    offset_template: '-{{ states(''input_number.offset_days_2'') }}d'
                    color: green
                    type: column
                    opacity: 0.1
                    stroke_width: 0.2
                    statistics:
                      type: state
              entities:
                - entity: input_number.offset_days_2
                - entity: input_number.offset_days

Hi… oh thats very interesting. so you are doing things pretty much identically, but you’re using the Offset in the series not at the chart level… and you don’t get an error pop up when you load the graph for 2 seconds ?.. I am going to try that… nice once, and fingers crossed. thanks @vingerha

The error indeed pops-up but very briefly, cannot make a screenshot. I think it error’s on the template as it first has no value, then gets the value from the input_number…guesswork only

oh poo :-(.

Thanks @vingerha, but using your method gives the same result. I get a 2-3 second error while the card-templatar “works out” what the value should be.

Its almost as if its a weakness of card-templatar in that we want it to resolve the “_template” into a (valid) default value while it resolves it from the watched entities.

Anyone else trying this sort of thing ?
thanks

Try to change the input text to input number, depending on what is in the entities itwas slower I remember…
And I also believe it is related to the template card…will try tthe other card config-template tomorrow

Have you tried ‘cheating’ and putting in a zero, such that it sees the value as “-0” before card templater does its magic? ie.

offset_template: '-0{{ states(''input_number.offset_days'') }}d'

Just tried that, makes it actually slower for me

I tried with the other template card, this seems nicer, at least not error and same response …

        - type: vertical-stack
          cards:
            - type: custom:config-template-card
              card:
                type: custom:apexcharts-card
                graph_span: 30h
                header:
                  show: true
                  title: >-
                    ${'-' + parseInt(states['input_number.offset_days'].state)
                    +'d'}
                  show_states: true
                  colorize_states: true
                series:
                  - entity: sensor.linky_daily
                    curve: smooth
                    color: '#df6366'
                    offset: ${'-' + states['input_number.offset_days'].state +'d'}
                    type: column
                    opacity: 0.6
                    stroke_width: 1
                    statistics:
                      type: state
                  - entity: sensor.linky_daily
                    curve: smooth
                    offset: ${'-' + states['input_number.offset_days_2'].state +'d'}
                    color: green
                    type: column
                    opacity: 0.1
                    stroke_width: 0.2
                    statistics:
                      type: state
              entities:
                - input_number.offset_days_2
                - input_number.offset_days
1 Like

I’ll try that tonight, thanks @vingerha and @michaelblight

ok, boom. so the moral of the story is that both card-templatar and config-template-card are great, BUT, card-templatar throws a visible error when the dashboard is loaded for 2-3 seconds while the graphs are loaded and the template is evaluated. config-template-card DOES NOT. It is slick and seamless :slight_smile:
Thanks to @vingerha and @michaelblight for commenting and assisting. I now have beautiful graphs that seamlessly and dynamically update to scrolling offset values.


Your dashboards look awsome, are you willing to share your code?

Hello!!

I’ve got this working, except I currently have to refresh home assistant after changing the input_select or input_number (I’ve tried both methods)

How do you force the chart to refresh?

With me that started working with the inut_number(s) in the entities section

That looks pretty neat :slight_smile: :clap:
Willing to share the code and the config of the helpers? Great idea to have one helper for the graph name (date).

I found this thread cause I had the same problem as OP, but I get a big miffed when someone gets help, and doesn’t reply to something as simple as people asking nicely if OP might share his code for formatting his graphs. It’s a compliment to be asked, and a bit rude to ignore imo.

I’m a fairly avid user of Apex Charts, so I thought I’d throw together a quick explanation of how OP (likely) has formatted his graphs.

Starting from an apex chart with “stacked: true” on the main options, and then 4 series which are in order [ sun-energy, battery charge/discharge, grid, battery SoC ], with a double y-axis (one for kWh on the left, and one for SoC % on the right).

Then I’d guess all of the actual “styling” is done under apex_config, a quick example I threw together looks like this

type: custom:apexcharts-card
  graph_span: 1d
  span:
    end: day
  apex_config:
    chart:
      width: 100%
      height: 300px
    stroke:
      dashArray:
        - 0
        - 0
        - 0
        - 5
    dataLabels:
      enabled: true
      enabledOnSeries:
        - 0
        - 1
        - 2
      textAnchor: middle
      style:
        fontSize: 12px
        fontWeight: bold
        colors:
          - '#f8f8ff'
      background:
        enabled: false
      dropShadow:
        enabled: true
    plotOptions:
      bar:
        datalabels:
          position: top,
          hideoverflowinglabels: true
          total:
            enabled: false
            fontSize: 14px
            fontWeight: bold
            style:
              color: '#f8f8ff'
        borderRadius:
          - 10
          - 10
          - 10
        columnWidth: 70%
    fill:
      type:
        - gradient
        - gradient
        - gradient
        - solid
      colors:
        - '#e6f900'
        - '#0000f9'
        - '#f93100'
      gradient:
        type: diagonal
        inverseColors: false
        shadeIntensity: 0.7
        gradientToColors:
          - '#f0ac00'
          - '#00a8f0'
          - '#c55942'
  header:
      show: true
      show_states: true
      colorize_states: true
    stacked: true
    yaxis:
      - id: energy
        min: ~5
        max: ~5
      - id: percent
        min: 0
        max: 100
        opposite: true
  series: ...

For more in-depth info on what can be added to apex_config it’s available here under “Options (Reference)”, just like RomRider mentions in the documentation.

A quick explanation of what part does what:

  • stroke.dashArray: Makes the graph of a series dashed instead of solid. The higher the number for a series, the more spacing in between dashes. This is how the dashed line for SoC is created, we put 0 for the other series so they aren’t affected.
  • dataLabels: Just some settings and styling for the datalabels, the important ones would be that it’s only enabled on the first 3 items of the series-array (first item in the array is number 0), which is how we “disable” it for SoC, and background.enabled = false, which is how we get rid of the box usually around datalabels.
  • plotOptions.bar: Here’s some more styling for the datalabels, but also importantly “borderRadius”, which is what gives the data-columns their rounded edges. Higher number = more rounded edge. We also set the width of the columns to 70% of the available space (which is actually the default for pure Apex Charts).
  • fill: Here we have the settings that give the shading color-tone, first we set type to “gradient” for the 3 bar-series, and to “solid” for the SoC-series.
    Secondly we specify the hex-codes for the 3 series we wish to manipulate (I just set the SoC-color in it’s series-setting, since we’re not manipulating that, suboptimal for a teaching example, but I was more creating a PoC than a refined product here…), the color is the “starting” color, which will be in the upper left corner, then we state that the gradient should be diagonal to get the same effect as OP, and we set “gradientToColors” to the (again in order) colors we wish our 3 series of bars should fade into. “shadeIntensity” is how quickly the transition happens from 0 to 1, so 0.7 is basically 30% original color and 70% the color transitioned to.

That’s more or less it, again, my code is a quick PoC, especially the colors I didn’t spend a whole lot of time to match OP, I just took some close’ish ones. But that’s just hex-codes, easy to play around with until you find what you like.

Then it’s similar settings for the other graphs btw, but for line/area-graph, and pie-chart. All the info is in the apex charts webside I mentioned earlier. It’s not the most intuitive website to navigate, but the info that’s there is pretty good and comprehensive.

Anyway, hope this helps someone. :slight_smile:

1 Like