Manually calculating power consumption of lights

I’d be interested in a database. I’ve done a Hue ambience and have an Hue candle RGB that I could do, but that would get pretty messy - four inputs rather than two.
No idea how to upload or share the data though

Hi

Picking this thread up again now I have time - has anyone come to a conclusion on how to do this? I appreciate the measurements for all bulbs may take time but looking at @CM000n example, do we create a sensor for every bulb

Or is this more of a script , where we can maintain a block of code that looks at all of your hue bulbs and then looks at their %brightness in order to calculate the watts used and then present the entire wattage used in the house ?

We can maintain the hue build models and their associated values by brightness level within the script ?

I just finished my code. It’s based off measurements of power by adjusting mired and brightness. I then fit a curve for each of the mired value sets, with brightness as the variable using a second order polynomial on this site Polynomial Regression Data Fit. The measured data at the bottom of this post is from a Philips Hue LTW001 bulb.

More than happy for feedback on the code.

- platform: template
  sensors:
    living_room_big_lamp_power:
      friendly_name: Living Room Big Lamp Power
      icon_template: mdi:lightning-bolt
      unit_of_measurement: "W"
      value_template: >
        {% if is_state('light.living_room_big_lamp','on') %}
          {% set mired = state_attr("light.living_room_big_lamp","color_temp") %}
          {% set brightness = ((state_attr("light.living_room_big_lamp","brightness") - 3) * (100 - 1)) / (254 - 3) + 1 %}
          {% set m153 = (0.000435254 * (brightness**2) + (0.002105929 * brightness) + 1.500175321) %}
          {% set m203 = (0.00057964 * (brightness**2) + 0.001134825 * brightness + 1.527090668) %}
          {% set m253 = (0.000785198 * (brightness**2) - 0.002785236 * brightness + 1.57216088) %}
          {% set m303 = (0.000733083 * (brightness**2) - 0.002574077 * brightness + 1.573079888) %}
          {% set m353 = (0.000668279 * (brightness**2) - 0.001853391 * brightness + 1.561981489) %}
          {% set m403 = (0.000570268 * (brightness**2) + 0.0000671178176666897 * brightness + 1.527920206) %}
          {% set m454 = (0.000487813 * (brightness**2) - 0.000161527 * brightness + 1.518884275) %}
          {% if mired >= 153 and mired < 203 %}
            {{ ((((mired - 153) * (m203 - m153)) / (203 - 153)) + m153)|round(2) }}
          {% elif mired >= 203 and mired < 253 %}
            {{ ((((mired - 203) * (m253 - m203)) / (253 - 203)) + m203)|round(2) }}
          {% elif mired >= 253 and mired < 303 %}
            {{ ((((mired - 253) * (m303 - m253)) / (303 - 253)) + m253)|round(2) }}
          {% elif mired >= 303 and mired < 353 %}
            {{ ((((mired - 303) * (m353 - m303)) / (353 - 303)) + m303)|round(2) }}
          {% elif mired >= 353 and mired < 403 %}
            {{ ((((mired - 353) * (m403 - m353)) / (403 - 353)) + m353)|round(2) }}
          {% elif mired >= 403 and mired < 454 %}
            {{ ((((mired - 403) * (m454 - 403)) / (454 - 403)) + m403)|round(2) }}
          {% elif mired == 454 %}
            {{ m454|round(2) }}
          {% endif %}
        {% else %}
          {{ 0.00 }}
        {% endif %}
Active Power (W)							
Kelvin	6535.947712	4926.108374	3952.56917	3300.330033	2832.86119	2481.389578	2202.643172
MIRED	153	203	253	303	353	403	454
BRIGHTNESS
1	1.52	1.53	1.55	1.54	1.54	1.53	1.52
10	1.56	1.59	1.59	1.61	1.6	1.58	1.57
20	1.7	1.78	1.85	1.83	1.8	1.75	1.72
30	1.94	2.08	2.23	2.18	2.13	2.04	1.96
40	2.28	2.51	2.76	2.69	2.59	2.46	2.23
50	2.71	3.05	3.44	3.32	3.18	2.98	2.76
60	3.2	3.69	4.23	4.05	3.84	3.59	3.28
70	3.78	4.42	5.16	4.92	4.66	4.3	3.89
80	4.45	5.32	6.29	5.99	5.64	5.16	4.63
90	5.23	6.32	7.67	7.26	6.8	6.16	5.48
100	6.05	7.45	9.22	8.72	8.11	7.25	6.36
1 Like

