Power consumption from Xiaomi smart plugs in HA

Like all good people, I have the Xiaomi gateway blocked from internet access at the router. This means I can’t see the power consumption of the various Xiaomi plugs in the house. Is there a way to record the consumption in HA? I would imagine it would be done via a sensor…

1 Like

I’d be interested in others applications for this to. You can pull the attribute out of the plug no problem but that is only current consumption. You would need to the feed this into a history type sensor but my understanding this is really only for hours on which doesn’t really help in this case.

I can see in the attributes

{
“in_use”: 1,
“load_power”: 103.24,
“power_consumed”: 746,
“battery_level”: 100,
“friendly_name”: “Plug_158d00028fea8f”,
“icon”: “mdi:power-plug”
}

so load power is great - but you’re right, need a history sensor to record the power consumed every x seconds (the attribute above has no time series data) and convert this into something useful… I’ll do some digging, unless someone has any clues how to achieve this?

Maybe you could feed this attribute into an external dB like influx and then graph it with grafana for last month/day etc in fact I’m going to try this when I get home. I’ll let you know how it goes.

in my sensors.yaml

- platform: template
  sensors:
    dishwasher_power_sensor:
      value_template: '{{ states.switch.plug_158d00023eeb2a.attributes.load_power }}'
      unit_of_measurement:  W
    study_power_sensor:
      value_template: '{{ states.switch.plug_158d00023ff169.attributes.load_power }}'
      unit_of_measurement:  W
    washingmachine_power_sensor:
      value_template: '{{ states.switch.plug_158d0002431039.attributes.load_power }}'
      unit_of_measurement:  W

in my history_graphs.yaml

power:
  name: Power Graph
  entities:
  - sensor.dishwasher_power_sensor
  - sensor.study_power_sensor
  - sensor.washingmachine_power_sensor
  - sensor.tumbledryer_power_sensor
  hours_to_show: 96
  refresh: 120
3 Likes

Looks good walaj, is the data recorded forever or just the last rolling 96 hrs? Or is it just displaying the last 96 hrs worth of data?

Edit: while it doesn’t explicitly mention it, I believe all values are saved in the SQLite db forever? https://www.home-assistant.io/components/history/

Edit2: history uses https://www.home-assistant.io/components/recorder/ and that has purge settings. I think this is it. Thanks all

No the dB is purged every 10 days by default you need to specify if you want longer. The example above I have tried but everytime you reboot the figures set back to zero and you can’t specify the period like last month to do comparison to current month or this time last year etc. I think influx and grafana will enable the best option if you actually wish to use the data to improve power bills etc.

Right… if you replace SQLite with something like Postgres (memory says you can do this in the documentation) would it retain the data? Or how do you feed the attribute into a different database?

I’m thinking id use something like power bi to do month on month comparison

All good, I have read up on influxDB, it’s purpose is bang on for what is needed. data is streaming to influx now, so all I need to do is visualise it with graphene or chronograph, just as @jimpower said. Thanks all

1 Like

And here is it in all its glory in chronograf:

load over time (this is the TV - you can see the drain when the TV is on standby. I think that small spike at 3am is the apple tv downloading updates):

Consumption over time in kwh (you can see the creep up in consumption between when the TV is off die to that 5-6 w leak somewhere):

Both load and consumption:

2 Likes

@CousinLarry I’m looking for a smart plug that reports power consumption. I am wondering is this the plug you are using?

If yes, can I use this for this component https://www.home-assistant.io/components/switch.xiaomi_miio/ ?

Other than controlling the switch, the component can also support power consumption reports?

That’s correct, however I am using the zigbee version, not the wifi version so I would do a little research to make sure wifi works.

Power consumption etc is an attribute exposed to home assistant - the plugin you link to just reports consumption, it does not recorded it and report historical info. I’m getting great results recording the ebtirety of all history of home assistant to influx dB, including power consumption.

I dont think the wifi one is supported in HA.

@masterkenobi I use both versions of the switch and they both are compatible with HA, zigbee version will be auto discovered with xiaomi_aqara platform, the WiFi requires you to get the token. The WiFi version does not do power consumption however only has temperature sensor. See below

Thanks. Will get the zigbee version then.

If power consumption is reported as attribute, then you can create a template sensor that will take the value from the attribute as the sensor value. For example…

sensor:
  - platform: template
    sensors:
      power_consumption:
        friendly_name: "Power Consumption"
        unit_of_measurement: 'W'
        value_template: "{{ state_attr('switch.plug_xxxxxx', 'load_power') }}"

then HA will record the value of the sensor in the db.

thanks for your clarification.

Anyone know how often the plug reports it’s current power load? I just got one and the reporting of the usage seem to be pretty random?

1 Like

I recently blocked internet access to my gateway. My zigbee switches still show up in HA as expected.
However, they are not reporting load_power anymore. If I unblock internet, the load_power value in HA starts updating again.

Anyone else get this behaviour?

I can concur! I wanted to use power consumption to detect when plugged in device is on (a pump) and to trigger automation. Unfortunately, I cannot get it to work as power is not updated regularly, sometimes not at all.

Did you manage to find any solution?

Hello guys,

I have the zigbee version ZNCZ04LM (Mi smart plug (zigbee)) with coinbee 2 and deconz.
it paired very well but i don’t have the power consumption. i only see switch.smart_plug_3 but no power consumption attribue.
ConBee says it has “Smart plug with consumption data” and [zigbee2mqtt.io] - which i don’t use - says the same. Is that some Deconz feature missing for this plug ?
I have other plug such as SP120 with power consumption working

Any idea ?

I have the same problem with ZHA integration and a conbee2. For the moment, i didn’t find any solution.
I’m just able to have instant consumption !
I need help too.