Multiple Trigger/Condition Automation advice needed

I have an automation which is working … adequately, but I’m wondering if there’s a better way of doing things.

I want to turn a fan (an IKEA TRETAKT socket) in my office on when:

  1. my PC is on (using Ping)
  2. the temperature from my Sonoff sensor has been over 20.5 for 5 minutes
  3. the derivative of the temperature is over 0.1C/h
  4. and I haven’t turned the fan off in the past 15 minutes

I figured I needed the automation to trigger on any one of these “When”, but then also check the same conditions “And if”.

So far so good. My YAML looks like this.

alias: Office Fan On
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.ewelink_snzb_02p_temperature
    above: 20.5
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - trigger: numeric_state
    entity_id:
      - sensor.derivative_sensor_temperature
    above: 0.1
  - trigger: state
    entity_id:
      - switch.ik_06
    to:
      - "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - trigger: state
    entity_id:
      - binary_sensor.192_168_1_60
    to:
      - "on"
conditions:
  - condition: numeric_state
    entity_id: sensor.ewelink_snzb_02p_temperature
    above: 20.5
  - condition: numeric_state
    entity_id: sensor.derivative_sensor_temperature
    above: 0.1
  - condition: device
    type: is_off
    device_id: 0031dc58ea25143fe7e6a2f05ae7c288
    entity_id: ff8f9fcc6f911c34e49f457f1bb5ed00
    domain: switch
    for:
      hours: 0
      minutes: 15
      seconds: 0
    alias: Fan in Office (IK-06) is off for 15:00
  - condition: state
    entity_id: binary_sensor.192_168_1_60
    state:
      - "on"
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.ik_06
mode: single

The one thing I can’t seem to recreate from the triggers is to check that the temperature has been over 20.5 for a period of time (5 minutes in this instance).

Is there a way to achieve this? Can I simplify the triggers and/or conditions such that there’s a minimum of duplication?

Thanks in advance!

Not directly. You can set up a Threshold or Template binary sensor, then use it’s state in your conditions.

1 Like

Ah, the Threshold sensor! That’s exactly what I need. Thanks for the suggestion.