TPLINK HS110 switch, use energy consumption as sensor

This is my code, it was working perfectly until the 0.61 update. The blog post said some of the attributes were renamed so I’ve done that but still comes back as unknown.

switch:

  • platform: tplink
    host: myipaddress
  • platform: template
    sensors:
    washingmachine_amps:
    value_template: ‘{{ states.switch.washing_machine.attributes[“current_a”] | replace(" A", “”) | float }}’
    unit_of_measurement: ‘A’
    washingmachine_watts:
    value_template: ‘{{ states.switch.washing_machine.attributes[“current_power_w”] | replace(" W", “”) | float }}’
    unit_of_measurement: ‘W’
    washingmachine_kw:
    value_template: ‘{{ states.switch.washing_machine.attributes[“today_energy_kwh”] | replace(" kW", “”) | float }}’
    unit_of_measurement: ‘kW’
    washingmachine_volts:
    value_template: ‘{{ states.switch.washing_machine.attributes[“voltage”] | replace(" V", “”) | float }}’
    unit_of_measurement: ‘V’

I was having the same issues, @Marauder2.

The 0.62 update has thankfully set things right again.

While everyone is updating their TP-Link sensor template, if you so choose, you may also want to update the total power unit of measurement to correctly correspond to the data value provided, i.e. “kWh” instead of the “kW” for the today and total energy attributes.

Been following this string for a while, and wanted to also say thanks to @kirichkov for the great template!

2 Likes

The energy values are all over the place, as reported in here: https://github.com/home-assistant/home-assistant/issues/12029

Hi @timdonovanuk,

Read your github post and noticed a few things that might clarify your HA reporting. First off, I wouldn’t be surprised if power usage over the last week and month were inconsistent. Due to the naming error in HA 0.61, datalogging would have been interrupted for the desired sensors from time of 0.61 update to 0.62. Haven’t confirm any of this, but seems logical to me. Hope this helps! :smile:

I’ve commented inline with your post in bold:

Here is what HA reports:

current_power_w: 0.00 v — This value represents power in watts. Your unit_of_measure is likely noted as Volts.

voltage: 250.2 — looks consistent with Kasa

total_energy_kwh: 1.880 (what is this value supposed to be, as it doesn’t match any even closely) — Not sure what duration the total or today energy use reference for time interval (trailing 24 hr or calendar day usage). I’ll be looking into that when I get a chance.

today_energy_kwh: 0.214 — this is consistent with your 1 day energy use in Kasa, however should have kWh unit of measurement.

current_a: 0.02 — Kasa unfortunately doesn’t report amperage, however your HA report is missing the unit of measurement.

Yes I can confirm that my problems were corrected with the 0.62 update.

Hey thanks @RMoussa for taking a look! However, why doesn’t HA just pull the data from the plug? I’ve not dived into the API but if you take a look at the image of the Kasa app (in the github issue) I would have thought the plug exposes these values via their API. HA just needs to pull them in and provide them as an attribute. HA doesn’t need to track or datalog the value constantly. The plug is the source of truth. As the example on github, could we not just have attributes as follows (that mirror what’s shown in the Kasa app):

30_day_total_energy_kwh: 33.3
7_day_total_energy_kwh: 16.1
1_day_total_energy_kwh: 0.21

30_day_average_energy_kwh: 1.11
7_day_average_energy_kwh: 16.1

1 Like

I am a bit new to this and cant figure out why this isn’t working:

My TP-Link HS110 doesn’t show the attributes. What is wrong?

Careful with Sonoffs, because most of them aren’t even CE approved. They’re very cool devices, but I wouldn’t use it with vey high loads.

1 Like

yeah, that was one of the reasons why I bought the TP-Link at the end.

Here my working code:

    switches:
      - platform: tplink
        host: 192.168.178.75
        name: "Waschmaschine"

sensor:
# Waschmaschine
  - platform: template
    sensors:
      waschmaschine_amps:
        value_template: '{{ states.switch.Waschmaschine.attributes["current_a"] | replace(" A", "") | float }}'
        unit_of_measurement: 'A'
      waschmaschine_watts:
        value_template: '{{ states.switch.Waschmaschine.attributes["current_power_w"] | replace(" W", "") | float }}'
        unit_of_measurement: 'W'
      waschmaschine_kw:
        value_template: '{{ states.switch.Waschmaschine.attributes["total_energy_kwh"] | replace(" kW", "") | float }}'
        unit_of_measurement: 'kW'
      waschmaschine_volts:
        value_template: '{{ states.switch.Waschmaschine.attributes["voltage"] | replace(" V", "") | float }}'
        unit_of_measurement: 'V'   

group.yaml
Schalter:
name: Schalter
entities:
- switch.waschmaschine
- sensor.waschmaschine_amps
- sensor.waschmaschine_watts
- sensor.waschmaschine_kw
- sensor.waschmaschine_volts

