Skip to content

"General" Lambda for all entities broken in 1.4.2? #84

@b3nn0

Description

@b3nn0

Hi,
just upgraded to 1.4.2 to get the new statistics support - awesome!

Unfortunately, that broke one of my more advanced graphs - a stacked area graph with a lambda to sort all values into 15s buckets.
It looks like plotly can't stack the graphs any more, and looks somewhat like in earlier versions if one tried to stack graphs with different X values (timestamps):
grafik

In the developer console, I can see a lot of this:

plotly-graph-card.js?hacstag=413812496142:44 TypeError: a.lambda is not a function
    at plotly-graph-card.js?hacstag=413812496142:44:6004
    at Array.flatMap (<anonymous>)
    at Qj.getData (plotly-graph-card.js?hacstag=413812496142:44:5594)
    at plotly-graph-card.js?hacstag=413812496142:44:7499
    at Qj.withoutRelayout (plotly-graph-card.js?hacstag=413812496142:44:585)
    at Qj.plot (plotly-graph-card.js?hacstag=413812496142:44:7450)
    at Qj.fetch (plotly-graph-card.js?hacstag=413812496142:12:13141)

so it seems like the lambda is not executed any more.
And indeed, if I put a console.log in the first line of my lambda, it seems to never be executed at all.

Card configuration:

type: custom:plotly-graph
layout:
  height: 550
  title:
    text: Strommix
entities:
  - entity: sensor.strom_eigenverbrauch
    name: Direktverbrauch
    fillcolor: 90EE90
    stackgroup: verbrauch
  - entity: sensor.strom_netzbezug
    name: Netzbezug
    stackgroup: verbrauch
    fillcolor: red
  - entity: sensor.strom_netzeinspeisung
    fillcolor: '#006400'
    name: Einspeisung
    stackgroup: verbrauch
hours_to_show: 12
refresh_interval: 10
defaults:
  entity:
    line:
      width: 0
    lambda: |-
      (ys, xs) => {
          const seconds = 15 // 60
          const window_size_ms = 1000 * seconds
          const t0 = Math.ceil(+xs[0]/window_size_ms)*window_size_ms;
          const t1 = Math.ceil(+xs[xs.length-1]/window_size_ms)*window_size_ms;
          const buckets = (t1 - t0) / window_size_ms

          const result = {
            x: Array.from({length: buckets}, (_, i) => new Date(t0 + i * window_size_ms)),
            y: Array(buckets).fill(NaN)
          };

          for (let i = 0; i < xs.length; i++) {
            const t = +xs[i]
            const bucket = Math.floor((t - t0) / window_size_ms)
            const values = [ys[i]]

            for (let j = i; j < xs.length; j++) {
              const t2 = +xs[j]
              const bucket2 = (t2 - t0) / window_size_ms
              if (bucket == bucket2) {
                values.push(ys[j])
                i++
              } else {
                break;
              }
            }
            let value = values.reduce((a, b) => a + b, 0) / values.length
            //let value = values[0]
            //console.log('bucket=' + bucket + ', value=' + value)
            result.y[bucket] = value
          }
          let prev = 0;
          for (let i = 0; i < result.y.length; i++) {
            if (isNaN(result.y[i]))
              result.y[i] = prev
            prev = result.y[i]
          }
          //console.log(result)
          return result
        }

BONUS: this is my PV graph. At around noon, you can see the effect of a partly solar eclipse today!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions