Power Strip Automation

Hello Guys!,

I’m a newbie in automation. I would like to setup automation to turn off “switch.listwa_biurko” when “load_power” is below 40 Watts. Let me share my automation code:

- id: Turn Off power strip
  alias: Turn Off power strip
  initial_state: 'on'
  trigger:
    platform: numeric_state
    entity_id: switch.listwa_biurko
    value_template: '{{ load_power }}'
    below: 40
    for:
      minutes: 1
  action:
    - service: switch.turn_off
      entity_id: switch.listwa_biurko

Please let me know where I made a mistake here?
The platform is xiaomi_miio

Thank you

Can you post your code with </> button in the text editor.

Done, thanks!

Try this:

- id: Turn Off power strip
  alias: Turn Off power strip
  initial_state: 'on'
  trigger:
    platform: numeric_state
    entity_id: switch.listwa_biurko
    value_template: '{{ state.attributes.load_power }}'
    below: 40
    for:
      minutes: 1
  action:
    - service: switch.turn_off
      entity_id: switch.listwa_biurko

The trigger needs to know where in the json dictionary to grab information. providing just ‘load_power’ does not give the trigger the full map to the state you need to grab.

All entitys return a state, example: {{ states.switch.listwa_biurko.state }}
All entitys contain attributes, if you want to access them: {{ states.switch.listwa_biurko.attributes.load_power }} or {{ states.switch.listwa_biurko.attributes[‘load_power’] }}

Inside a trigger, when using a value template and you’ve specified an entity_id, you can just call “state” in your value template to essentially grab {{ states.switch.listwa_biurko }}.

In time you will get used to this crap. Just remember, everything is an object and you’ll always be accessing the state of the object, or an attribute inside the object.

1 Like

Works perfectly! Thank you very much for the explanation