Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HA + Apexchart + Tariff prices #4

Closed
heijjan opened this issue Dec 2, 2022 · 5 comments
Closed

HA + Apexchart + Tariff prices #4

heijjan opened this issue Dec 2, 2022 · 5 comments

Comments

@heijjan
Copy link

heijjan commented Dec 2, 2022

Hi,

Thanks for a great and simple solution for the SPOT price analysis. I was wondering if it was possible to implement the tariff prices (nigh/day) to the ApexChart configuration so that the tariff prices would be added on top of the SPOT price for every hour? I just can't figure out how to do it. Another solution would be to implement the tariff to the configuration of the main sensor shf_electricity_price somehow.

Our tariffs are 6.8c/kWh during 7-22 and 5.24c/kWh during 22-07.

I already included these to the sensor config of the current price

  • sensor:
    • name: SHF Electricity price now
      unique_id: shf_electricity_price_now
      unit_of_measurement: "€/kWh"
      device_class: monetary
      state: >
      {%- set siirto_paiva = 0.068 -%}
      {%- set siirto_yo = 0.0524 -%}
      {%- set alv = 1.10 -%}
      {%- set marginaali = 0.002016 -%}
      {{ (state_attr("sensor.shf_electricity_price", "data")[states("sensor.shf_idx")|int+now().hour]["PriceWithTax"] + marginaali * alv + siirto_paiva)|round(3) if 6 < (now().hour) < 22 else (state_attr("sensor.shf_electricity_price", "data")[states("sensor.shf_idx")|int+now().hour]["PriceWithTax"] + marginaali * alv + siirto_yo)|round(3) }}
@T3m3z
Copy link
Owner

T3m3z commented Dec 3, 2022

Hello!

If you want to add the transmission and other extra fees only to the chart, then maybe this quick hack could work:

type: custom:apexcharts-card
graph_span: 48h
experimental:
  color_threshold: true
show:
  last_updated: true
header:
  title: Electricity price (c/kWh)
  show: true
  show_states: true
  colorize_states: true
span:
  start: day
yaxis:
  - min: 0
    decimals: 2
    apex_config:
      forceNiceScale: true
now:
  show: true
  label: Now
series:
  - entity: sensor.shf_electricity_price
    show:
      in_header: false
      extremas: true
    type: column
    color: lightgray
    float_precision: 2
    data_generator: |
      var extra_fees = [5.46, 5.46, 5.46, 5.46, 5.46, 5.46, 5.46, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 5.46, 5.46 ] ;
      return entity.attributes.data.map((d, index) => {
        var tempdate = new Date(d["DateTime"]);
        var hour = tempdate.getHours();
        return [tempdate.getTime(), entity.attributes.data[index]["PriceWithTax"]*100+extra_fees[hour]];
      });
    color_threshold:
      - value: 0
        color: 368f39
      - value: 10
        color: a3b34d
      - value: 20
        color: ffd57e
      - value: 30
        color: f18c56
      - value: 40
        color: de425b
  - entity: sensor.shf_electricity_price
    show:
      in_header: true
      header_color_threshold: true
      in_chart: false
    type: line
    color: lightgray
    float_precision: 2
    stroke_width: 0
    data_generator: |
      var extra_fees = [5.46, 5.46, 5.46, 5.46, 5.46, 5.46, 5.46, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 7.02, 5.46, 5.46 ] ;
      var currentTime = new Date().setMinutes(0, 0, 0);
      let currentPrice = entity.attributes.data.find(d => {
        return new Date(d["DateTime"]).getTime() === currentTime;
      })["PriceWithTax"];
      return entity.attributes.data.map((d, index) => {
        var t = new Date();
        return [new Date(d["DateTime"]).getTime(), currentPrice*100 + extra_fees[t.getHours()]];
      });
    color_threshold:
      - value: 0
        color: 368f39
      - value: 10
        color: a3b34d
      - value: 20
        color: ffd57e
      - value: 30
        color: f18c56
      - value: 40
        color: de425b

Currently implementing transmission fees + other extra fees to the data loader isn't that simple as this package is using Restful sensor included in the HomeAssistant. Modifying the Array of prices isn't right now possible (or at least easy) as Template Sensors always output a String. But I'm going to think about this as I see the value of this. Sometimes day/night transmission fees could change the outcome of some scripts.

@heijjan
Copy link
Author

heijjan commented Dec 4, 2022

Great, thank you! This was exaclty what I was looking for.

@T3m3z
Copy link
Owner

T3m3z commented Dec 4, 2022

Alright, nice to hear! I will close this issue then.

But please note that the code provided affects only the charts and other logic inside this package does not know about your transmission fees. I might add some functionality for this in the future.

@T3m3z T3m3z closed this as completed Dec 4, 2022
@heijjan
Copy link
Author

heijjan commented Dec 4, 2022

Yes, this was the target. I have the template sensors controlling automations, which take the margins and transfer prices into account. The only problem I had was the apexchart not showing the total amount of the price, but with this hack it works perfectly.

@T3m3z
Copy link
Owner

T3m3z commented Dec 19, 2022

I added some functionality around these transmission fees etc. They are not fully tested and therefore they are currently available in a separate branch: https://github.com/T3m3z/spotprices2ha/tree/price_margins

Feel free to test this. Could be useful for automations too and not only for the chart.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants