Add total daily energy sensor (#79)

* Add total daily energy sensor

* Remove image
This commit is contained in:
Otto Winter 2018-11-10 14:31:59 +01:00 committed by GitHub
parent a416804209
commit 4504fff418
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 243 deletions

View File

@ -0,0 +1,69 @@
Total Daily Energy Sensor
=========================
The ``total_daily_energy`` sensor is a helper sensor that can use the energy value of
other sensors like the :doc:`HLW8012 <hlw8012>`, :doc:`CSE7766 <cse776>`, etc and integrate
it over time.
So this component allows you to convert readings in ``W`` or ``kW`` to readings of the total
daily energy usage in ``Wh`` or ``kWh``.
.. code:: yaml
# Example configuration entry
sensor:
- platform: total_daily_energy
pin: 12
name: "Total Daily Energy"
power_id: my_power
# The power sensor to convert, can be any power sensor
- platform: hlw8012
# ...
power:
id: my_power
# Enable time component to reset energy at midnight
time:
- platform: sntp
id: my_time
Configuration variables:
------------------------
- **power_id** (**Required**, :ref:`config-id`): The ID of the power sensor
to integrate over time.
- **name** (**Required**, string): The name of the sensor.
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
- All other options from :ref:`Sensor <config-sensor>` and :ref:`MQTT Component <config-mqtt-component>`.
Converting from W to kW
-----------------------
Some sensors such as the :doc:`HLW8012 <hlw8012>` expose their power sensor with a unit of measurement of
``W``. To have your readings in ``kW``, use a filter:
.. code::
sensor:
# The power sensor to convert, can be any power sensor
- platform: hlw8012
# ...
power:
id: my_power
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kW
See Also
--------
- :ref:`sensor-filters`
- :doc:`hlw8012`
- :doc:`cse7766`
- :doc:`/esphomeyaml/cookbook/power_meter`
- :doc:`API Reference </api/sensor/total_daily_energy>`
- `Edit this page on GitHub <https://github.com/OttoWinter/esphomedocs/blob/current/esphomeyaml/components/sensor/total_daily_energy.rst>`__
.. disqus::

View File

@ -1,242 +0,0 @@
Total Energy Consumption Per Day
================================
Some components (like the :doc:`Sonoff POW R1 </esphomeyaml/components/sensor/hlw8012>`,
:doc:`Sonoff POW R2 </esphomeyaml/components/sensor/cse7766>`, :doc:`self-made power meter <power_meter>`, ...)
can provide the power usage in watts - so the amount of energy currently consumed per unit of time.
However, sometimes it's desired to have a sensor that shows the total energy consumption per day in watt **hours**.
Fortunately, you can use existing components inside esphomeyaml/lib to create such a sensor. The configuration
is split up into three main components:
- A :doc:`template sensor </esphomeyaml/components/sensor/template>`
- The power meter (for example :doc:`power_meter`)
- A :doc:`time component </esphomeyaml/components/time>`
The template sensor here is basically just a stub. It doesn't report any values itself (`return {}`), and
all state updates will be supplied from outside using `publish_state`. We're giving it a ``name`` so that
it shows up in the frontend, an ``id`` so that we can address it in :ref:`lambdas <config-lambda>`.
Additionally, a ``unit_of_measurement`` is set (so that the value shows up with a unit in the front end)
and the default ``filters`` are disabled (see :ref:`sensor-default_filter`).
The power meter (here with a generic platform ``<THE_PLATFORM>``), observes the power values as it would normally.
Each time the pulse counter receives a value, a lambda is triggered that houses most of the logic. In this lambda,
we have a static variable ``last_update`` which we use to get the relative time in hours since the last call of our
lambda. Then we just multiply the value ``x`` from the power meter by the relative time (formula ``Energy = Power * time``).
Lastly, that value is published to the template sensor.
And the ``time`` component is in charge of resetting the total energy every day at midnight (you can of course
change the time for your needs).
.. code:: yaml
sensor:
# This is the sensor displaying total energy consumed.
- platform: template
lambda: 'return {};'
update_interval: never
name: "Total Daily Energy Usage"
id: total_energy
unit_of_measurement: kWh
# Disable filters
filters:
accuracy_decimals: 2
# This is the power meter which outputs values
- platform: <THE_PLATFORM>
# ...
# Disable default filters
filters:
on_value:
lambda: |-
static uint32_t last_update = 0;
if (last_update == 0)
id(total_energy).publish_state(0);
// the time since the last update in hours
float delta_time = (millis() - last_update) / 1000.0 / 60.0 / 60.0;
last_update = millis();
// x is the power in kW.
// the energy consumed in this interval in kWh (E = P * t)
float consumed_energy_kwh = x * delta_time;
id(total_energy).publish_state(id(total_energy).state + consumed_energy_kwh);
time:
- platform: sntp
id: time
on_time:
# Reset total energy at midnight
seconds: 0
minutes: 0
hours: 0
then:
lambda: |-
id(total_energy).publish_state(0);
Configuration for Pulse-Counter based Power Meters
--------------------------------------------------
.. code:: yaml
sensor:
# This is the sensor displaying total energy consumed.
- platform: template
lambda: 'return {};'
update_interval: never
name: "Total Daily Energy Usage"
id: total_energy
unit_of_measurement: kWh
# Disable filters
filters:
accuracy_decimals: 2
# This is the power meter which outputs values
- platform: pulse_counter
pin: GPIO12
unit_of_measurement: 'kW'
name: 'Power Meter'
filters:
- multiply: 0.06
on_value:
lambda: |-
static uint32_t last_update = 0;
if (last_update == 0)
id(total_energy).publish_state(0);
// the time since the last update in hours
float delta_time = (millis() - last_update) / 1000.0 / 60.0 / 60.0;
last_update = millis();
// x is the power in kW.
// the energy consumed in this interval in kWh (E = P * t)
float consumed_energy_kwh = x * delta_time;
id(total_energy).publish_state(id(total_energy).state + consumed_energy_kwh);
time:
- platform: sntp
id: time
on_time:
# Reset total energy at midnight
seconds: 0
minutes: 0
hours: 0
then:
lambda: |-
id(total_energy).publish_state(0);
Configuration for Sonoff POW R1
-------------------------------
.. code:: yaml
sensor:
# This is the sensor displaying total energy consumed.
- platform: template
lambda: 'return {};'
update_interval: never
name: "Total Daily Energy Usage"
id: total_energy
unit_of_measurement: kWh
# Disable filters
filters:
accuracy_decimals: 2
# This is the power meter which outputs values
- platform: hlw8012
sel_pin: 5
cf_pin: 14
cf1_pin: 13
# current, voltage, ...
power:
name: "HLW8012 Power"
on_value:
lambda: |-
static uint32_t last_update = 0;
if (last_update == 0)
id(total_energy).publish_state(0);
// the time since the last update in hours
float delta_time = (millis() - last_update) / 1000.0 / 60.0 / 60.0;
last_update = millis();
// x is the power in W.
// the energy consumed in this interval in kWh (E = P * t)
float consumed_energy_kwh = x * delta_time / 1000.0;
id(total_energy).publish_state(id(total_energy).state + consumed_energy_kwh);
time:
- platform: sntp
id: time
on_time:
# Reset total energy at midnight
seconds: 0
minutes: 0
hours: 0
then:
lambda: |-
id(total_energy).publish_state(0);
Configuration for Sonoff POW R2
-------------------------------
.. code:: yaml
logger:
baud_rate: 0
uart:
rx_pin: RX
baud_rate: 4800
sensor:
# This is the sensor displaying total energy consumed.
- platform: template
lambda: 'return {};'
update_interval: never
name: "Total Daily Energy Usage"
id: total_energy
unit_of_measurement: kWh
# Disable filters
filters:
accuracy_decimals: 2
# This is the power meter which outputs values
- platform: cse7766
# current, voltage, ...
power:
name: "CSE7766 Power"
on_value:
lambda: |-
static uint32_t last_update = 0;
if (last_update == 0)
id(total_energy).publish_state(0);
// the time since the last update in hours
float delta_time = (millis() - last_update) / 1000.0 / 60.0 / 60.0;
last_update = millis();
// x is the power in W.
// the energy consumed in this interval in kWh (E = P * t)
float consumed_energy_kwh = x * delta_time / 1000.0;
id(total_energy).publish_state(id(total_energy).state + consumed_energy_kwh);
time:
- platform: sntp
id: time
on_time:
# Reset total energy at midnight
seconds: 0
minutes: 0
hours: 0
then:
lambda: |-
id(total_energy).publish_state(0);
See Also
--------
- :doc:`power_meter`
- :doc:`/esphomeyaml/components/sensor/cse7766`
- :doc:`/esphomeyaml/components/sensor/hlw8012`
- :doc:`/esphomeyaml/components/sensor/pulse_counter`
- `Edit this page on GitHub <https://github.com/OttoWinter/esphomedocs/blob/current/esphomeyaml/cookbook/total_energy.rst>`__
.. disqus::

View File

@ -125,6 +125,7 @@ Sensor Components
SHT3X-D, components/sensor/sht3xd, sht3xd.jpg
TCS34725, components/sensor/tcs34725, tcs34725.jpg
Template Sensor, components/sensor/template, description.svg
Total Daily Energy, components/sensor/total_daily_energy, sigma.svg
TSL2561, components/sensor/tsl2561, tsl2561.jpg
Ultrasonic Sensor, components/sensor/ultrasonic, ultrasonic.jpg
Uptime Sensor, components/sensor/uptime, timer.svg
@ -275,7 +276,6 @@ This list contains items that are technically already supported by other compone
TEMT6000, cookbook/temt6000, temt6000.jpg
Non-Invasive Power Meter, cookbook/power_meter, power_meter.jpg
Dual Relay Motor Cover, cookbook/dual-r2-cover, sonoff_dual_r2.jpg
Total Energy Per Day, cookbook/total_energy, sigma.svg
Do you have other awesome automations or 2nd order components? Please feel free to add them to the
documentation for others to copy. See :doc:`Contributing <guides/contributing>`.