Hey everyone, I wanted to share a project I’ve been working on to create virtual energy meters for my smart devices. It’s been a fun challenge and I think it could be really helpful for others looking to track energy consumption in their homes.
The Problem
I have several smart devices around my home, like fans and lights, but they don’t come with built-in energy tracking. I wanted a way to monitor how much energy each device was using without having to install physical sensors everywhere.
The Solution
After some research, I discovered that if you can see the state of a device in Home Assistant, you can create a virtual energy sensor for it. Here’s how I did it for my ceiling fan:
-
Measure Power Consumption: I used a real power meter to measure the fan’s consumption at different speeds. Here’s what I found:
- Off: 0 W
- 16%: 5.94 W
- 33%: 7.98 W
- 50%: 12.99 W
- 66%: 17.93 W
- 83%: 23.07 W
- 100%: 37.14 W
-
Create a Template Sensor: I set up a template sensor in Home Assistant to track the fan’s power usage. Here’s the configuration I used:
yaml
- trigger:
- platform: time_pattern
seconds: ‘/2’
sensor: - name: Template PM Living Room Fan Power
icon: ‘mdi:flash’
device_class: power
unit_of_measurement: “W”
state_class: measurement
state: >
{%- if is_state(“fan.livingroom_ceiling_fan”, “on”) -%}
{%- if is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 16) -%}{%- set pwr = 5.94 -%}
{%- elif is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 33) -%}{%- set pwr = 7.98 -%}
{%- elif is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 50) -%}{%- set pwr = 12.99 -%}
{%- elif is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 66) -%}{%- set pwr = 17.93 -%}
{%- elif is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 83) -%}{%- set pwr = 23.0 -%}
{%- elif is_state_attr(“fan.livingroom_ceiling_fan”, ‘percentage’, 100) -%}{%- set pwr = 37.14 -%}
{%- endif -%}
{{ (pwr + range(-5, 5) | random/100)|round(2) }}
{%- else -%}
0.00
{%- endif %}
- platform: time_pattern
- Integrate for Energy Tracking: I used the Riemann sum integral to create an energy sensor. Here’s the configuration:
yaml
sensor:
- platform: integration
source: sensor.template_pm_living_room_fan_power
name: “Template PM Living Room Fan Integral”
unique_id: template_pm_living_room_fan_integral
round: 0
Results
After setting this up, I was able to track the fan’s energy consumption in real-time. It’s been really helpful to see how much energy different settings use and to optimize my usage.
Tips
- Fluctuations: Adding a small random fluctuation to the power sensor ensures that the integration works smoothly.
- Regular Updates: The sensor updates every 2 seconds to capture changes accurately.
Conclusion
This method has been a great way to monitor energy usage without additional hardware. I’d love to hear if anyone else has tried something similar or has tips for improving this setup!
Cheers,
[Your Name]