Skip to content

Instantly share code, notes, and snippets.

@klaasnicolaas
Last active June 26, 2024 12:54
Show Gist options
  • Save klaasnicolaas/ccfd1cd3da62a13b3199ff378480bdbb to your computer and use it in GitHub Desktop.
Save klaasnicolaas/ccfd1cd3da62a13b3199ff378480bdbb to your computer and use it in GitHub Desktop.
Collecting the hourly energy prices via a service in the EnergyZero or easyEnergy integration and using them in an ApexCharts card in your dashboard.
- type: custom:apexcharts-card
graph_span: 1d
header:
show: true
title: Electriciteitsprijzen Vandaag (€/kwh)
span:
start: day
now:
show: true
label: Now
yaxis:
- id: price
decimals: 2
apex_config:
tickAmount: 5
series:
- entity: sensor.energy_prices_today
name: Price this hour
yaxis_id: price
data_generator: |
return entity.attributes.prices.map((entry) => {
return [new Date(entry.timestamp).getTime(), entry.price];
});
type: column
show:
extremas: true
opacity: 0.8
float_precision: 2
---
# Today - Hourly energy prices
template:
- trigger:
- platform: homeassistant
event: start
- platform: time_pattern
hours: "*"
action:
- service: energyzero.get_energy_prices
response_variable: response
data:
config_entry: PUT_HERE_YOURS
incl_vat: True
sensor:
- name: Energy prices - Today
device_class: timestamp
state: "{{ now() }}"
attributes:
prices: '{{ response.prices }}'
# Tomorrow - Hourly energy prices
- trigger:
- platform: homeassistant
event: start
- platform: template
value_template: "{{ now() > today_at('14:00') and now().minute == 0 }}"
action:
- service: energyzero.get_energy_prices
response_variable: response
data:
config_entry: PUT_HERE_YOURS
start: "{{ now() + timedelta(days=1) }}"
end: "{{ now() + timedelta(days=1) }}"
incl_vat: True
sensor:
- name: Energy prices - Tomorrow
device_class: timestamp
state: "{{ now() + timedelta(days=1) }}"
attributes:
prices: '{{ response.prices }}'
@thijstriemstra
Copy link

thijstriemstra commented Jan 27, 2023

I tested this with your development version of the energyzero integration but I'm getting this error for this chart:

apexcharts-card:  Error: TypeError: entity.attributes.today is undefined 

I only see the following attributes:

Captur4

any idea @klaasnicolaas?

@klaasnicolaas
Copy link
Author

The above code does not work with the integration in core, this has to do with the decision that was made here and therefore the attributes you need are not available.

@thijstriemstra
Copy link

thijstriemstra commented Jan 27, 2023

Thanks for the feedback @klaasnicolaas. Does that mean it's not possible to create a graph like seen on https://doe-duurzaam.nl/2023/01/02/dynamische-energieprijzen-inlezen-met-home-assistant-zo-doe-je-dat/ right now? Will it in the future? I'm slightly confused hehe.

@klaasnicolaas
Copy link
Author

Does that mean it's not possible to create a graph like seen on

That is correct, see also my blog with additional explanation

@thijstriemstra
Copy link

Gotcha. From the blog:

Please note that the integration does not support the mapping of every hour in Apexcharts via the attributes, this method is a workaround that creates a bulk load of data in the state machine and is not accepted in core.
Who knows, maybe there will be better support for this in the future.

Fingers crossed!

@klaasnicolaas
Copy link
Author

I updated the Gist because with the Home Assistant January release (2024.1) it's possible to retrieve hourly prices via a service with the core integration of EnergyZero! 😄

@wouterdebruijn
Copy link

Thank you for the gist update, it works perfectly in 2024.1

@MaartenDrenth
Copy link

@klaasnicolaas

Thanks for the updated code. Unfortunately I'm not able to get in working. When copying the

