Automation for Turning off Fan when temperature drops

Hi all

I’ve automation to turn off the fan when the temperature drops below 24.5 degrees, only during 11 pm and 9 am in the morning. However, when the temperature drops below 24.5 at 10:30 pm as example, the automation condition has failed due to the time and then when the temperature is even 22 degrees after 11 pm , it doesn’t work. The code is below. Any help is appreciated

alias: Temp
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: xxxx
    entity_id: sensor.xxxx_temperature
    domain: sensor
    below: 24.5
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: time
    before: "09:00:00"
    after: "21:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - type: turn_off
    device_id: xxxxx
    entity_id: xxxx
    domain: switch
mode: single
1 Like

Anyone? Hello!!!

The problem you have is the timer isn’t checked every 5mins, but rather only 5 mins after reaching 24.5 just the one time.

One way to fix it is to instead do a time pattern “minutes: /5”trigger every 5 mins and then check the temp as the condition.

The following is the standard way of handling the situation. Add a Time Trigger and a Numeric State Condition.

The additional State Condition simply prevents needlessly turning off the switch if it’s already off.

alias: Temp
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.your_temperature_sensor
    below: 24.5
    for:
      minutes: 5
  - platform: time
    at: 21:00:01
condition:
  - condition: time
    before: "09:00:00"
    after: "21:00:00"
  - condition: numeric_state
    entity_id: sensor.your_temperature_sensor
    below: 24.5
  - condition: state
    entity_id: switch.your_switch
    state: 'on'
action:
  - service: switch.turn_off
    target:
      entity_id: switch.your_switch
mode: single

In this case, there’s no need to repeatedly check the temperature. Employing a Time Pattern Trigger is inefficient compared to simply checking the temperature once at the beginning of the time period.

3 Likes

Doesn’t checking the temp once mean that if the temperature drops below 24.5 after 21:00, nothing will happen? - or have I misread this?

I would have thought the “and” condition would work - temp drops below 24.5 “AND” it is after 21:00 etc. Nothing happens until the temp triggers and then it starts checking the time.

There are two triggers, a Numeric State Trigger and a Time Trigger.

In the following two scenarios, the switch is assumed to be on.

  • If the temperature decreases below 24.5 the Numeric State Trigger will trigger. If the current time is within the desired time period (21:00-09:00), all three conditions will be true and the action will be executed.

  • If the temperature decreases below 24.5 the Numeric State Trigger will trigger. If the current time is not within the desired time period, the Time Condition will be false and the action will not be executed. When the time becomes 21:00 (the start of the desired time period), the Time Trigger will trigger, all three conditions will be true (because the temperature had decreased below 24.5 prior to 21:00) and the action will be executed.

Taras

Thanks for the explanation. Once I put it into Visual Editing mode it made sense. In effect each trigger is also a condition and vice-versa - or at least that’s the way my logic flow sees it!

Hello, this is my first post here, did the revised code work? I would like to implement the script for my A/C. Oh and we have the same name :slight_smile: