[SOLVED] Automation unable to turn off smart plug when power consumption decreases below the threshold

Hi. I’m trying to switch off a plug when the power consumption of the battery charger drops below 5W.
This is the automation:

alias: '[CARICABATTERIE] [01] - Spegne il caricabatterie a carica ultimata'
description: ''
trigger:
  - platform: numeric_state
    entity_id: switch.caricabatterie_switch
    value_template: >-
      {{ states.switch.caricabatterie_switch.attributes.current_consumption |
      int }}
    below: 5
    for:
      seconds: 10
condition: []
action:
  - type: turn_off
    device_id: 038fb754a37a66adc676fc27c865e123
    entity_id: switch.caricabatterie_switch
    domain: switch
  - service: notify.mobile_app_iphonese
    data:
      message: Carica della batteria terminata
      data:
        push:
          sound: none
mode: single

I have written it after reading a similar post but the solution doesn’t appears to work for me.
What I’m doing wrong?
Thanks

What “doesn’t work”?
What is the value of {{ states.switch.caricabatterie_switch.attributes.current_consumption | int }} in the template debugger?

The value once charged is finished is between 2.4 and 0 Watts, placing | int has solved the problem of the guy who wrote that post.
I can’t figure out why I can’t have it working…
The template debugger if I write


{{states.switch.caricabatterie_switch}}

returns


template TemplateState(<state switch.caricabatterie_switch=off; current=0, current_consumption=0.0, voltage=223.6

I think you want:

trigger:
  - platform: numeric_state
    entity_id: switch.caricabatterie_switch
    value_template: "{{ state.attributes.current_consumption }}"

See the docs:

Importantly, the numeric_state trigger only fires if the value crosses the threshold. In this case, the value must drop from above 5 to below 5 to trigger. If the switch is on and the value is already below 5, it won’t trigger.

Also, does the automation work if you manually run it (from Configuration / Automations)? That bypasses the trigger and condition, and tests the action.

Ah, you have to specify the attribute in your trigger:

attribute: current_consumption

The trigger is only, by default, evaluated when the state of the entity changes, i.e. on/off.
adding the attribute will make it monitor the attribute in question.

1 Like

I’ve tried to change the automation to that but it doesn’t work. What am I doing wrong?

alias: '[CARICABATTERIE] [01] - Spegne il caricabatterie a carica ultimata'
description: ''
trigger:
  - platform: numeric_state
    entity_id: switch.caricabatterie_switch
    attribute: current_consumption
    value_template: >-
      {{ states.switch.caricabatterie_switch.attributes.current_consumption |
      int }}
    for: '0:00:20'
    below: '10'
condition:
  - condition: device
    type: is_on
    device_id: 038fb754a37a66adc676fc27c865e123
    entity_id: switch.caricabatterie_switch
    domain: switch
action:
  - type: turn_off
    device_id: 038fb754a37a66adc676fc27c865e123
    entity_id: switch.caricabatterie_switch
    domain: switch
  - service: notify.mobile_app_iphonese
    data:
      message: Carica della batteria terminata
      data:
        push:
          sound: none
mode: single

You didn’t include the code I suggested, plus @koying’s addition. Replace your trigger with this:

trigger:
  - platform: numeric_state
    entity_id: switch.caricabatterie_switch
    attribute: current_consumption
    below: 5
    for:
      seconds: 10

Thank you @Troon !
Finally I have understood why the automation didn’t worked before… It was simply because when I was turning on the charger, its power consumption was already below 10 Watts since the battery wasn’t still connected (or it seems to be the reason).
I’ve changed the threshold to a lower value (2.5 W) and now the automation works.
Thank you for your support!

P.S. In the topic that I’ve previously linked is advised to add a value template to convert the float value of the sensor in an integer value for the correct functioning of the automation, that’s the reason why in my code appears

value_template: >-
      {{ states.switch.caricabatterie_switch.attributes.current_consumption |
      int }}

The automation works seamlessly if that line of code is present or not.
Thank you again!

You definitely don’t need to convert float to int for it to work.

Glad it’s all working for you now.