knudsvik / energyscore Goto Github PK
View Code? Open in Web Editor NEWCustom Integration for Home Assistant to score how energy is utilized based on price point
License: MIT License
Custom Integration for Home Assistant to score how energy is utilized based on price point
License: MIT License
Since version 0.4.0 the score adds up constantly. It shows a value higher than 100%. This did not happen before this version.
The attributes show this:
state_class: measurement
energy_entity: sensor.zonneplan_p1_electricity_consumption_today
price_entity: sensor.zonneplan_current_electricity_tariff
quality: 1
total_energy:
2022-12-28T12:00:00+0100: 8.65
2022-12-28T13:00:00+0100: 9.1
2022-12-28T14:00:00+0100: 9.68
2022-12-28T15:00:00+0100: 10.2
2022-12-28T16:00:00+0100: 10.81
2022-12-28T17:00:00+0100: 11.52
2022-12-28T18:00:00+0100: 12.34
2022-12-28T19:00:00+0100: 13.08
2022-12-28T20:00:00+0100: 13.68
2022-12-28T21:00:00+0100: 14.12
2022-12-28T22:00:00+0100: 14.54
2022-12-28T23:00:00+0100: 14.95
2022-12-29T00:00:00+0100: 0.38
2022-12-29T01:00:00+0100: 0.8
2022-12-29T02:00:00+0100: 0.96
2022-12-29T03:00:00+0100: 1.63
2022-12-29T04:00:00+0100: 2.03
2022-12-29T05:00:00+0100: 2.45
2022-12-29T06:00:00+0100: 3.04
2022-12-29T07:00:00+0100: 3.47
2022-12-29T08:00:00+0100: 3.89
2022-12-29T09:00:00+0100: 4.54
2022-12-29T10:00:00+0100: 5.02
2022-12-29T11:00:00+0100: 5.34
price:
2022-12-28T12:00:00+0100: 0.21
2022-12-28T13:00:00+0100: 0.19
2022-12-28T14:00:00+0100: 0.16
2022-12-28T15:00:00+0100: 0.19
2022-12-28T16:00:00+0100: 0.2
2022-12-28T17:00:00+0100: 0.21
2022-12-28T18:00:00+0100: 0.21
2022-12-28T19:00:00+0100: 0.18
2022-12-28T20:00:00+0100: 0.18
2022-12-28T21:00:00+0100: 0.15
2022-12-28T22:00:00+0100: 0.12
2022-12-28T23:00:00+0100: 0.1
2022-12-29T00:00:00+0100: 0.08
2022-12-29T01:00:00+0100: 0.08
2022-12-29T02:00:00+0100: 0.07
2022-12-29T03:00:00+0100: 0.07
2022-12-29T04:00:00+0100: 0.07
2022-12-29T05:00:00+0100: 0.08
2022-12-29T06:00:00+0100: 0.08
2022-12-29T07:00:00+0100: 0.08
2022-12-29T08:00:00+0100: 0.12
2022-12-29T09:00:00+0100: 0.18
2022-12-29T10:00:00+0100: 0.2
2022-12-29T11:00:00+0100: 0.19
last_updated: '2022-12-29T11:59:46.345340+01:00'
unit_of_measurement: '%'
icon: mdi:speedometer
friendly_name: Total Energy Score
Check: https://developers.home-assistant.io/docs/asyncio_working_with_async/
E.g. need to add some await when pulling states for price and energy.
A dishwasher that runs for 90 minutes cannot score 100% even when run in the chespest hour. Could be 100% if run during the two cheapest hours.?
Probably have to mock an entity with attributes first to have something to restore.
Dicts should maybe have clock hours like "17-18" instead of "17". See how nordpool does this with full date time objects. This may need to be broken down into an even smaller item (just replace with datetime). Using datetime objects should also fix elif self.attr[ENERGY_YESTERDAY] is not None:
to check if it actually is from yesterday.. no need to use the value if it's a week old. Be aware that the times are in UTC in database, need to verify local.
Having a default and optional icon defined in the config. The default is already in place in .const, but has not been implemented.
Updating should be close to the whole hours, e.g. every minute between 13:58 and 14:02. Alternatively push updates when a new value of energy or price is available, see some info here.
Get coverage report and badge
Introduced new potential bug with Nordpool: when processing new data, energy last hour is just added from the hour before. How to verify that it is still in sync with the state?
Sensor name is now name
, should be named after yaml config.
Changing the price_entity in the config is not reflected in the setup after restart. Is it restored maybe?
Add the repo to HACS for easier installation
0.3.1
- platform: energyscore
name: House Energy Score
unique_id: a6e5f61a-0761-478a-8520-87612e0c1ea5
price_entity: sensor.electricity_cost
energy_entity: sensor.home_energy_meter_gen5_electric_consumed_kwh
Sensor initialization is throwing an error at https://github.com/knudsvik/EnergyScore/blob/master/custom_components/energyscore/sensor.py#L114-L116. I assume it happens because the price_entity is not setup yet when the sensor is built.
I've confirmed this hypothesis by simply removing the line (the integration is then working as expected).
I would suggest to delay this piece of code to later (async_added_to_hass
or later in process_new_data
since there is no way to know the order of entity bootstrap as far as I know).
2022-12-22 18:29:13.546 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up energyscore platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform
await asyncio.shield(task)
File "/config/custom_components/energyscore/sensor.py", line 63, in async_setup_platform
async_add_entities([EnergyScore(hass, config)], update_before_add=False)
File "/config/custom_components/energyscore/sensor.py", line 116, in __init__
if entity_reg.async_get(self._price_entity).platform == "nordpool":
AttributeError: 'NoneType' object has no attribute 'platform'
Define a sensor name based on the entity_id if the name is not provided. PowerCalc does this.
Create a service to purge attribute data like price and energy, to clean poor data.
Energy in the first hour of the day does not seem to be recorded in the attributes. Leading to wrong score and wrong quality
Multiple sensors defined in configuration.yaml
are not catered for yet.
Every time Home Assistant restarts, the sensors are set to 100%. Should remember their previous values.
See info here.
Check that the input entities are of device_class energy and currency? See https://www.home-assistant.io/integrations/sensor/
UI:
YAML:
** EnergyScore sensors **
Potential savings in local currency if the energy use was moved to the cheapest hour.. or cheapest hours rather.
potential also needs actual price, so maybe that is a third sensor
There are two async methods to set and check unique id in a config flow step, but if implemented as-is it will be over written by the class instantiation. See here for docs.
Necessary if an options flow is to be created, e.g. changing the entities (will change the unique ID per now) or future options.
Without setting anything up, like
sensor
- platform: powerscore
Could it be set up with the totals from the energy dashboard?
Create a custom visualisation, eg. based on apexchart. Could have energyscore, energy, quality, sensitivity. Code and picture to be included in repo.
Example from user in Home Assistant Norge facebook thread:
`type: custom:apexcharts-card
apex_config:
legend: false
chart:
height: 240px
show:
loading: false
experimental:
color_threshold: true
header:
show: true
show_states: true
colorize_states: false
chart_type: radialBar
series:
Is there a need to update the MIT license?
Sensor unavailability should be showed with which of them is unavailable. Currently it does not report to the logger which of them.
Refactor dicts:
unavailable
throws right exceptionunknown
throws right exceptionSome info on testing here: https://developers.home-assistant.io/docs/development_testing/
Custom component tutorial has some intro on testing.
An EV charger should maybe look for best timing over a week, not just the intra-day
See powercalc and Easee repos for badges
Could get data from NordPool sensors attribute for the whole day, but does it matter if the integration can remember states through restarts? It could help with being more correct throughout the day since it knows the price for the whole day in advance,.
The sensitivity for energy-price current day could be an attribute. Needs to look at historic price-variations, either by saving them or looking at the long term statistics of the entity.
Using a sensor entity that does not exist returns None and raises an AttributeError at line 278 (for price).
Optional input: Treshold for energy use, e.g. for a dishwasher that uses a tiny bit of standby power.
Users may want to only run dishwasher between 07:00 and 22:00. Would make sense to be able to configure this and only normalise price for these hours.
0.3.2
- platform: energyscore
name: House Energy Score
unique_id: a6e5f61a-0761-478a-8520-87612e0c1ea5
price_entity: sensor.electricity_cost
energy_entity: sensor.home_energy_meter_gen5_electric_consumed_kw
Score is reset to 100% everyday at midnight.
It means the only moment where score is precise is 1min before midnight.
I think the correct behavior would be to have a rolling score on the last 24h.
When energy usage is 0, the dot-function is reducing the score (if energy same day in not cheapest hour). Usage = 0 should not be included in calc..
As the developer I want users to send me a log file with all relevant entries when they encounter issues.
Automatic tests in Github Action
the entity_id
of the sensor should be created based on the name
attribute.
A sensor which haven't used any energy should be 100%, not 0%
0.3.1
- platform: energyscore
name: House Energy Score
unique_id: a6e5f61a-0761-478a-8520-87612e0c1ea5
price_entity: sensor.electricity_cost
energy_entity: sensor.home_energy_meter_gen5_electric_consumed_kw
Update of the entity fails with:
Traceback (most recent call last):
File "/config/custom_components/energyscore/sensor.py", line 268, in async_update
self._state = self.process_new_data()
File "/config/custom_components/energyscore/sensor.py", line 233, in process_new_data
_norm_prices = normalise_price(self.attr[PRICES])
File "/config/custom_components/energyscore/sensor.py", line 73, in normalise_price
return {
File "/config/custom_components/energyscore/sensor.py", line 74, in <dictcomp>
key: (max_value - value) / (max_value - min_value)
ZeroDivisionError: float division by zero
(line numbers are a bit shifted because I added some log lines to understand the issue).
The price dictionary contains 2 values with the same value (thus the division by zero).
It happens on a setup without nordpool sensor: it's a simple price sensor to follow peak/offpeak price.
Sensor UoM should be %
, now it does not have anything.
Sensor updates from real entities provided in config. See line 126 here for an example.
price_entity and energy_entity needs to handle state unavailable
.
Attribute that is some kind of a quality indicator:
Check docs here.
source entity_id's as state attributes of the sensor. See powercalc sensor for demo.
Integration name in HA without unique_id is now sensor
, should be EnergyScore
Something to do with unique_id
it seems, when defining it the it will change to energyscore
for the sensors that have a valid input. For those without it still is sensor
. Several other components use entry.get(CONF_UNIQUE_ID)
instead of just config[CONF_UNIQUE_ID]
, e.g. modbus, bayesian or generic thermostat.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.