Alias trigger If the value of the sensor is in the correct range of a certain time

i have xiaomi zigbee plug with such load power sensor

  - platform: template
    sensors:
      xiaomi_current_power:
        friendly_name: 'load power'
        value_template: '{{ float(states.switch.plug_158xxx.attributes.load_power) }}'
        unit_of_measurement: W

in result i see 0.0 W or 350.45 W

i want to do alias which turn off this xiaomi plug if the value of load_power’s sensor will drop below 50 but above 30 and will be within that range at least 30 min

too hard trigger for me now (
that something like this
pls correct me

- alias: "Turn off"
  trigger:
    - platform: numeric_state
      entity_id: sensor.xiaomi_current_power
      below: 60
      above: 30
      for:
        minutes: 30
action:
    - service: switch.turn_off
      entity_id: switch.plug_xiaomi

Automation triggers are OR logic so for your code: if your sensor is above 30 or if it is below 60. So it will always be true. Not good.

Automation conditions are AND logic by default. If you put your test as an automation condition it will evaluate as true when above 30 and below 60. This is what you want.

You just have to figure out what to trigger your automation on. You could test every minute and use the numeric test as a condition for the automation.

Hi Tom,
I’m looking for a trigger that will fire when a numeric value goes out of a specified range. For example, if I set the range as 40-60, then the change from 41 to 39, or the from 59 to 60 will trigger the automation once. That is to say, I want the automation to be hysteresis.

However I cannot do this with multiple triggers as there’s no way to get the previous numeric state. Like I know the current value is 61, but don’t know if the previous value is 62 or 59.

Do you have any idea about it?

Sounds like a job for a

You can set an upper and or lower limit and apply hysteresis.

Trigger your automation on this sensor.