Home Assistant
Show the spot price in Home Assistant and switch a device on in the cheapest hours, with the RESTful integration. No API key, add-on or custom component is needed.
The price now
This sensor reads the price of the current hour every 15 minutes. Its prices attribute holds the hourly prices from midnight today to the last published hour, ready for a chart card. The second sensor tells how cheap the hour is compared with the rest of the day, from VERY_CHEAP (the cheapest fifth) to VERY_EXPENSIVE.
# configuration.yaml
rest:
- resource: https://milloin.xyz/api/v1/electricity/prices?resolution=hour
scan_interval: 900
sensor:
- name: Electricity price
unique_id: milloin_electricity_price
value_template: "{{ value_json.current.price }}"
unit_of_measurement: "c/kWh"
state_class: measurement
json_attributes:
- prices
- name: Electricity price today
unique_id: milloin_electricity_price_today
value_template: "{{ value_json.current.dayCategory }}" Leave out resolution=hour for the 15-minute price. The attribute then holds four times as many prices, over the 16 kB Home Assistant keeps in its database.
Run a device in the cheapest hours
This binary sensor is on during the cheapest 4 hours of each day, in blocks of at least an hour, for example for a water heater. earliestStart=today and latestEnd=tomorrow plan the whole Finnish day from midnight to midnight, so the plan stays the same as the day goes on: it does not move when blocks are over or when tomorrow's prices arrive.
# configuration.yaml, under the same rest: key
- resource: https://milloin.xyz/api/v1/electricity/plans?pausable=true&durationHours=4&minBlockHours=1&earliestStart=today&latestEnd=tomorrow
scan_interval: 60
binary_sensor:
- name: Water heater cheap hours
unique_id: milloin_water_heater_cheap_hours
value_template: "{{ value_json.activeNow }}"An automation switches the device when the sensor changes:
# automations.yaml
- alias: Water heater on in the cheap hours
triggers:
- trigger: state
entity_id: binary_sensor.water_heater_cheap_hours
to: ["on", "off"]
actions:
- action: "switch.turn_{{ trigger.to_state.state }}"
target:
entity_id: switch.water_heater Change durationHours to how long the device needs to run, or give energyKwh and maxPowerKw instead. Every parameter is in Custom plans. Polling every minute is fine: the CDN answers the repeats, and activeNow changes on the quarter hour.
The cheapest fifth of the day
Without a plan: a template sensor that is on whenever the price is among the cheapest fifth of its day.
# configuration.yaml
template:
- binary_sensor:
- name: Electricity cheapest fifth of the day
unique_id: milloin_cheapest_fifth
state: "{{ is_state('sensor.electricity_price_today', 'VERY_CHEAP') }}"