Limit trigger

Hi,

I have an automation that triggers a switch when a certain power sensor value comes above 150. (150Watt)
When the sensor is below 150W then the switch is turned off or should turn off.
I do this with this automation:

alias: "[STUDIO] Switch speakers based on PC power"
description: ""
trigger:
  - platform: state
    entity_id: sensor.sonoff_pow_r2_2_power
condition: []
action:
  - service: |-
      {% if states('sensor.sonoff_pow_r2_2_power') | float(0) > 150 %}
          switch.turn_on
      {% else %} 
          switch.turn_off 
      {% endif %}
    entity_id: switch.sonoff_pow_r2_3

This works perfectly fine except that it gets triggered every few seconds which is obvious because the sensor value change every few seconds (due to power usage). Ofcourse you won’t notice a turn_on action when it’s already ON or a turn_off action when it’s already OFF…

What I want: A trigger that activates when the sensor changes from any value below 150 to a value above 150 AND/OR when the value changes from a value above 150 to a value below 150.
So it’s not constantly triggered when is above 150 (or below 150)

Hope my explanation is clear. :grinning:

Use two numeric state triggers:


  trigger:
  - platform: numeric_state
    entity_id: sensor.xxx
    below: 150
    id: 'off'

  - platform: numeric_state
    entity_id: sensor.xxx
    above: 149
    id: 'on'

  action:
    - service: switch.turn_{{ trigger.id }}
      target:
        entity_id: …

1 Like

Thanks for your reply.
But I think this would probably do they same because as the sensor changes every few seconds (it’s a Power usage value). So if it changes from i.e 223 to 256 it also gets triggered because it’s both above 150 and that continues every few seconds while they switch is already on.
Same visa versa when power stays below 150 (and switch is off) it gets triggered every few second while being below 150. Right?

Your automation triggers the action on every state change regardless if the value is below or above 150. The second automation triggers only if the sensor value trespasses the given trigger value. Or in other words: If the wattage is 149 or lower the automation gets triggered in this moment; same if the wattage rises above 149.

P.S.: Of course, if the value permanently jumps between 149 and 150, the second automation example also will be triggered permanently.

Exactly but my problem is that I get many triggers this way in my logs which most didn’t do an action.
Also, I can’t see when it was triggered last with doing an action.

Looking for a cleaner way where it will only get triggered once and is not triggered while the value stays above 150 (even if it changes from value as long it’s greater than 150)

The automation gets triggered only in this single moment, wenn the threshold is crossed. Or did I understand you wrong?

1 Like

My apologies. Your solution actually works perfectly!
I was misunderstanding so thanks for your help!

I’m saving this one. It’s amazingly efficient! Was using a poller that would run every minute before and it was just really chatty. This only runs when the condition goes above/below the thresholds and instantly turns on/off a paired device!