Automation to record the current "watt" value of plug and turn off when drops by %

I am trying to create an Automation however my (copied/adjusted) attempt just returns with the
“Message malformed: expected float for dictionary value @ data[‘below’]” error.
In turn would really appreciate guidance on my crude attempt, with thanks. Perhaps I am doing this all wrong?

Concept below being:
When (switch.tp_link_p110_02) is turned on for 30seconds, to make note of the value of sensor.tp_link_p110_02_current_power).
Based on this value, to then monitor and turn off the plug when it drops by 10% (or if easier, by x number of watts).
I don’t think I am doing it right by using a target of (input_number_initial_power) as would that assume that I set a value there whereas, I wouldn’t know the value and is what I want to self manage if that makes sense?

alias: Automation TPlink-02 off when below 10% of starting value
description: Turns off the plug if power consumption drops by 10% from the initial reading.
mode: single
triggers:
  - entity_id:
      - switch.tp_link_p110_02
    to: "on"
    from: "off"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 30
conditions: []
actions:
  - target:
      entity_id:
        - input_number.initial_power
    data:
      value: "{{ states('sensor.tp_link_p110_02_current_power') | float }}"
    action: input_number.set_value
  - wait_for_trigger:
      - entity_id: sensor.tp_link_p110_02_current_power
        below: "{{ states('input_number.initial_power') | float * 0.9 }}"
        for: "00:01:00"
        trigger: numeric_state
    continue_on_timeout: false
  - target:
      entity_id: switch.tp_link_p110_02
    action: switch.turn_off
    data: {}

Thanks in advance.

Not sure this is the issue but templates should be enclosed in " "

Thanks for the reply. As in to show as
“{{ states(‘sensor.tp_link_p110_02_current_power’) | float }}”
“{{ states(‘input_number_initial_power’) | float * 0.9 }}”
I tried that to no avail and same error unfortunately. I edited initial post above to include those now.

You cannot template the below: field. Use a wait for template trigger instead.

Thanks, I adjusted where it says “below” to wait. The error then shows as
“Message malformed: extra keys not allowed @ data[‘wait’]”
So something else isn’t right unfortunately!


instead of wait for trigger

1 Like

Unfortunately have got a little lost now. I tried to adjust to be "Wait for a template".but error gets worse and no doubt due to it being iincorrectly implemented as I have no experience with it.
Message malformed: template value should be a string for dictionary value @ data[‘actions’][3][‘wait_template’]

Might have to try from scratch or alternate method, as no idea what I am doing. :woozy_face:

Given that it’s a waiting automation then it wouldn’t be very great anyways.
As I see it the trigger should be this:

"{{ states('sensor.tp_link_p110_02_current_power') < states('input_number_initial_power') | float * 0.9 }}"

And the action is switch off.
That’s it.
Your second automation is to set the input number when the switch turns on.
But doing it all in one line like that is prone to create issues.

You could also use the choose and get something like this:

alias: Automation TPlink-02 off when below 10% of starting value
description: Turns off the plug if power consumption drops by 10% from the initial reading.
mode: single
triggers:
  - entity_id:
      - switch.tp_link_p110_02
    to: "on"
    from: "off"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: turn on
  - trigger: template
    value_template: >-
      {{ states('sensor.tp_link_p110_02_current_power') <
      states('input_number_initial_power') | float * 0.9 }}
    id: template
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - turn on
        sequence:
          - target:
              entity_id:
                - input_number_initial_power
            data:
              value: "{{ states('sensor.tp_link_p110_02_current_power') | float }}"
            action: input_number.set_value
      - conditions:
          - condition: trigger
            id:
              - template
        sequence:
          - target:
              entity_id: switch.tp_link_p110_02
            action: switch.turn_off
            data: {}
2 Likes

Wow, thanks for taking the time to create that.
I pasted that as a new Automation and it saved without errors :grinning:. Now to test it! Will report back.

It didn’t do anything i.e. power remained on after Watts dropped from 18 to 0.
Not sure if is because the Automation is reading plug as 0 when turned on. It needs to wait 30secs for initial reading to show?
Tested: With Automation On and then turning the plug on. I also tested by Turning the Automatation on after plug turned on. Neither worked.
Will try digest it more later as perhaps I am not applying correctly.

It’s using the same trigger as you had.
So it waits 30 seconds then sets the helper value.

What does your traces show?

I just copied your stuff. But that isn’t correct.
It should probably be

