Powercalc - Virtual power sensors

In the last few weeks I have been working on a custom component to get virtual power sensors for your lights or other devices. It is stable enough right now to share it on this forum and make dedicated topic for it.

The component provides multiple strategies to calculate estimated power consumption. One of the most interesting is the LUT method (using lookup tables). Some community members and me have taken measurements for all kind of different brightness/hue/saturation settings and their respective power values.
For coloured lights that are approximately 40.000 unique data points. Currently 58 light models are supported for this method (mostly Philips Hue), but this will keep extending.
Besides this you can also define the power values of a device yourself using linear mode and fixed value. With a few lines of configuration you can have a power sensor for your appliance.

You can make all kind of cool dashboards with this data, and see the power consumption of individual devices in your entire home. And optimize your power consumption.
Also you can add these inidividual sensors to the HA energy dashboard.

preview

Let me know if this is useful in your installation. Any question, shoot.

79 Likes

Awesome work @bramski!
Thank you so much. This component will make power tracking of devices without powermeter so much easier :slight_smile:

2 Likes

@bramski - I saw this pop up in HACS. I’m interested. I have a lot of sensors that I have created to calculate power usage based on the state and attributes of entities. I’m probably going to install this and test it out before migrating.

For example, here is a sensor I am using to calculate the power usage of my kitchen cabinet lights. They are LIFX-Z strips. They don’t report power but I know roughly what power they use based on their brightness. It isn’t perfect but it is basically accurate.

One of the issues I ran into was that because this isn’t a real power report coming from a device, there wasn’t any variation in the power usage all day for a light that was turned on and not changed. The impact of this was that the corresponding sensor I created to track energy usage didn’t work properly for reasons I don’t fully understand but I think based on how they do the math in the integration platform, they need the data to change to calculate energy usage. That is why you see me add states("input_number.calc_switch") | float to the power value. This input_number alternates every 5 seconds or so from .0001 to 0 and back to .0001… This keeps the number changing without really impacting its value.

Have you experienced this issue with your component?

I am very interested in migrating as creating these sensors the way I am takes a lot of time.

Power Sensor Template

platform: template
sensors:
  kitchen_cabinet_lights_power:
    value_template: '{{
      "%0.1f"| format(((
      state_attr("light.kitchen_cabinet_lights", "brightness") | replace("None", 0.0) | float /
      255 *
      state_attr("light.kitchen_cabinet_lights", "entity_id")| length *
      states("input_number.lifx_strip_power_rating") | float ) +
      (0.4 *
      state_attr("light.kitchen_cabinet_lights", "entity_id")|length
      ) +
      states("input_number.calc_switch") | float
      ))
    }}'
    friendly_name: "Kitchen Cabinet Lights"
    unit_of_measurement: "W"
    icon_template: mdi:flash
    device_class: power

Energy Meter Template

platform: integration
source: sensor.kitchen_cabinet_lights_power
name: Kitchen Cabinet Lights Energy Meter
method: trapezoidal
unit_prefix: k
round: 2
unit: kWh

Hi, I don’t use the integration platform myself (yet) so I don’t know if my component solves your problem.
My component works by listening to state changes of the bounded entity (for example a light), whenever this light brightness or hue changes the power sensor will change state accordingly. When the light does not change, the power sensor also doesn’t change state.
You can create your power sensor with a few lines of configuration the powercalc component, so maybe you are able to test with one sensor before migrating all your lights.
Let me know your findings.

I don’t know if it is in scope or not but it would be great if it will be also added fixed valuse for devices like sonoff basic, shelly1 and so. What do you think?

This is already supported. In fact, I am using a Shelly smartplug myself to switch my desk light, which I configured to use the fixed mode.

But if I understand well you will not count also power consumption of the device itself ( more or less 1w for shelly1 devices based on this video = Shelly1 power consumption - YouTube )

So for example in this case

sensor:
  - platform: powercalc
    entity_id: light.nondimmabled_bulb
    fixed:
      power: 20

In frontend I should see 21 against 20.

I hope my comment is clear.

1 Like

You can use standby_usage option for that:

