Automation to turn off TP-LINK HS110 (switch) when current power (W) (numeric state attribute) is low not working

I’ve created an automation that should turn off an HS110 smart plug when all of the devices attached to it enter standby and current power (W) drops below 8.

Here are the state attributes reported in the developer tools section (so I know I don’t have a typo):

And here is me testing a template string in the developer tools section:

The value template in the automation is not exactly the same as the template string I tested in developer tools, because I assume (from looking at the automation docs and examples) that state in the context of the automation is already set to states.{entity_id} (e.g. states.switch.office).

I tried it with and without seconds: 15 in the “For” section. I reloaded automations and even completely restarted the server in server control configuration. I watched the current power (W) value both in Home Assistant and the Kasa iOS app while I powered devices up and down for a minute at a time to ensure the current power (W) value would record a change from being above to below the specified value.

My other automations to turn the same switch ON when my solar system is exporting 1000W to the grid every morning is working fine.

What am I doing wrong?

Please try with the following states.switch.office.attributes[current_power_w] instead of just switch.office and report back

@kirichkov Thanks for your reply.

In the automation value template, I tried:

  • {{ states.switch.office.attributes["current_power_w"] }} (works in the developer tools template section)
  • {{ states.switch.office.attributes[current_power_w] }} (this is an error in the developer tools template section)
  • {{ states.switch.office.attributes.current_power_w }} (works in the developer tools template section)

None of these work in my automation. I turn on devices to raise the current power (W) value to >100 and wait 30s then turn them off to lower current power (W) to 5 and wait 30s. I tried with nothing in the For section and with seconds: 5. I setting the Below limit to 50 instead of 8, just in case it was marginally fluctuating across the boundary.

I never had just switch.office in the value template, and for the entity ID, it is selected from a dropdown and I can’t modify it.

I also tried this directly in configuration.yaml:

automation:
  trigger:
    platform: numeric_state
    entity_id: switch.office
    value_template: "{{ state.attributes.current_power_w }}"
    below: 50
    for:
      seconds: 5
  action:
    service: notify.pushover_tai
    data:
      message: Office off
      title: Home Assistant

But this also didn’t trigger. Also note the action is changed to a notification, just for testing.

I also tried entity_id: states.switch.office:

automation:
  trigger:
    platform: numeric_state
    entity_id: states.switch.office
    value_template: "{{ state.attributes.current_power_w }}"
    below: 50
    for:
      seconds: 5
  action:
    service: notify.pushover_tai
    data:
      message: Office off
      title: Home Assistant

Which raised an error when checking my config:

Invalid config for [automation]: Entity ID states.switch.office is an invalid entity id for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 35). Please check the docs at Automation - Home Assistant

Same for entity_id: states.switch.office.attributes["current_power_w"]

Entity ID should be switch.office (without states).

@m0wlheld thanks, it is. That’s what I select from the dropdown in the automations configuration (and can’t change it there). And also that is what I tried first in configuration.yaml.

So, does it work now?

@m0wlheld No, entity ID was switch.office from the beginning. I initially created the automation via the configuration -> automations editor (where I select the entity from a dropdown, and cannot alter it). Then I also tried some other values for both value_template and entity_id directly via configuration.yaml, which didn’t work.

Looking at the very first screenshot in my OP, is there anything wrong? The only thing I can see is that the value template {{ state.attributes.current_power_w }} appears to be assuming that state == states.switch.office.

The automation docs include examples like state.attributes.FOO, like I have, so I believe the configured entity is made available to the value template as state?

And {{ states.switch.office.attributes.current_power_w }} works in the developer tools -> template editor. So I know the attribute is named correctly and being read from the HS110.

Second try:

{{ state.attributes.current_power_w | int }}

If it doesn’t work try putting the attribute in brackets - [“current_power_w”]

So,

this should work. I have a TP Link HS110 by myself and checked the attribute name.

The entity_id must be the switch itself (switch.office). It’s used to watch for changes on that entity in general, while value_template focusses on the actual monitored item.

automation:
  trigger:
    platform: numeric_state
    entity_id: switch.office
    value_template: '{{ states.switch.office.attributes.current_power_w | int }}'
    below: 50
    for:
      seconds: 5
  action:
    service: notify.pushover_tai
    data:
      message: Office off
      title: Home Assistant

I don’t know how or why, but I tested my original config (first screenshot) but with | int added to the value template and that worked. Then I tried again without | int to confirm both the problem (string value) and fix (coerce to int), but it worked! So it looks like my original config was fine, and I must have just been imagining it that it wasn’t working. Sorry everyone for wasting your time!