@cloak, which device/sensor did you use to measure the power consumption of the bulb? I want to measure some other Hue lights but not sure how to measure low AC current accurately.

I used a Power-Mate Lite: https://www.systemcontrol.com.au/product-specs/11470505/PML10A

I can’t remember where i got it, or how much it cost.

If you have a smart plug with a meter in it, that may work. I have’t done a back to back comparison, so I’m not sure of the accuracy (of a Smartthings smart plug).

I’m trying to get some test rig setup using a current sensor which I can attach to a ESP microcontroller or raspberry Pi.
Want to automate setting all different brightness and mired combinations using the Hue API and than saving the current readings to a JSON file / database.
I have these sensors ACS712 and SCT-013, but I am not sure about their accuracy on low currents, need to test this soon.
Will get back to you when I get this working.

1 Like

Hi @cloak

Thanks so much for sharing that hue sensor template, although I’m not exactly sure what to do with it to work with my hue bulb?

My bulb has the entity id of light.living_room_light so i have updated your references to “ light.living_room_big_lamp” but i can’t see anything related on the UI or in the developers tools view ?

Hey @nodecentral, if youve changed only the light entity, once you reboot you should have an entity named sensor.living_room_big_lamp_power - third line of the code.

Screenshot 2021-05-16 215831

Cool approaches guys :slight_smile:

@cloak , did you really do all the measurement points?
With an interval of 1-100% brightness and 153-500 mired, it should “only” result in 34353 data points.
The best thing would really be if we had stored them somewhere. Then everyone can store a lookup table in their private database and query the values via an SQL sensor depending on brightness and colour temperature.
Then there would be no need for such inaccurate approximations via some formulas :wink:

@CM000n, I would sort of like to say that I made 34353 measurements…
Mired in 50 mired(s?) steps and brighness in 10% steps, which works out to 77.

I’m all for storing these measurements, but wouldn’t know where to start.

Maybe we can agree on some sort of JSON format or CSV format and publish to a git repository.

Something like:

[
    {
        "model": "LTW001",
        "measurements": [
            {
                "brightness": 1,
                "mired": 153,
                "watt": 1.52
            },
            {
                "brightness": 1,
                "mired": 203,
                "watt": 1.53
            }
        ]
    }
]

Or maybe one json file per Hue model.
Than multiple people can contribute to having measurements for more models, and later we can build some tools to import this data into a SQL database. And maybe build a custom component or something like that.

Btw. how would we approach heaving measurements for RGBW lights? should we take measurements for all brightness, R, G, B, W combinations, which is way to much if we do all 1-255.

1 Like

pfff, only about ~1.6 million datapoints. Easy peasy :sunglasses:

The coolest thing would be if someone could write a Python script that iterates through all the possible combinations and writes the measurements for them away in a database.

Maybe something like this?:

# Set bulb to measure and corresponding powermeter
delay_time_secs = 30
entity_id = data.get("entity_id")
powermeter = data.get("powermeter")

for bri in range(1, 101):
    for r in range(1, 256):
        for g in range(1, 256):
            for b in range(1, 256):
                print(bri, r, g, b)
                # 1.Set specified Bulb to current bri,r,g,b values
                service_data = {"entity_id": entity_id, "rgb_color": [r, g, b], "brightness": bri}
                hass.services.call("light", "turn_on", service_data, False)
                # 2.Wait 30 Seconds to get correct power values
                time.sleep(delay_time_secs)
                # 3.Read power values
                powerconsumption = hass.states.get(powermeter)
                # 4.Write power values with bri,r,g,b values to specified database
                try:
                    connection = psycopg2.connect(user="MYUSER",password="MYPASS",host="192.168.178.3",port="5442",database="ha_sensordata")
                    cursor = connection.cursor()

                    postgres_insert_query = """ INSERT INTO powermeasures_rgb (manufacturer, bulb, bri, r, g, b) VALUES (%s,%s,%s,%s,%s,%s)"""
                    record_to_insert = ('signify', 'testbulb', bri, r, g, b)
                    cursor.execute(postgres_insert_query, record_to_insert)

                    connection.commit()
                    count = cursor.rowcount
                    print(count, "Record inserted successfully into powermeasures_rgb table")

                except (Exception, psycopg2.Error) as error:
                    print("Failed to insert record into powermeasures_rgb table", error)

                finally:
                    # closing database connection.
                    if connection:
                        cursor.close()
                        connection.close()
                        print("PostgreSQL connection is closed")