6 Likes

How do I create a sensor that displays monthly consumption?
Inn Kasa App i can see this information.

Thanks!

Old thread, but i would really appreciate some help. I’ve been trying to get this to work for a week :frowning:

I am on 0.103.6 version.
TP Link HS 110 Plugs: Hardware version 2.1. Firmware 1.5.7
The name of the plug in the Kasa App is ‘office’.
The plugs work fine on/off in HA but i can’t get the energy monitoring to work. It shows ‘Unavailable’

This is what i have in configuration.yaml:

sensor:
  - platform: template
    sensors:
      office_amps:
        value_template: '{{ states.switch.office.attributes["current_a"] | replace(" A", "") | float }}'
        unit_of_measurement: 'A'
      office_watts:
        value_template: '{{ states.switch.office.attributes["current_power_w"] | replace(" W", "") | float }}'
        unit_of_measurement: 'W'
      office_kw:
        value_template: '{{ states.switch.office.attributes["total_energy_kwh"] | replace(" kW", "") | float }}'
        unit_of_measurement: 'kW'
      office_volts:
        value_template: '{{ states.switch.office.attributes["voltage"] | replace(" V", "") | float }}'
        unit_of_measurement: 'V' 

Also in configuration.yaml:

tplink:
  discovery: false
  switch:
    - host: 192.168.0.38

Can someone please help? I’ve been trying desperately to get this to work without success. Thanks.

EDIT: NVM, i found the issue. I was taking the name from Smartthings which was renamed wrongly and not straight from the Tplink integration which was named correctly. All is good now.

Hi all,
I am considering buying an HS110 to measure consumption of my dishwasher and also to detecte when it is off.
For the ones that already using it, do you know how precise (in time) is the consumption reported?
(I don’t want my automation to detect my dishwasher is done after 1 hour. :grin: )
Thanks,

Maybe someone who knows the details will respond with specifics. To me it appears to be virtually real time. Maybe it polls on some interval, but it’s too quick for me to notice. I’m running these on two sump pumps, so I don’t need millisecond accuracy. The resolution is at least within 10 seconds or so, maybe better.

Real enough to real time to be unimportant in this use case (I think it’s updated every 8 seconds).

You’ll find the dishwasher or whatever you use it on isn’t constantly using power anyway, so you’ll need to incorporate a delay. As an example, I used one to monitor my heat pump dryer and for part of the cycle it wasn’t using any detectable amount of power. So to ensure that there were no false positives, I had to add a delay of 15 minutes (so power < x for 15 minutes, send a notification) to get rid of the false positives.

However, for the washing machine I was able to reduce that delay down to 5 minutes.

So you’ll need to do a few cycles with the device to get a baseline usage pattern to work out when the cycle is actually finished.

The change of the consumption is really quick.

For my wasching maschine and dryer I use a delay of 3 minutes under 10W to determine if one of the maschines has finished.

Hey guys,

I’m new to Home Assistant and having trouble pulling the energy attributes from my HS110.

I’ve tried the following, but value comes up as unavailable.

My entity ID is switch.07_upper_garage_plug
The name I’ve given it is upper

I’ve tried both of the following in the config…yaml file to no avail.

sensor:

  • platform: template
    sensors:
    my_tp_switch_watts:
    value_template: ‘{{ states.switch.upper.attributes[“current_power_w”] | float }}’
    unit_of_measurement: ‘W’

sensor:

  • platform: template
    sensors:
    my_tp_switch_watts:
    value_template: ‘{{ states.switch.07_upper_garage_plug.attributes[“current_power_w”] | float }}’
    unit_of_measurement: ‘W’

What am I missing?

Any help would be appreciated.

Thanks!

Edit: I found the issue! My plug was named “07. Upper Garage Plug” in the KASA app. I thought the “.” was causing issues, so I renamed the plug to Upper Garage Plug in the KASA app, rebooted HA, and all is working now!

All:

For the life of me I can’t figure out what I am doing wrong. I have a LB130 and I can see the watts in the developer tools states, but when I try to add it as a template to my sensors yaml, I am not able to see just the watts as a sensor in developer tools:

  - platform: template
    sensors:
      familyroomne_watts:
        friendly_name_template: "{{ states.light.familyroomne.name}} Current Consumption"
        value_template: '{{ states.light.familyroomne.attributes["current_power_w"] | replace(" W", "") | float }}'
        unit_of_measurement: 'W'

Any ideas where I am going wrong? I have zero experience with Templates. I have tried to understand it but I can’t quite seem to grasp the idea.

Thanks!

I worked this out yesterday, this is working for me:

      value_template: '{{ states.switch.yamaha_rx_v2400_mains_power.attributes["current_power_w"] | float }}'
      unit_of_measurement: 'W'

It looks like you’ve got an extra bit in there: replace(" W", "")
Maybe that’s the problem?