Turn on ceiling fan when above a threshold temperature

Hello

I have set up an automation that turns my ceiling fan on above 25c with a time condition. I’ve discovered that when the time condition is true the ceiling fan doesn’t start even though above 25c. It starts when the temperature changes not when the temperature is above the threshold. Could someone give me a hand, I’m pretty new and am on a steep learning curve :grinning:

I know this is basic, I initially wanted the fan to turn off when the room temperature got to the HVAC set temperature and turn back on if above HVAC set temperature but this was well above my pay grade.

        value_template: "{{ state_attr('climate.office','temperature') }}"

Below is attributes from the HVAC wall controller

hvac_modes: heat_cool, heat, dry, fan_only, cool, off
min_temp: 18
max_temp: 30
target_temp_step: 1
fan_modes: auto, speed 1, speed 2, speed 3, speed 4, speed 5, speed 6
swing_modes: off, vertical, horizontal, both
current_temperature: 27
temperature: 24
fan_mode: speed 1
swing_mode: both
icon: mdi:snowflake
friendly_name: Office
supported_features: 41

alias: Automatic Office Fan
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.hvac_office_temperature
condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
  - condition: time
    after: "07:00:00"
    before: "18:00:00"
    enabled: true
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.hvac_office_temperature
        above: 25
    then:
      - service: fan.turn_on
        data: {}
        target:
          entity_id: fan.office
    else:
      - service: fan.turn_off
        data: {}
        target:
          entity_id: fan.office
mode: single
alias: Automatic Office Fan
description: ""
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    above: 25
    for:
      seconds: 15
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    below: 25
    for:
      seconds: 15
condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
  - condition: time
    after: "07:00:00"
    before: "18:00:00"
    enabled: true
action:
  - service: "fan.turn_{{ trigger.id }}"
    target:
      entity_id: fan.office
mode: single

NOTE

Your automation is designed to allow for this scenario:

  • The temperature rises above 25 at 17:50 and turns on the fan.
  • The temperature falls below 25 at 18:05 but the fan is not turned off because the time is now outside the range of 07:00 to 18:00.

Let me know if you want to prevent that from happening.

1 Like

Yes that would be great if it’s not going to turn off at 18:00 with the time condition. I am also experiencing the opposite issue. Right now it’s 08:40 and the fan has still not turned on it’s 27c on temperature sensor. The temp was over 25c prior to 07:00.

I have three room ceiling fan automations, two with a time condition (home offices) and one without which is my living room.

Hi everyone, thanks @123 for the above script. I’m finding that if the temperature is above 25c before the time condition window opens when it gets to 07:00:00 the fan does not start? Is there a way to make that happen or is that a limitation of HA?

How about using delay and condition in action instead of “for” in the trigger?

Following @123:

alias: Automatic Office Fan
description: ""
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    above: 25
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    below: 25
condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
  - condition: time
    after: "07:00:00"
    before: "18:00:00"
    enabled: true
action:
  - delay: 15
  - condition: template
    value_template: "{{ (trigger.id=='on' and trigger.to_state.state|float>25) or (trigger.id=='off' and trigger.to_state.state|float<25) }}"
  - service: "fan.turn_{{ trigger.id }}"
    target:
      entity_id: fan.office
mode: single
1 Like

Thank you for the yaml code. I look forward to Monday morning :grinning:

Yes. Here’s the enhanced example. The additional Time Trigger triggers at 7 and checks if the temperature is above/below 25 then sets the appropriate command.

alias: Automatic Office Fan
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    above: 25
    for:
      seconds: 15
    variables:
      cmd: 'on'
  - platform: numeric_state
    entity_id: sensor.hvac_office_temperature
    below: 25
    for:
      seconds: 15
    variables:
      cmd: 'off'
  - platform: time 
    at: '07:00:01'
    variables:
      cmd: "{{ iif(states('sensor.hvac_office_temperature')|float(0) > 25, 'on', 'off') }}"
condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
  - condition: time
    after: "07:00:00"
    before: "18:00:00"
    enabled: true
action:
  - service: "fan.turn_{{ cmd }}"
    target:
      entity_id: fan.office
mode: single

EDIT 1

Correction. Added missing portion of the template.

EDIT 2

Correction. Replaced trigger.id with cmd in the service template.

1 Like

Thank you for Enhanced code. I have added this to my Monday test on second fan. Thank you so much everyone. Enjoy your weekend!!!

Message malformed: required key not provided @ data['action']

@123 are you able to point me in the right direction regarding the message HA is advising?

Sorry about that; my mistake. I have corrected the example posted above. It was missing a few important characters at the end of the template for the cmd variable.

1 Like

Cheers, that has saved without issue. I can see the closing special characters that were added. It’s good learning the construct, now I just need to research on what the value_template and cmd coding does.

Hi @123 i have received the following errors this morning. Would this be because there are no Device ID’s? Is cmd interchangable with id?

“The automation “Automatic Office Fan” (automation.automatic_office_fan ) has an action that calls an unknown service: fan.turn_2, To fix this error, edit the automation and remove the action that calls this service.”

That’s due to my mistake. After modifying the example, I forgot to replace fan.turn_{{ trigger.id }} with fan.turn_{{ cmd }}

I have corrected the example posted above.

1 Like

Thank you, I have updated your code and look forward to tomorrow mornings experiment :slight_smile:

Works perfectly now, thanks for all your help.

1 Like