input_number.initial_power

dot, not underscore.

There is two of them in your automation

Ah n yes, I adjusted that locally earlier, but not on my initial post which I will do now.
I’ll also adjust on the one I copied from you and test later as about to head out. Will report back. Thanks for that.

Small fix, but this would be better. The first value must be cast to a numeric type too – and I added defaults just to avoid log warnings.

{{ states('sensor.tp_link_p110_02_current_power') | float(0) <
      states('input_number.initial_power') | float(0) * 0.9 }}
2 Likes

Noted, thank you.

I intended to do that but forgot

1 Like

I adjusted (while out and about as had to know), and it works. Excellent!
In case either were mitigating causes to earlier fails? I

  • Adjusted initial read time from 30s to 45s in case it took longer for HA to pick up value.
  • Restarted HA to ensure things kicked in.

This (for ref) is what I now have:

alias: "Automation: Power | TP_Link_P110_02 | Turn off when power (W) drops by 10%"
description: Turns off when power (W) drops by 10%.
triggers:
  - entity_id:
      - switch.tp_link_p110_02
    to: "on"
    from: "off"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 40
    id: turn on
  - trigger: template
    value_template: >-
      {{ states('sensor.tp_link_p110_02_current_power') | float(0) <
      states('input_number.initial_power') | float(0) * 0.9 }}
    id: template
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - turn on
        sequence:
          - target:
              entity_id:
                - input_number.initial_power
            data:
              value: "{{ states('sensor.tp_link_p110_02_current_power') | float }}"
            action: input_number.set_value
      - conditions:
          - condition: trigger
            id:
              - template
        sequence:
          - target:
              entity_id: switch.tp_link_p110_02
            action: switch.turn_off
            data: {}
mode: single

Thank you (and others) so much for taking the time. It is very much appreciated.
I do have a couple of questions on all of this (possible options) but will come back after absorbing (understanding) how above works.

You can read it as two separate automations in one.
The trigger id (amongst others) can be used to differentiate between what part of the code should run in a choose or if then else.

If you find it easier then it can be split to automation one:

alias: "Automation: Power | TP_Link_P110_02 | Turn off when power (W) drops by 10%"
description: Turns off when power (W) drops by 10%.
triggers:
  - entity_id:
      - switch.tp_link_p110_02
    to: "on"
    from: "off"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 40
    id: turn on
conditions: []
actions:
  - target:
      entity_id:
        - input_number.initial_power
    data:
      value: "{{ states('sensor.tp_link_p110_02_current_power') | float }}"
    action: input_number.set_value
mode: single

and automation 2:

alias: "Automation: Power | TP_Link_P110_02 | Turn off when power (W) drops by 10%"
description: Turns off when power (W) drops by 10%.
triggers:
  - trigger: template
    value_template: >-
      {{ states('sensor.tp_link_p110_02_current_power') | float(0) <
      states('input_number.initial_power') | float(0) * 0.9 }}
    id: template
conditions: []
actions:
  - target:
      entity_id: switch.tp_link_p110_02
    action: switch.turn_off
    data: {}
mode: single

I was lazy and didn’t change the alias and descriptions but the two above is the same as the previous.

2 Likes

Ha, yeah I have yet (tomorrow) to look closely at what you included as I spotted the options but yet to separate/understand.
Apologies. I should have waited before replying.with my current “YAML” that works (that included both options),
Having said that, thanks for noting this as you have now explained it for me & more importantly for others that may be reading this and like me, new to automatons using templates. Thank You

Just tested both and each slightly different. I have yet to decide which I will use (perhaps both depending on what I am doing?).

Automation 1:
Needs to be enabled in advance as it only triggers upon detecting the plugs state from off to on.
The device being charged needs to be already connected to ensure that a power reading is detected (above 0w) within 40secs of being turned on.

Automation 2
Can be enabled (turned on) anytime during charging and just looks for a drop of 10% from the time the Automation is enabled.
Pros: Can be enabled without having to toggle power from off to on. Power however must be above 0w from the get go.

Purpose of this Automation: For charging devices. As the power usually starts to drop off once batteries reach 75%+/-, I like to turn off at that point to prolong battery life. In turn this automation should work well with any device. The closer devices gets to 100% charge, the higher the drop in voltage so one could change the 10% (0.9) to be a higher value if preferred.

Thanks again, very much appreciated. One for me to work on perhaps!