Use hourly prices in Home Assistant
This guide shows how to bring the hourly grid fee into Home Assistant and use it to run an EV charger, heat pump or other device during the cheapest hours of the day. No add-ons are needed, only Home Assistant's built-in RESTful and Template integrations.
What you get
| Entity | What it shows |
|---|---|
sensor.grid_fee_prices | Fetches the prices once an hour. The prices are stored in its actual attribute. |
sensor.grid_fee_price_now | The price for the current hour, in SEK/kW incl. VAT. Use it in dashboards and history graphs. |
binary_sensor.grid_fee_cheap_hour | on during the 4 cheapest hours of the day. Use it to start and stop devices. |
Plus an example automation that turns an EV charger on and off.
Configuration
Add this to your configuration.yaml, change the product and switch.ev_charger to match your setup, then check the configuration and restart Home Assistant:
# Göteborg Energi hourly grid fee in Home Assistant.
# Add to configuration.yaml, check the configuration and restart Home Assistant.
rest:
- resource: https://api.uat.goteborgenergi.cloud/gridtariff/v0/prices?product=GN6KWST
scan_interval: 3600 # fetch once an hour
timeout: 30
sensor:
- name: Grid fee prices
unique_id: goteborg_energi_grid_fee_prices
# The state is the number of hours with set prices.
# The prices themselves are stored in the "actual" attribute.
value_template: "{{ value_json.actual | count }}"
json_attributes:
- actual
template:
- sensor:
- name: Grid fee price now
unique_id: goteborg_energi_grid_fee_price_now
unit_of_measurement: "SEK/kW"
availability: "{{ state_attr('sensor.grid_fee_prices', 'actual') is not none }}"
state: >
{% set ns = namespace(price=none) %}
{% for hour in state_attr('sensor.grid_fee_prices', 'actual') or [] %}
{% if as_timestamp(hour.start) <= now().timestamp() < as_timestamp(hour.end) %}
{% set ns.price = hour.priceIncVat %}
{% endif %}
{% endfor %}
{{ ns.price }}
- binary_sensor:
# On during the cheapest hours of the day
- name: Grid fee cheap hour
unique_id: goteborg_energi_grid_fee_cheap_hour
availability: "{{ state_attr('sensor.grid_fee_prices', 'actual') is not none }}"
state: >
{% set hours_needed = 4 %}
{% set today = now().date() | string %}
{% set ns = namespace(prices=[], now_price=none) %}
{% for hour in state_attr('sensor.grid_fee_prices', 'actual') or [] %}
{% if hour.start[:10] == today %}
{% set ns.prices = ns.prices + [hour.priceIncVat] %}
{% endif %}
{% if as_timestamp(hour.start) <= now().timestamp() < as_timestamp(hour.end) %}
{% set ns.now_price = hour.priceIncVat %}
{% endif %}
{% endfor %}
{% if ns.now_price is none or not ns.prices %}
false
{% else %}
{% set limit = (ns.prices | sort)[[hours_needed, ns.prices | count] | min - 1] %}
{{ ns.now_price <= limit }}
{% endif %}
automation:
- alias: Charge the EV during cheap grid fee hours
triggers:
- trigger: state
entity_id: binary_sensor.grid_fee_cheap_hour
to: "on"
id: cheap
- trigger: state
entity_id: binary_sensor.grid_fee_cheap_hour
from: "on"
id: not_cheap
actions:
- choose:
- conditions:
- condition: trigger
id: cheap
sequence:
- action: switch.turn_on
target:
entity_id: switch.ev_charger # replace with your charger
default:
- action: switch.turn_off
target:
entity_id: switch.ev_charger
If you already have rest:, template: or automation: sections, add the entries to them instead of creating new ones. You can also create the automation in the Home Assistant UI instead.
Adjusting it
- Number of hours: change
hours_neededin the binary sensor. If several hours have the same price, more hours than that can be marked as cheap. - Your tariff: replace
GN6KWSTin the URL with the product of your tariff. Only tariffs with an hourly power fee have hourly prices. - When prices update: the next day's prices are set by 15:00. Fetching once an hour picks them up in time.
The binary sensor picks the cheapest hours among today's hours. If you want to plan across midnight, for example to charge the car overnight, compare the hours from now until the time it needs to be ready instead.