dbuezas / lovelace-plotly-graph-card

Highly customisable Lovelace card to plot interactive graphs. Brings scrolling, zooming, and much more!
407 stars 19 forks source link

Negative values misssing for x-axis in histogramm2dcontour #261

Open thecem opened 1 year ago

thecem commented 1 year ago

Describe the bug Beside there are negative values, the negative temperature values are not shown in histogramm2dcontour.

Screenshots grafik

yaml

type: custom:plotly-graph
entities:
  - entity: sensor.at_yesterday
    statistic: mean
    period: day
    time_offset: '-1d'
    internal: true
    filters:
      - filter: i>0
    fn: $fn ({ ys, vars }) => vars.temperature = ys
  - entity: sensor.gas_verbrauch_kwh
    statistic: state
    period: day
    name: Gas
    internal: true
    filters:
      - filter: i>0
      - derivate: d
    fn: $fn ({ ys, vars }) => vars.gas = ys
  - entity: ''
    x: $fn ({ vars }) => vars.temperature
    'y': $fn ({ vars }) => vars.gas
    mode: markers
    name: points
    marker:
      color: rgb(102,0,0)
      size: 3
      opacity: 0.4
    type: scatter
  - entity: ''
    line:
      shape: linear
      color: blue
    filters:
      - fn: |
          ({ys, xs, vars}) => {
            return {xs: vars.temperature, ys: vars.gas};
          }
      - trendline
  - entity: ''
    filters:
      - fn: |
          ({vars}) => {
            const xs = vars.temperature;
            const ys = vars.gas;
            const pairs = xs.map((x, i) => [x,ys[i]])
              .filter( ([x,y]) => Number.isFinite(x) && Number.isFinite(y)) // unavailables in the x axis break trendlines
              .sort((a,b)=>a[0]-b[0]); // the trendline filter expects the x axis to be sorted
            const r = {
              xs: pairs.map(([x,y])=>x),
              ys: pairs.map(([x,y])=>y),
            };
            console.log(r);
            return r;
          }
      - trendline
      - store_var: trend
    x: >-
      $fn ({ vars }) => vars.trend.xs.map(x=>+x) // the trendline made dates out
      of the x axis
    'y': >-
      $fn ({ vars }) => vars.trend.ys // and since this is a raw_plotly_config,
      we need to set x and y ourselves
  - entity: ''
    x: $fn ({ vars }) => vars.temperature
    'y': $fn ({ vars }) => vars.gas
    name: density
    ncontours: 40
    colorscale: YlGnBu
    nbinsx: 15
    nbinsy: 15
    reversescale: true
    showscale: false
    line:
      color: lightgray
    type: histogram2dcontour
  - entity: ''
    x: $fn ({ vars }) => vars.temperature
    name: x density
    texttemplate: '%{x:0.1f}'
    marker:
      color: "rgb(40\t57\t143\t)"
    yaxis: y2
    opacity: 1
    type: histogram
  - entity: ''
    x: undefined
    'y': $fn ({ vars }) => vars.gas
    name: y density
    texttemplate: '%{y:0.1f}'
    marker:
      color: "rgb(40\t57\t143\t)"
    xaxis: x2
    type: histogram
hours_to_show: 4800
title: >-
  $fn({getFromConfig})=>`Heating - Last ${getFromConfig('hours_to_show')/24}
  days`
raw_plotly_config: true
layout:
  margin:
    t: 20
    l: 70
    r: 20
    b: 70
  height: 400
  bargap: 0.1
  showlegend: false
  xaxis:
    title:
      text: Outside Temperature (°C)
    autorange: true
    domain:
      - 0
      - 0.85
    showgrid: false
    zeroline: false
  yaxis:
    title:
      text: Gas burned (kWh per day)
    domain:
      - 0
      - 0.85
    showgrid: false
    zeroline: false
  xaxis2:
    domain:
      - 0.85
      - 1
    showgrid: false
    zeroline: false
  yaxis2:
    domain:
      - 0.85
      - 1
    showgrid: false
    zeroline: false

Additional context With following config, we see the negative values for temperature. On another mashine this is working, asi posted here https://github.com/dbuezas/lovelace-plotly-graph-card/discussions/216. May there is something wrong with my config above.

THX 4 help!

grafik

type: custom:plotly-graph
entities:
  - entity: sensor.at_yesterday
    statistic: mean
    period: day
    time_offset: '-1d'
    show_value: true
    name: Ø Temp
  - entity: sensor.hmip_bw19_temperature_humidity_sensor_outdoor_temperature
    statistic: mean
    period: hour
    show_value: true
    name: Out Temp
  - entity: sensor.gas_verbrauch_kwh
    statistic: state
    period: day
    type: bar
    opacity: 0.5
    texttemplate: '%{y:0.1f}'
    filters:
      - derivate: d
    name: Gas
hours_to_show: 360
refresh_interval: 10
title: Heat Energy (Gas/Temp)
thecem commented 1 year ago

chnage "hours_to_show: 4800" to "hours_to_show: 2112":

grafik

dbuezas commented 1 year ago

If you go too far into the past it can happen that one of the entities has more data points than the other one. I suggest:

Let me know if you find something out!, I don't think this is a bug in the card