Nordpool themselves also have a API for fetching and trading (I assume they don’t give out login to everyone)
So aim trying to come up with a way to update a sensor with the values from the price.
So the sensor displays the current price but also holds history so price for both history hours and the coming hours exist in the system.
I currently only have history and current values from a IFTTT integration (using service called Bright)
If anyone done this before or have a great ide it would be welcome.
So its both the time and the price that aim trying to get.
Please correct me, but i do not see how that would be possible with scraper.
Hopefully aim wrong, I am hopeless with these strings
Again aim really thankful that you did take the time to look at this.
If i find another solution i will post back here. Must be a few more Swedish people that would like to know the hourly prices in advance, enabling automatstations that prevent energy spikes during expensive hours.
import asyncio
import logging
from datetime import timedelta
import aiohttp
import async_timeout
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'Electricity Price'
_RESOURCE = 'https://elen.nu/timpriser-pa-el-for-elomrade-se3-stockholm/'
SCAN_INTERVAL = timedelta(hours=2)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the Swedish Electricity Price sensor."""
name = config.get(CONF_NAME)
async_add_devices([SwedishElectricityPriceSensorSensor(hass, name)], True)
class SwedishElectricityPriceSensorSensor(Entity):
"""Implementation of a Swedish Electricity Price sensor."""
def __init__(self, hass, name):
"""Initialize the Swedish Electricity Price sensor."""
self.hass = hass
self._name = name
self._state = None
self.attrs = {}
self._unit_of_measurement = 'öre/kWh'
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return self.attrs
@asyncio.coroutine
def async_update(self):
"""Get the latest data from website and update the state."""
from bs4 import BeautifulSoup
try:
session = async_get_clientsession(self.hass)
with async_timeout.timeout(5, loop=self.hass.loop):
req = yield from session.get(_RESOURCE)
except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Error while accessing: %s", self._url)
return False
data = yield from req.text()
soup = BeautifulSoup(data, 'html.parser')
keys = ['labels', 'data']
content = []
for line in str(soup.find_all('script')[10]).splitlines():
if line.split(':')[0].strip() in keys:
content.append(str(line.split(' ')[1].strip().replace('[', '').replace('],', '')))
self.attrs = dict(zip(content[1].split(','), content[2].split(',')))
self._state = "The current values"
As for the data source - I’ve been using http://datafeed.expektra.se/datafeed.svc/spotprice since 2014. Gives you the prices from current hour to the last hour of the next day. You can set the bidding area and currency and choose between XML and JSON output.
This custom component is working pretty nicely, but how i could make today and tomorrow as graph? And best if i could make some colors, like green for below 0,005€ and red beyond?
you might be interested to read through this: https://github.com/custom-components/nordpool/pull/4#event-2642324775
There is a macro that can help you add fees. Note that there are some brackets missing from tariff formulas, but these are easy to find.
Timezone error still seems to be a bug in code. Hope that this gets solved at some point.