Automation not triggering although the conditions are met

I’ve been trying to automate the smartplug in my bedroom to switch off when it isn’t used in the evening.

The conditions are:
Lights in the living room (lights.woonkamer_verlichting) are off.
The smartplug in the living room is off (switch.tv_meubel)
It’s later than 10 pm and earlier than 6 am.

It triggers when the usage of the switch.slaapkamer is below 6W and higher than 1W. This should prevent it from switching off when the Missus is watching Netflix in the bedroom. Thus keeping the WAF intact :slight_smile:

I’m using the following automation:

- id: slaapkamer_plug_auto_off

  initial_state: 'on'

  alias: Slaapkamer Plug Auto Off

  trigger:

    platform: numeric_state

    entity_id: sensor.slaapkamer_watts

    below: 6

    above: 1

    for:

      minutes: 1

  condition:

    condition: and

    conditions:

    - condition: time

      after: '22:00'

      before: 06:00

    - condition: state

      entity_id: light.woonkamer_verlichting

      state: 'off'

    - condition: state

      entity_id: switch.tv_meubel

      state: 'off'

  action:

  - service: switch.turn_off

    entity_id: switch.slaapkamer

The only thing I can think of why it isn’t triggering, is that the W usage of my switch is fluctuating…
But I’ve tested it by using other conditions (light.toilet off and switch.daughter off) and then it works.

I’ve checked if the states of the switch and lights group are correct, and they are off so it should meet the conditions…

Here’s what you need to know about triggers.

They will only fire when a state changes such that their previous state was outside of the trigger conditions and their next state is inside.

Lets take a look at your trigger.

  trigger:
    platform: numeric_state
    entity_id: sensor.slaapkamer_watts
    below: 6
    above: 1
    for:
      minutes: 1

Ok, this will trigger when the smart plug is between 1 and 6 Watts for an entire minute. Seems odd to have an upper or lower bound here. I would think one of them would be sufficient. (i.e. only have below 6). This could be one reason why it’s not firing. If it drops below 1W for any reading, it would reset the 1 minute timer.

Ok, here’s the important part. This will fire once when it’s below 6 for 1 minute. Great! What does it do now?

It checks the conditions.

If all conditions are true, plug should turn off. Else, plug will remain on.

Here’s the kicker. It wont ever retrigger until it goes above 6 watts again. This means when the light and tv eventually turn off, the plug wont turn itself off. It’s going to be on all night until the trigger state leaves the bounds of the trigger, then reenters.

I’m not sure by what you mean with “it doesn’t trigger”. I assume you only tested when one of those conditions wasn’t true at the time of testing. I’m guessing if you tested after 10 pm and the light and tv were already off, THEN the power usage went below 6, it would have worked.

Thus, you have a few options.

  1. Trigger on a periodic timer to reevaluate the states of the tv and lights.

  2. Trigger at 10pm and wait for the other conditions to be true

  3. Trigger on something else, not power usage.

I like #2 personally.

trigger:
  # Start the madness at 10pm
  platform: time
  at: "22:00:00"
condition:
  # Only bother if this plug is on
  condition: state
  entity_id: switch.slaapkamer
  state: 'on'
action:
    # Wait until the TV is off. Continues right away if TV is already off.
  - wait_template: "{{ is_state('switch.tv_meubel', 'off') }}"
    # Next, wait until the light is off. Continues right away if light is already off.
  - wait_template: "{{ is_state('light.woonkamer_verlichting', 'off') }}"
    # Finally, make sure the power usage is below 6 Watts.
    # Note, this wont wait for it to be less thn 6 Watts for 1 minute. It will just turn off as soon as it's 
    # below 6 watts. 
  - wait_template: "{{ states('switch.slaapkamer') | float < 6 }}"
    # Now turn off the switch.
  - service: switch.turn_off
    entity_id: switch.slaapkamer
   
2 Likes

Oh wow this is an excellent reply :heart_eyes:

I have another automation to switch the plug back on every morning. But this bit helped me:

So when the TV in the bedroom is being used, the automation will trigger again (because it went above the 6W boundary). But if the TV hasn’t been used the entire day, it will not trigger.

Thanks for clarifying this for me.

Your example however is a much better one.
So if I read it correctly, the automation will run and wait till the next state changes and pause if it doesn’t.

Very neat!! Thank you!

Oh, the powered switch is for a TV.

In my example, it will essentially turn off the instant it is reported to be below 6W. As long as while it’s on it will 100% be above 6W, it should be OK. You might want to lower the number, not sure. I’d test what a completely black screen on mute uses for power. That might be the lowest power state while on? Just a guess.

My solution might not even work too well! But hopefully now you’ll be able to think about triggers in a slightly different way and you’ll be able to come up with something that works well for you.

It’s for a plug that has the TV and Decoder connected to. The stand-by usage is fluctuating between 5,3 and 5.8W
When it’s switched on the usage is way above 6W. So no issues there (I hope).

It’s always cool to see how others automate things and learn from there. I’ve already started investigating how I can optimize my other automations thanks to your example :slight_smile:

1 Like