Then we can make the measurements available, as you say, as CSV or JSON, preferably via a git repo.

Sadly Home Assistant Python Scripts doesn’t seem to support sql connections :frowning:

I might have to take a look at the Home Assistant REST API to be able to control and read entities through it. Then we could use an external Python script that allows writing to an database.

I’ve been procrastinating on doing a few measurements for my Hue candle light to see if there’s any pattern that could make additional measurements more targeted.

Later tonight I’ll have a look at the accuracy of my Smartthings Smart Plug vs the meter i originally used. I should be able to do some sort of calibration and your code could/should work…I will definitely need help making the code happen - I don’t know where to even put it!

Thanks for your input. There is a small issues with your approach.
There are just too many datapoints. It’s not 1.6 million but 1.6 billion. This will take 52 years to process and I forgot about the 30 seconds delay which would take 1577 years. Around that time we will be fully colonized on Mars :wink:

The reason I wanted to investigate taking measurements with a direct sensor on a Raspberry PI is to eliminate the need to wait for x seconds to have the Zwave/Zigbee/Wifi plug communicate the new Power measurements. I also have some Zwave smartplugs at home. Will do some testing with them as well.

To minimalize the amount of datapoints we need to measure we can do in steps of 10 as @cloak also did in his measurements. Or maybe steps of 5. This will be accurate enough imo to just pick the closest datapoint when calculating the power consumption in Home Assistant.

Also I see Hue uses the HSL color value (Hue, Saturation, Lightness). I think it would be the best to take measurements in this format.
Hue = 0 - 65535 (take in steps of 1000, which would be 65 points)
Sat = 0 - 255 (take in steps of 10 for 25 points)
Bri(L) = 0 - 255 (also in 10 steps)

For a total of 65x25x25 = 40625 measurements
Let’s say we need to wait 5 seconds between each measurement than it will take 56 hours, which will be fine.

There is no need to save this data to a relational database first. Why not directly save to a JSON file using the python file IO and json libraries? This will eliminate the need of other external dependencies.

When we have taken measurements in JSON files we can develop some python library which can load the JSON files in memory and have a function to give the power consumption for a given model and the closest HSL values. Than this library could be consumed by some custom_component or maybe in the official Hue component.

Let me know what you think.
I can setup a GIT repo in the weekend.

1 Like

I am curious if you get similar results with both meters or the results are all over the place.

You are absolutely right. Sorry for the confusion with our German number formats :wink:
Maybe I overshot the mark a little. Otherwise we’ll all be old by the time we have any consumption values. :smiley:
This also makes it clear why there are so few detailed consumption figures from official sources.

Well, there’s a couple of problems with my experiemental design:

  1. Voltage fluctuations, which have an imact on the readings at such low power. This could be overcome by running it off my UPS

  2. Precision of the Smartthings readings. These are at the integer level only and I don’t know how to customize the device/entity to get to two decimal place precision. This will eliminate this plug to be used unless anyone has any advice on how to change it?

  3. Lag from the change in readings, this basically eliminates the Smartthings plug, unless someone knows how to modify the scan frequency. It’s in the order of 5-10 seconds before the readings change after a brightness change. For comparison, using my calibrated eyeometer, it’s less than a second to settle on the Power-Mate meter.

  4. The direction of increase or decrease of brightness seems to impact the actual power value at which the the Smarthings plug changes up or down - ie, it can hold a power reading at say 4W when reducing brightness values, and when it changes to 3W the brightness/actual power is lower than if increasing from 3W to 4W

Anywho, below are the readings. I increased by 5% brightness, and where you see a non multiple of 5 reading, that’s where i tried to land the power mate as close as possible to an integer value of power to see what happened with the plug readings.

