TPLINK HS110 switch, use energy consumption as sensor

Hi,

I’m playing with a TPLINK HS110 with tplink switch component https://home-assistant.io/components/switch.tplink/

But I would like to use the energy consumption value as a sensor because this data is inside the switch state attributes:

{ "Current consumption": "369.3 W", "Current": "1.3 A", "Voltage": "289.32 V", "friendly_name": "TPLink Switch HS100", "Total consumption": "0.08 kW" }

Which is the best way to achieve this? create a tplink sensor?

EDIT:

Using template sensor I can get some values but the problem is with the values that have spaces in the name, like “Total Consumption”, this doesn’t work:

   - platform: template
     sensors:
       energy_aquarium:
         unit_of_measurement: kWh
          value_template: '{{ states.switch.tplink_switch_hs100.attributes.CURRENT_CONSUMPTION }}'

Thanks!

1 Like

I was at first considering to get the energy monitor as a sensor, instead of being “attributes” on the switch, but indeed the template platform gets the job done. Here is how to get the “attributes” as a sensor in the UI:

- platform: template
  sensors:
    tp_link_amps:
      value_template: '{{ states.switch.tplink_switch_hs100.attributes["Current"] | replace(" A", "") | float }}'
      unit_of_measurement: 'A'
    tp_link_watts:
      value_template: '{{ states.switch.tplink_switch_hs100.attributes["Current consumption"] | replace(" W", "") | float }}'
      unit_of_measurement: 'W'
    tp_link_kw:
      value_template: '{{ states.switch.tplink_switch_hs100.attributes["Total consumption"] | replace(" kW", "") | float }}'
      unit_of_measurement: 'kW'
    tp_link_volts:
      value_template: '{{ states.switch.tplink_switch_hs100.attributes["Voltage"] | replace(" V", "") | float }}'
      unit_of_measurement: 'V'
10 Likes

Thanks @kirichkov !!! I get it working after edit the tplink switch component file for remove blank spaces in attributes names (i didn’t know this syntax in templates!).

Looks like you got it working, I’m still struggling, though.
Hope you can help me out here.

This is my config:

switch:
  - platform: tplink
    host: 192.168.7.62
sensor:
  - platform: template
    sensors:
      volts:
        value_template: '{{ states.switch.tplink_switch_hs100.attributes.voltage }}'
        unit_of_measurement: 'V'

I get the circle with the ‘V’ in the sensor section, but it doesn’t show a value i there.
What else do I need to do?

is your swtich named “switch.tplink_hs100”?

Also do you see the attribute of voltage displayed next to it? I didn’t think the HS100 pulled power info, only the HS110.

No it’s not called anything - does it need to have a ‘friendly name’?
And it is a HS110, I can see current voltage in the pop-up when U click on it - together with total consumption, current consumption, etc.

Plus:
Wow @silvrr - that was easy :-S
I just gave it a name, replaced the string “switch.tplink_hs100” in the template section and now the voltage is showing up :wink:

Awesome work :D.

quick question, if i know how much my power company charges me per kW, would it be possible to calculate a daily cost for example ?

I assume you know the cost per kWh not kW. If so then just create a template sensor and in the template multiply by your cost in kWh using the daily Kwh consumption.

you are correct about the kWh.

so that would then be : value_template: ‘{{ states.switch.tplink_switch_hs110.attributes[“Daily consumption”] | replace(" kW", “”) | float * 0.095}}’ in which 0.095 is the cost in euro per kWh, right ?

Thanks @kirichkov, that’s a pretty neat bit of templating. :smiley:

Some of you maybe interested in voting for the auto-discovery feature request I just created to simplify your configuration.yaml:

I don’t know where you live, but in Europe prices are (including all type of taxes) in the 0,20 range (0,20Euro/KWh = 200 Euro/MWh

Guys, you recommend this plug? Does it sync well the state on/of? Considerin the price, this seems a good option to integrate with HA.

It does sync the state and the underlying library is actively maintained, so software-wise it’s a solid choice. BUT, I’ve encountered occasional crashes of the plug itself. It stops responding unless you remove it and re-add it. I think it gets worse if you have severe voltage drops. At a place where electricity was unstable I had to reconnect it every few hours. If the voltage was stable at 230-240V, crashes happen once every few months, which I’d consider acceptable given the price.

My choice was to go with Z-wave plugs, however. They are marginally more expensive in Europe, if you are on the lookout for the Amazon deals.

1 Like

I have a few of them, and they’ve worked flawless for me. So I’d recommend them :+1:t3:

1 Like

I like the plug, but I ran into an issue that I documented here:

I still like the plug, though, and it works reliably for me apart from the issue above which I learned to live with.

Tks for the feedback! Bought one to see how things go! Was a bit apprehensive with kirichkov comentary regarding the restart of the plug. Will give it a try for one month to see how things go.

I’ve also had the recomendation of a sonoff S20 with an firmware replacement, and since it’s quite cheaper I’m considering also buying one just to compare both and decide. In the majority of the plugs I have the monitoring is not that important.

Hi all,

I don’t know why but since few updates ago the template sensor doesn’t work.

If I click on the switch info I see all the infos (consuption etc) but none of them are avaible in the template sensor.

Here’s the code:

  - platform: template
    sensors: 
      tp_link_amps:
        value_template: '{{ states.switch.presa_smart.attributes["Current"] | replace(" A", "") | float }}'
        unit_of_measurement: 'A'
      tp_link_watts:
        value_template: '{{ states.switch.presa_smart.attributes["Current consumption"] | replace(" W", "") | float }}'
        unit_of_measurement: 'W'
      tp_link_kwd:
        value_template: '{{ states.switch.presa_smart.attributes["Daily consumption"] | replace(" kW", "") | float }}'
        unit_of_measurement: 'kW'
      tp_link_kwt:
        value_template: '{{ states.switch.presa_smart.attributes["Total consumption"] | replace(" kW", "") | float }}'
        unit_of_measurement: 'kW'
      tp_link_kwdh:
        value_template: '{{ states.switch.presa_smart.attributes["Daily consumption"] | replace(" kW", "") | float * 0.20}}'
        unit_of_measurement: '€'
      tp_link_volts:
        value_template: '{{ states.switch.presa_smart.attributes["Voltage"] | replace(" V", "") | float }}'
        unit_of_measurement: 'V'

Can someone please help me ?

Thank you in advance!

It was a breaking change is 0.55

TP-Link Switch: Attribute names now uses underscores and unnecessary capitalization was removed. (@djchen - #9607) (switch.tplink docs) (breaking change)

3 Likes

it worked! Thank you very much!

Hi, maybe I’m stupid but I can’t make my HS110 work since the last upgrades. The switch works and I can opperate it from HA, but I’cant see any values besides on/off.

So, in my sensors.yaml it looks like this:

  - platform: template
    name: Termo
    sensors:
    tp_link_amps:
      value_template: '{{ states.switch.termo.attributes["current"] | replace(" A", "") | float }}'
      unit_of_measurement: 'A'
    tp_link_watts:
      value_template: '{{ states.switch.termo.attributes["current_consumption"] | replace(" W", "") | float }}'
      unit_of_measurement: 'W'
    tp_link_kw:
      value_template: '{{ states.switch.termo.attributes["total_consumption"] | replace(" kW", "") | float }}'
      unit_of_measurement: 'kW'
    tp_link_volts:
      value_template: '{{ states.switch.termo.attributes["voltage"] | replace(" V", "") | float }}'
      unit_of_measurement: 'V'

and in the switch.yaml it looks like:

- platform: tplink
  host: !secret iptermo
  name: "Termo"

Also, the group is:

termo_info:
name: Termo
icon: mdi:hot-tub
view: yes
entities:
- group.Termo

Termo:
- sensor.tp_link_current_consumption
- sensor.tp_link_watts
- sensor.tp_link_kw
- sensor.tp_link_volts

I can’t find what is wrong with that

1 Like