sensor:
  - platform: powercalc
    entity_id: light.nondimmabled_bulb
    fixed:
      power: 20
    standby_usage: 1

Than the power sensor should display 21 when turned on and 1 when the device is turned off

1 Like

Correction, it will display 20 when turned on so you need to set fixed power to 21 as well.

2 Likes

Great! Thank you! But my question was that maybe those info can be added in the LUT and, sometimes should be possible to combines LUTs.(when you have switches and smart lamps combined)

Example:

lamp1 with no dimmerable lights managed by shelly1 (all my ceiling lamps)

sensor:
  - platform: powercalc
    entity_id: light.living_room
    manufacturer: shelly
    model: shelly1


that will replace this
    fixed:
      power: 21
    standby_usage: 1

lamp2 philips hue lamp mounted on a floor lamp controlled by a shelly plug S:

  - platform: powercalc
    entity_id: light.floor_lamp
    manufacturer: signify
    model: shellyplugs

So the sensor will sum 2 LUTs (one for the bulb and the other for the plug).

Too complicated (maybe the example 2 is a bit overkill )?

1 Like

Hi friend, I’m testing your application and it has been very useful for my automation, but when I try to add entities that are not automatically discovered (configured directly in configuration.yaml) they don’t work, power sensors don’t show up in my home assistant , what do I do so that I can manipulate them?

You should check the logs for errors. And maybe enable debug logging (see last chapter in readme). Hope this will give any clues what the problem is.

This is an example of a part of my configuration and my problem

switch in configuration.yaml:

  - platform: mqtt
    name: Central Cozinha
    state_topic: "mr2/status"
    command_topic: "mr2/cmd"
    payload_on: "1"
    payload_off: "0"
    state_on: "1"
    state_off: "0"

sensor in configuration.yaml:

  - platform: powercalc
    entity_id: switch.central_cozinha
    standby_usage: 0.6
    fixed:
      power: 36.9


log:

2021-07-20 17:28:51 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up powercalc platform for sensor
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/entity_platform.py", line 250, in _async_setup_platform
    await asyncio.shield(task)
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensor.py", line 120, in async_setup_platform
    entity_name = entity_state.name
AttributeError: 'NoneType' object has no attribute 'name'

I have 5 switch entities and 2 light entities configured manually in my configuration.yaml, but none of them the electrical consumption sensor works only in auto-discoveries, what to do?

1 Like

Could you please create an issue in github? I need to make some code changes to support these entities. Will have a look into this next weekend.

2 Likes

thank you very much for your attention, i just created an issue there

1 Like

You can create a group of sensors to sum power usage of multiple powercalc sensors into a single sensor. Have a look at this github issue for an example how to do this.
The purpose of LUT files included in the component are to lookup power consumption for different brightness levels and color temperatures of a known light. The files are created by changing the light and measuring power consumption for each brightness level. Behind a shelly switch there can be all kind of different lights/appliances which we don’t know the power consumption. This is different for each user. You have to configure those sensors manually. This is not what the build in lut mode is for.

1 Like

Hi
Thanks for you plugin.
I’m eager to try using it in home assistant, but a very simple question:
How do you get the Watt display beneath the bulb?

I have added

sensor:
  - platform: powercalc
    entity_id: light.ph80

to my configuration.yaml
but from there, I’m not able to figure out, what the next step is

Cheers
Lars

Powercalc will create a seperate power sensor (entity) for your light named sensor.ph80_power. You can find this sensor in ConfigurationEntities or with the developer tools.
When this sensor is not created you should have a look into the logs for any errors.

It’s up to you to show this sensor and your light control in lovelace UI. In the example in github I am using a light card and a gauge card (these are both build in cards in home assistant)

For example:

type: vertical-stack
cards:
  - type: light
    entity: light.ph80
  - type: gauge
    entity: sensor.ph80_power

Hi,
Thanks for your answer
My problem is, I’m not getting the sensor.ph80_power entity created
I’ll try to figure it out

Cheers
Lars

Have a look at the logs. You can also enable debug logging, see the readme. Also check if your light is in the supported models list.