Brightness	Power-Mate (W)	Smartthings (W)
OFF	0.44	1
1	1.53	3
5	1.54	3
10	1.61	3
15	1.69	3
20	1.84	3
25	2.01	3
30	2.21	3
35	2.45	3
40	2.74	3
44	2.98	3
45	3.06	3
50	3.41	4
55	3.77	5
58	4.01	5
60	4.2	5
65	4.65	5
69	5.04	5
70	5.11	5
75	5.63	6
78	6	6
80	6.2	7
85	6.85	7
86	6.94	8
90	7.48	8
93	8.02	9
95	8.34	9
99	8.98	10
100	9.09	10

Simon, would it be possible if you share a snipped on how you created the nice grafana dashboard?
I have the Wattage Sensors running since a year or so, and I just fall over this conversation here. Unfortunately, I never did anything with it and adding it to Grafana looks quite nice.
Thanks

Hi @cloak

Many thanks for responding, I’m afraid I can’t see any sensor called living_room_big_lamp_power however there is the following error in the logs.

Any ideas ?

2021-05-18 23:04:28 ERROR (MainThread) [homeassistant.components.homeassistant] Invalid config for [sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got None extra keys not allowed @ data['living_room_big_lamp_power']. Got OrderedDict([('friendly_name', 'Living Room Main Light Power'), ('icon_template', 'mdi:lightning-bolt'), ('unit_of_measurement', 'W'), ('value_template', '{% if is_state(\'light.living_room_light\',\'on\') %}\n {% set mired = state_attr("light.living_room_light","color_temp") %}\n {% set brightness = ((state_attr("light.living_room_light","brightness") - 3) * (100 - 1)) / (254 - 3) + 1 %}\n {% set m153 = (0.000435254 * (brightness**2) + (0.002105929 * brightness) + 1.500175321) %}\n {% se.... (See ?, line ?).

Full code from configuration.yaml is

sensor:
  - platform: snmp
    host: 10.10.10.103
    community: public
    baseoid: 3.6.1.2.1.1.1.0
  - platform: fixer
    api_key: 5609c7need-to-hide-this7bc2fc1f3a16
    target: USD
  - platform: template
    sensors:
    living_room_big_lamp_power:
      friendly_name: Living Room Main Light Power
      icon_template: mdi:lightning-bolt
      unit_of_measurement: "W"
      value_template: >
        {% if is_state('light.living_room_light','on') %}
          {% set mired = state_attr("light.living_room_light","color_temp") %}
          {% set brightness = ((state_attr("light.living_room_light","brightness") - 3) * (100 - 1)) / (254 - 3) + 1 %}
          {% set m153 = (0.000435254 * (brightness**2) + (0.002105929 * brightness) + 1.500175321) %}
          {% set m203 = (0.00057964 * (brightness**2) + 0.001134825 * brightness + 1.527090668) %}
          {% set m253 = (0.000785198 * (brightness**2) - 0.002785236 * brightness + 1.57216088) %}
          {% set m303 = (0.000733083 * (brightness**2) - 0.002574077 * brightness + 1.573079888) %}
          {% set m353 = (0.000668279 * (brightness**2) - 0.001853391 * brightness + 1.561981489) %}
          {% set m403 = (0.000570268 * (brightness**2) + 0.0000671178176666897 * brightness + 1.527920206) %}
          {% set m454 = (0.000487813 * (brightness**2) - 0.000161527 * brightness + 1.518884275) %}
          {% if mired >= 153 and mired < 203 %}
            {{ ((((mired - 153) * (m203 - m153)) / (203 - 153)) + m153)|round(2) }}
          {% elif mired >= 203 and mired < 253 %}
            {{ ((((mired - 203) * (m253 - m203)) / (253 - 203)) + m203)|round(2) }}
          {% elif mired >= 253 and mired < 303 %}
            {{ ((((mired - 253) * (m303 - m253)) / (303 - 253)) + m253)|round(2) }}
          {% elif mired >= 303 and mired < 353 %}
            {{ ((((mired - 303) * (m353 - m303)) / (353 - 303)) + m303)|round(2) }}
          {% elif mired >= 353 and mired < 403 %}
            {{ ((((mired - 353) * (m403 - m353)) / (403 - 353)) + m353)|round(2) }}
          {% elif mired >= 403 and mired < 454 %}
            {{ ((((mired - 403) * (m454 - 403)) / (454 - 403)) + m403)|round(2) }}
          {% elif mired == 454 %}
            {{ m454|round(2) }}
          {% endif %}
        {% else %}
        {{ 0.00 }}
        {% endif %}
```