BottlecapDave / HomeAssistant-OctopusEnergy

Unofficial Home Assistant integration for interacting with Octopus Energy
https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/
MIT License
532 stars 49 forks source link

Greeenness_score as the state of sensor.octopus_energy_z_yyyyyyy_greenness_forecast_current_index #881

Closed ando2040 closed 1 month ago

ando2040 commented 1 month ago

Describe the feature

Thanks Dave as ever for the integration!

Currently, sensor.octopus_energy_z_yyyyyyy_greenness_forecast_current_index has a state of 'unknown'

I like to track the historical value of greenness_score and have created a template sensor to do so, because the attributes of array sensors aren't held in history.

I believe if the sensor had a state, the last value of it would automatically be held in history? That would avoid a template sensor and I believe be the intuitive value to hold as state (it is the current state of the greenness forecast)

Expected behaviour

State of sensor be set to current index value, once per day.

Use Case

History of sensor without separate template sensor

Confirmation

BottlecapDave commented 1 month ago

Hello. The greenness sensor only shows a value during applicable times as reported by OE. At the present time OE only provides data during the night as they said they only track wind data. However I think there's plans in the future for them to track other things (like solar) which might provide greenness indexes during the day. The sensor as it stands will track the history, but only while a state is present.

The sensor could default to zero, but then people won't be able to determine the difference between an actual zero value and no data provided by OE (like it is now). The state of unknown is what HA recommend when data is not available.

Because of the above and I don't want to manipulate the data provided by OE in case it becomes more rich in the future and because a green night doesn't mean a green day, this sensor will not be changing behaviour.

ando2040 commented 1 month ago

Good explanation, I hadn't actually spotted that the sensor gains a state during the night (because I'm not usually looking at HA 2300-0600!) Apex-Charts-Card wasn't showing it, but HA history is, so I need to look at my data generator.

ando2040 commented 1 month ago

Happy to put my hand up to a mistake. For anyone finding this, and thanks to Dave's hint that the state does get set and logged as history (at night when I hadn't noticed, and it wasn't showing up in a default chart), it is possible to show the historical state using the group_by: function of ApexCharts-Card. This is what I set out to achieve - turns out it was already a feature, my bad.

image

type: custom:apexcharts-card
graph_span: 5d
span:
  offset: +2d
apex_config:
  xaxis:
    tooltip:
      enabled: false
  stroke:
    dashArray:
      - 2
      - 0
      - 0
      - 0
      - 2
      - 0
now:
  show: true
  color: red
header:
  show: true
  title: Carbon Intensity History(3d)/Forecast(2d)
series:
  - entity: sensor.national_grid_current_carbon_intensity
    yaxis_id: CO2
    name: Current
    color: black
    opacity: 0.8
    stroke_width: 2
    data_generator: ''
    show:
      legend_value: true
      in_header: false
  - entity: sensor.national_grid_48h_carbon_intensity_forecast
    name: Fcst in 2d
    yaxis_id: CO2
    color: black
    opacity: 0.8
    stroke_width: 2
    data_generator: |
      return entity.attributes.data.map(obj => {
        return [new Date(obj.to).getTime(), obj.intensity.forecast];
      });
  - entity: sun.sun
    yaxis_id: CO2
    name: Night
    color: grey
    type: area
    curve: stepline
    stroke_width: 0
    opacity: 0.2
    transform: 'return x === "below_horizon" ? 400 : 0;'
    show:
      legend_value: false
      in_header: false
  - entity: sensor.octopus_energy_y_xxxxxxxx_greenness_forecast_current_index
    yaxis_id: percent
    name: Greenness Fcst
    curve: stepline
    color: green
    opacity: 0.8
    stroke_width: 2
    data_generator: |
      return entity.attributes.forecast.map(obj => {
        return [new Date(obj.start).getTime(), obj.greenness_score];
      });
    show:
      legend_value: false
      in_header: false

  - entity: sensor.octopus_energy_y_xxxxxxx_greenness_forecast_current_index
    data_generator: ''
    type: column
    yaxis_id: percent
    name: Greenness Score History
    show:
      legend_value: false
    stroke_width: 2
    color: green
    group_by:
      func: max
      duration: 30min
yaxis:
  - id: CO2
    min: 0
    max: 400
    decimals: 0
  - id: percent
    min: 0
    max: 100
    opposite: true