price_template_sensors.yaml](https://gist.github.com/klaasnicolaas/ccfd1cd3da62a13b3199ff378480bdbb#file-price_template_sensors-yaml)""

I get the error 'response' is undefined". I did copy my own config_ entry here... config_entry: PUT_HERE_YOURS

Can you advise?

And when taking the code for the chart and add it directly to the dashboard I also get some errors:

Configuration errors detected:
bad indentation of a mapping entry (2:13)

1 | type: custom:apexcharts-card
2 | graph_span: 23h
-----------------^
3 | header:
4 | show: true

Hopefully you can help me...
Its all a bit new for me

Thanks!

@AGVisser46
Copy link

AGVisser46 commented Jan 7, 2024

@klaasnicolaas
After pasting the price_template_sensors.yaml in my configuration.yaml I got it to work, thanks. The service is running and the entity contains the prices. The apexcharts shows the hourly prices (without the additional costs of course), but the yaxis of the chart looks funny: 0.1 at each mark of the y-axis. Attemps to define a min and max in a yaxis block fail: results in errors like 'yaxis is not an array'.
Is this because the yaxis_id is given within the series block? Any suggestions how to solve this?
Thanks, Andries

type: custom:apexcharts-card
graph_span: 24h
header:
  show: true
  title: Electriciteitsprijzen Vandaag (€/kwh)
span:
  start: day
now:
  show: true
  label: Now
series:
  - entity: sensor.energy_prices_today
    name: Price this hour
    yaxis_id: price
    data_generator: |
      return entity.attributes.prices.map((entry) => {
        return [new Date(entry.timestamp).getTime(), entry.price];
      });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 2

@klaasnicolaas
Copy link
Author

@MaartenDrenth Something probably didn't go quite right with copying and you have to check your indents, without knowing specifically what is in your yaml file, now it's difficult for me to help you.

@klaasnicolaas
Copy link
Author

klaasnicolaas commented Jan 8, 2024

@AGVisser46 I've adjusted the yaml code with a separate styling for the yaxis, I think this has helped you quite a bit.

image

If you also want to include the additional costs in the graph, you could adjust the data_generator to something like:

- entity: sensor.energy_prices_today
   name: "Dynamische energieprijs"
   data_generator: |
     return entity.attributes.prices.map((entry) => {
       return [new Date(entry.timestamp).getTime(), (entry.price + 0.1312 + 0.0484)];
     });

@MaartenDrenth
Copy link

MaartenDrenth commented Jan 8, 2024

Hi

thanks for your reply.

I think I managed to solve the format issue of the apexcard code.

But the template still does not work.. it still gives me the message "response is undefined'?
Any idea whats wrong? What is this response referring to?

# Today - Hourly energy prices
- trigger:
    - platform: homeassistant
      event: start
    - platform: time_pattern
      hours: "*"
  action:
    - service: energyzero.get_energy_prices
      response_variable: response
      data:
        config_entry: P42989e0ca59cdfbfe81622c9d7fd5b61
        incl_vat: True
  sensor:
    - name: Energy prices - Today
      device_class: timestamp
      state: "{{ now() }}"
      attributes:
        prices: "{{ response.prices }}"

@wouterdebruijn
Copy link

@MaartenDrenth I suspect a fault with your config_entry id, The P at the start might be wrong? Mine is 33 characters while yours is 34. You can get your config_entry through the developer tools > services tab. Selecting the EnergyZero: Get energy prices service and selecting your energy zero integration through UI mode. After that you can switch to YAML mode and copy the ID.

@AGVisser46
Copy link

@klaasnicolaas
Thanks a lot, both for the yaxis styling and the allin prices in the data_generator . It is a useful tool now.

@MaartenDrenth
Copy link

Hi

Thanks. Updating the ID indeed worked!

I also got the 2 charts working properly. One for the prices of today and one for tomorrow.

Would be great if these charts could be combined in one.

Is it easy for you to update the apexchart code to get this done? 😉

@MaartenDrenth
Copy link

MaartenDrenth commented Jan 9, 2024

I updated the chart myself a bit like this to improve the y-axis:

type: custom:apexcharts-card
graph_span: 48h
header:
  show: true
  title: Electriciteitsprijzen Vandaag EnergyZero (€/kwh)
span:
  start: day
now:
  show: true
  label: Now
yaxis:
  - id: price
    decimals: 3
    min: -0.2
    max: 0.3
    apex_config:
      tickAmount: 5
series:
  - entity: sensor.energy_prices_today
    name: Price this hour
    yaxis_id: price
    data_generator: |
      return entity.attributes.prices.map((entry) => {
        return [new Date(entry.timestamp).getTime(), entry.price];
      });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 3

image

@klaasnicolaas
Copy link
Author

Would be great if these charts could be combined in one.

Why would you want to combine them? I don't really see an advantage/improvement in that.

@MaartenDrenth
Copy link

Mainly cosmetics
Just one chart instead of 2
Learning how this needs to be done.

@MaartenDrenth
Copy link

MaartenDrenth commented Jan 10, 2024

Hi
Not required anymore.
I managed to create it myself..

type: custom:apexcharts-card
graph_span: 48h
header:
  show: true
  title: Electriciteitsprijzen Vandaag en Morgen EnergyZero (€/kwh)
span:
  start: day
now:
  show: true
  label: Now
yaxis:
  - id: price
    decimals: 3
    min: -0.2
    max: 0.3
    apex_config:
      tickAmount: 10
series:
  - entity: sensor.energy_prices_today
    name: Price this hour
    yaxis_id: price
    data_generator: |
      return entity.attributes.prices.map((entry) => {
        return [new Date(entry.timestamp).getTime(), entry.price];
      });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 3
  - entity: sensor.energy_prices_tomorrow
    name: Price this hour
    yaxis_id: price
    data_generator: >-
      return entity.attributes.prices.map((entry) => { return [new
      Date(entry.timestamp).getTime(), entry.price]; });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 3

Thanks

@MariellevDijk
Copy link

Hi @klaasnicolaas I've run into some Yaml validation error when including the https://gist.github.com/klaasnicolaas/ccfd1cd3da62a13b3199ff378480bdbb#file-price_template_sensors-yaml file in configuration.yaml. As far as I can tell from looking at the official documentation, everything should work, but I keep getting the error "Invalid config for 'sensor' at configuration.yaml, line 12: required key 'platform' not provided"

The file is included on line 12, so that's logical, but I don't see how the platform key is missing, as it's clearly right there. I'm using your gist version almost literally (only the ID is filled in with my own). What could I be missing here?

I'll paste my file as-is anyway.

---
# Today - Hourly energy prices
template:
  - trigger:
      - platform: time_pattern
        minutes: 1
      - platform: homeassistant
        event: start
    action:
      - service: energyzero.get_energy_prices
        response_variable: response
        data:
          config_entry: 86c311fb791184eec3ee798d27997b3d
          incl_vat: True
    sensor:
      - name: Energy prices - Today
        device_class: timestamp
        state: "{{ now() }}"
        attributes:
          prices: '{{ response.prices }}'

  # Tomorrow - Hourly energy prices
  - trigger:
      - platform: time_pattern
        minutes: 1
      - platform: homeassistant
        event: start
    action:
      - service: energyzero.get_energy_prices
        response_variable: response
        data:
          config_entry: 86c311fb791184eec3ee798d27997b3d
          start: "{{ now() + timedelta(days=1) }}"
          end: "{{ now() + timedelta(days=1) }}"
          incl_vat: True
    sensor:
      - name: Energy prices - Tomorrow
        device_class: timestamp
        state: "{{ now() + timedelta(days=1) }}"
        attributes:
          prices: '{{ response.prices }}'

@klaasnicolaas
Copy link
Author

@MariellevDijk my suspicion is that you did something wrong with your include in configuration.yaml 🤷‍♂️

@mhoeneveld
Copy link

Hi @klaasnicolaas I've run into some Yaml validation error when including the https://gist.github.com/klaasnicolaas/ccfd1cd3da62a13b3199ff378480bdbb#file-price_template_sensors-yaml file in configuration.yaml. As far as I can tell from looking at the official documentation, everything should work, but I keep getting the error "Invalid config for 'sensor' at configuration.yaml, line 12: required key 'platform' not provided"

The file is included on line 12, so that's logical, but I don't see how the platform key is missing, as it's clearly right there. I'm using your gist version almost literally (only the ID is filled in with my own). What could I be missing here?

I'll paste my file as-is anyway.

---
# Today - Hourly energy prices
template:
  - trigger:
      - platform: time_pattern
        minutes: 1
      - platform: homeassistant
        event: start

For sure it makes no sense to request the prices each minute. EnergyZero made a statement that when exceeding more than 20 requests per hour they will block the requests.

@wouterdebruijn
Copy link

wouterdebruijn commented Feb 6, 2024

@mhoeneveld The provided trigger:

- platform: time_pattern
  minutes: 1

Matches every hour one minute past whole.

See https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger for more information.

@GofranAy
Copy link

GofranAy commented Mar 16, 2024

Hello i am beginner with home assistant and wanted to get this work.
but keep getting stuck i have energyzero installed and apexchart.
when trying like to change sensor when copying a yaml from above.
it get stuck with Loading. i only changed a one yaml i copied from Maartendrenth.
but it says missng
apexcharts-card
Entity not available: sensor.energy_prices_today <---- where to get this and the one under i tried change it to
energyzero_today_energy_current_hour_price.

this one is also missing
Entity not available: sensor.energy_prices_tomorrow

type: custom:apexcharts-card
graph_span: 48h
header:
  show: true
  title: Electriciteitsprijzen Vandaag en Morgen EnergyZero (€/kwh)
span:
  start: day
now:
  show: true
  label: Now
yaxis:
  - id: price
    decimals: 3
    min: -0.2
    max: 0.3
    apex_config:
      tickAmount: 10
series:
  - entity: sensor.energy_prices_today
    name: Price this hour
    yaxis_id: price
    data_generator: |
      return entity.attributes.prices.map((entry) => {
        return [new Date(entry.timestamp).getTime(), entry.price];
      });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 3
  - entity: sensor.energy_prices_tomorrow
    name: Price this hour
    yaxis_id: price
    data_generator: >-
      return entity.attributes.prices.map((entry) => { return [new
      Date(entry.timestamp).getTime(), entry.price]; });
    type: column
    show:
      extremas: true
    opacity: 0.8
    float_precision: 3

@klaasnicolaas
Copy link
Author

@GofranAy You have only copied the apexcharts code, but without the template sensors (which retrieve the data) you will not get the correct entities.

./Klaas

@GofranAy
Copy link

GofranAy commented Apr 4, 2024

@klaasnicolaas can you please share where to go add that entities i have anwb for energy.
I live in holland/dutch/netherland

@klaasnicolaas
Copy link
Author

@GofranAy in your configuration.yaml?

@GofranAy
Copy link

GofranAy commented Apr 4, 2024

@klaasnicolaas i am sorry do you mean i need to add.
hourly_price_charts.yaml and price_template_sensors.yaml

So just copy paste in to configuration.yaml.

@klaasnicolaas
Copy link
Author

Only the YAML of price_template_sensors.yaml to get the 2 trigger template sensors in Home Assistant. You use the apex code snippet where you created your dashboard in YAML.

Side note: everyone uses YAML differently in their Home Assistant instance, without context (how you organize your config) it's impossible to give advice on where to put the code snippets and I assume that people know where you should place these.

@CreaticDD
Copy link

Didn't work: defining the template sensor under Settings > Devices & Services > Helpers
Keep getting error "response is undefined"

What works: copy/pasting the template sensor code into configuration.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment