How do i automate smart plug to turn off if power consumption is lower than x for 1 minute?

Hello,

I’ve been using Home Assistant for about a month now (running HAOS directly on a Pi 5) successfully set up a few automations, but there’s one particular automation I can’t seem to figure out. I’m trying to manage my smart plug (switch.aqara_plug_switch), specifically focusing on its power consumption as monitored by sensor.aqara_plug_power. The objective is to have the switch automatically turn off under two conditions:

  1. When it’s turned on but not in use, leading to a power draw of less than 5W for more than 1 minute.
  2. When it has been used, but the power consumption subsequently drops below 5W for over 1 minute.

I can’t get the automation meant to turn off the switch to trigger. (it works if I manually run it). Here are the YAML configurations I’ve been working with:

First attempt:

trigger:
  - platform: template
    value_template: "{{ states('sensor.aqara_plug_power') | float < 5 }}"
    for: "00:01:00"
action:
  - service: switch.turn_off
    entity_id: switch.aqara_plug_switch
mode: single

Second attempt:

trigger:
  - platform: numeric_state
    entity_id: sensor.aqara_plug_power
    below: 5
    for:
      minutes: 1
action:
  - service: switch.turn_off
    entity_id: switch.aqara_plug_switch
mode: single

Could anyone offer some advice on what might be causing this issue or suggest how I might adjust my automation to work as intended? I’d greatly appreciate any guidance or examples of similar automations that have been successful.

Thank you!

What does a trace of your second attempt tell you?

Did the value of sensor.aqara_plug_power start above 5 or has it been above 5 since you saved the automation?

Thanks for pointing this out. I forgot to mention that the automation works when the value of sensor.aqara_plug_power start above 5. The problem was getting it to trigger when the switch turned on but never reached a number bigger than 5.

I think that i’ve actually managed to find a solution but i don’t know if its a good one.
It seems to be working so far.

trigger:
  - platform: state
    entity_id: switch.aqara_plug_switch
    to: "on"
action:
  - wait_for_trigger:
      - platform: numeric_state
        entity_id: sensor.aqara_plug_power
        below: 5
        for: "00:01:00"
    timeout: "02:00:00"
    continue_on_timeout: false
  - service: switch.turn_off
    entity_id: switch.aqara_plug_switch
mode: restart

Triggers only evaluate when they become true. So your new one triggers when the switch turns on. Then waits for another event (your wait for trigger)

Your original code did not work if your device was turned on and never reached the threshold output (because the trigger will never happen)

Read the trigger section as when ‘this’ (your trigger) becomes true. (you can have a list of multiple triggers BTW, I think you’ll need it)

Read condition as ‘only if’ or ‘unless’

The action is this happens (you’re good there)

You could have instead added another trigger to check if the device turned on and never reached threshold then turn off.

3 Likes

supporting @NathanCu’s good answer… take a quick read of this.

4 Likes