Overnight fan control

So I got the idea of automating my ceiling fan to turn on when:

  • Immediately if the bedtime scene is called and temp is above a threshold
  • 10 minutes If the temp climbs above a threshold for X minute. This is to prevent power cycling if the thermostat (ecobee via homekit) jumps for just a minute or two.

What I came up with is:

  - alias: 'Fan Control On'
    initial_state: false
    trigger:
      - platform: numeric_state  
        entity_id: climate.bedroom_local
        value_template: "{{ state.attributes.current_temperature }}"
        above: 68                              # Don't forget to change this in the condition below as well
        for:                                   # Duplicated so we can condition with a for: clause 
          hours: 0 
          minutes: 10 
          seconds: 0 
      - platform: event
        event_type: call_service
        event_data:
          service_data:
            entity_id: [ scene.get_ready_for_bed]
            domain: scene
    condition:
      condition: numeric_state
      entity_id: climate.bedroom_local
      above: 68                                # A second time for when the scene vent is called
      value_template: "{{ state.attributes.current_temperature }}"
    action:
      service: fan.set_speed  
      entity_id: fan.bedroom_fan
      data:
        speed: low

This only partially works. If I comment out the condition it will work when the scene is called. So I suspect my problems are with the condition to check the temperature, most likely something to do with value_template however I’m stumped as to what syntax is off here.

Curious if anyone else looking at this sees a potential issue or has an idea why this condition is causing it to fail.

Try this:

value_template: "{{ state.attributes.current_temperature | int }}"

Interesting idea, but didn’t seem to make a difference.

Triple checking the entity and I do see the attribute is there with a value.

Try only specifying minutes:

        for:                                   # Duplicated so we can condition with a for: clause 
          minutes: 10 

Only specifying minutes didn’t work either.

If I comment out condition the service call trigger works, so the common denominator seems to be:

      - platform: numeric_state  
        entity_id: climate.bedroom_local
        value_template: "{{ state.attributes.current_temperature | int }}"
        above: 68                              # Don't forget to change this in the condition below as well

platform/condition obviously depending on where it’s used.

This is equivalent:

   condition:
     condition: template
     value_template: "{{ state_attr('climate.bedroom_local', 'current_temperature') | int > 68 }}"

See if that works any better (or not).

NOTE
The screenshot above indicates current_temperature is 67 so that’s too low to satisfy the condition. For testing purposes, don’t overlook to reduce the value in the template.

Yes I’m aware the temperature changed since I uploaded the initial snippet. I opened the window in the middle of this all :slight_smile: .

So you were right… using the template version seems to work!

Thanks for the help @123, @tom_l.

In hopes that this thread could help someone else, here’s roughly what I ended up with:

scene:
  - name: "Get Ready For Bed"
    icon: "mdi:Bed"
    entities:
      automation.fan_control_on:
        state: on
      automation.fan_control_off:
        state: on 

  - name: "Good Morning"
    icon: "mdi:sun"
    entities:
      automation.fan_control_on:
        state: off
      automation.fan_control_off:
        state: off
      fan.bedroom_fan:
        state: off

automation:
  # Turn on as it gets warm
  - alias: 'Fan Control On'
    initial_state: false
    trigger:
      - platform: template
        value_template: "{{ state_attr('climate.bedroom_local', 'current_temperature') | int > 68 }}"
        for:
          minutes: 0
      - platform: event
        event_type: call_service
        event_data:
          service_data:
            entity_id: [ scene.get_ready_for_bed]
            domain: scene
    condition:
      condition: template
      value_template: "{{ state_attr('climate.bedroom_local', 'current_temperature') | int > 68 }}"
    action:
      service: fan.set_speed
      entity_id: fan.bedroom_fan
      data:
        speed: low
    
  # Turn off if it cools down
  - alias: 'Fan Control Off'
    initial_state: false
    trigger:
      platform: template
      value_template: "{{ state_attr('climate.bedroom_local', 'current_temperature') | int < 67 }}"
      for:
        hours: 0
        minutes: 10
        seconds: 0
    action:
      service: fan.set_speed
      entity_id: fan.bedroom_fan
      data:
        speed: off
      
  # If the fan is manually turned off, don't touch it (trust the human).
  - alias: 'Fan Control Override'
    initial_state: true
    trigger:
      platform: state
      entity_id: fan.bedroom_fan                                       
      from: "on"
      to: "off"
    action:
      - service: automation.turn_off
        entity_id: automation.fan_control_off
      - service: automation.turn_off
        entity_id: automation.fan_control_on

Brief explanation:

The scene (get ready for bed) turns on these automations. If the temperature is above the threshold when the scene is set or at some point overnight the fan will turn on. If it drops the fan will turn off.

If the fan is manually turned off, the automations will be disabled until the get ready for bed scene is re-run. The good morning automation will disable the whole thing.

Glad to hear it’s working.

If the template I suggested resolved your original question, please mark my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title and signal to others that this topic has an accepted solution. In addition, a link will appear below your first post that leads to the solution. All of this helps others find answers to similar questions.

1 Like

Hi racettura,
Where do you place this code. I am a beginner and trying to learn from your successes!