Fan automation based on temperature

I have a fan component for a ceiling fan via broadlink. Works well and have set up an automation based on a temperature above 26c, triggered by temperature, presence and time. I was hoping to add more smarts to it, so above 26c, it starts on Low, above 28c set to Medium. Etc.

Would I need different automations for this or is there a way to do it in one automation?

You could use a data_template (i think that’s the right one) to have all of these in one automation…I think. I’m not good at templates and all of mine have been created with the help of others on here. I’ll try and dig something up, otherwise I’m keen to see what you hear back with from others.

EDIT: actually I think maybe service_template. I may be able to create an example for you

Ok. Will look into service templates. I’m also not very good with templates. I’ll try and find an example to use.

this WILL be rough, like I said, templates aren’t my thing… thinking about it again…maybe a data_template like this… but I am guessing at the syntax right now

automation:
  - alias: Fan Control
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: sensor.your_temp_sensor

    action:
      - service: switch.turn_on
        data_template: >
          {% if trigger.above '26' %}
            switch.your_low_speed_switch
          {% elif trigger.above '28' %}
            switch.your_medium_speed_switch
          {% endif %}

I’m using the fan set speed action, which needs a data part. I’m not sure how I would use this with a data_tempate? This is the current action code.

    action:
      service: fan.set_speed
      entity_id: fan.lounge_fan
      data:
        speed: low

at a guess…

automation:
  - alias: Fan Control
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: sensor.your_temp_sensor

    action:
      - service: fan.set_speed
        data_template: >
          {% if trigger.above '26' %}
            speed.low
          {% elif trigger.above '28' %}
            speed.medium
          {% endif %}

something along those lines. I’m not familiar with the set_speed service

Looking at the below in the docs I’m wondering if this may be better…

{% if states('sensor.temperature') | float > 20 %}
automation:
  - alias: Fan Control
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: sensor.your_temp_sensor

    action:
      - service: fan.set_speed
        data_template: >
          {% if states('sensor.your_temp_sensor') | float > 26 %}
            speed.low
          {% if states('sensor.your_temp_sensor') | float > 28 %}
            speed.medium
          {% endif %}

I think the syntax is out a bit. Keep getting errors with this as the action.
Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None.

    action:
      - service: fan.set_speed
        entity_id: fan.luke_fan
        data_template: >
          {% if states('sensor.wirelesstag_luke_temperature') | float > 26 %}
            speed: low
          {% if states('sensor.wirelesstag_luke_temperature') | float > 28 %}
            speed: medium
          {% endif %}

yeah, I’m not sure how to help further.

Also, you can add in more lines to give something such as:

action:
  - service: fan.set_speed
    entity_id: fan.luke_fan
    data_template: >
      {% if states('sensor.wirelesstag_luke_temperature') | float > 26 %}
        speed: low
      {% if states('sensor.wirelesstag_luke_temperature') | float > 28 %}
        speed: medium
      {% if states('sensor.wirelesstag_luke_temperature') | float > 30 %}
        speed: high
      {% endif %}

but maybe get it working first!

Thanks for the help, with have a play around.
I want to try and add a slow the fan automation if temp drops. But, as you say, I need to get this working first.

I think the trigger I gave you also needs work. There are some killer templating guru’s on here that I’m sure will tell you that my examples are rubbish and will give you something far better. :rofl: We just need them online!

Could you post your existing automation? That would be helpful, especially since the documentation for fan components is somewhat lacking.

But basically, when using a service where you want a data item to be calculated on the fly, you use data_template instead of data, then use a template for whichever data options need to be calculated, along the lines that @sparkydave has suggested.

By posting your existing automation it will be more clear what entities are involved, how you’re triggering, etc., so useful suggestions can be made.

This is my existing automation. Thanks

  - id: lounge_fan_on
    alias: Lounge Fan On
    trigger:
      - platform: numeric_state
        entity_id: sensor.wirelesstag_lounge_temperature
        above: 26
      - platform: state
        entity_id: group.family_presence
        to: 'on'
      - platform: time
        at: '06:30:00'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:00:00'
          before: '20:00:00'
        - condition: numeric_state
          entity_id: sensor.wirelesstag_lounge_temperature
          above: 26
        - condition: state
          entity_id: fan.lounge_fan
          state: 'off'
        - condition: state
          entity_id: group.family_presence
          state: 'on'
    action:
      service: fan.set_speed
      entity_id: fan.lounge_fan
      data:
        speed: low
1 Like

Ok, my first stab at it would be:

  - id: lounge_fan_on
    alias: Lounge Fan On
    trigger:
      - platform: state
        entity_id: sensor.wirelesstag_lounge_temperature
      - platform: state
        entity_id: group.family_presence
        to: 'on'
      - platform: time
        at: '06:30:00'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:00:00'
          before: '20:00:00'
        - condition: numeric_state
          entity_id: sensor.wirelesstag_lounge_temperature
          above: 26
        - condition: state
          entity_id: group.family_presence
          state: 'on'
    action:
      service: fan.set_speed
      entity_id: fan.lounge_fan
      data_template:
        speed: >
          {% set temp = states('sensor.wirelesstag_lounge_temperature')|float %}
          {% if temp < 28 %} low
          {% elif temp < 30 %} medium
          {% else %} high
          {% endif %}

The idea is whenever someone comes home after everyone being away, or at 6:30 am, or whenever the temperature changes, as long as it is between 6:00 am and 8:00 pm, and someone is home, and the temperature is above 26, then the fan will be turned on, and the speed will depend on the current temperature. Obviously change the thresholds and the speed values if I didn’t get that part right.

4 Likes

That works well. It’s currently 28.7 and triggering the automation sets the fan to medium.

Good first stab!

Now, would it require another if/else to get the speed to lower as the temperature lowers? Or would that require a different trigger?

you already have the code there to set low speed once the temp drops below 28

as I said @rowdybeans , there are others on here, a.k.a. @pnbruckner that are GURU’s with templates! :open_mouth: If only I could be half as good at them… Glad you got it going.

Is it possible to enter a condition if the temperature is below 24, the fan turns off? thank you

I would just do that in another automation, with a numeric_state trigger with below: 24.

I use the following. It turns it off if below 26, we leave or at 11pm.

  - id: lounge_fan_off
    alias: Lounge Fan Off
    trigger:
      - platform: numeric_state
        entity_id: sensor.wirelesstag_lounge_temperature
        below: 26
      - platform: state
        entity_id: group.family_presence
        to: 'off'
      - platform: time
        at: '23:00:00'
    condition:
      - condition: state
        entity_id: fan.lounge_fan
        state: 'on'
    action:
      service: fan.turn_off
      entity_id: fan.lounge_fan

could this automation be correct?

- id: 'Ventilatore_fan_on'
  alias: Ventilatore_Fan On
  trigger:
    - platform: state
      entity_id: sensor.netatmo_cucina_temperature
    - platform: zone
      entity_id: device_tracker.iphone_matteo
      zone: zone.home
    - platform: time
      at: '09:00:00'
    - platform: season
      event: spring
    - platform: season
      event: summer
     condition:
    condition: and
    conditions:
      - condition: time
        after: '09:00:00'
        before: '21:00:00'
      - condition: numeric_state
        entity_id: sensor.netatmo_cucina_temperature
        above: 21
      - condition: zone
        entity_id: device_tracker.iphone_matteo
        zone: zone.home
  action:
    - service: fan.set_speed
      entity_id: fan.smartmi_fan
      data_template:
        speed: >
          {% set temp = states('sensor.netatmo_cucina_temperature')|float %}
          {% if temp > 24 %} Level 1
          {% elif temp > 26 %} Level 2
          {% elif temp > 28 %} Level 3
          {% else %} Level 4
          {% endif %}
    - service: notify.ios_iphone_matteo
      data:
        title: "Smart Home Alerts"
        message: 'Ventilatore ON - temperatura {{ states("sensor.netatmo_cucina_temperature") }} C'
    - data:
        effect: 'WhatsApp'
        entity_id: light.cucina_3
      service: light.turn_on
- id: 'Ventilatore_fan_off'
  alias: Ventilatore Fan Off
  trigger:
  - platform: numeric_state
    entity_id: sensor.netatmo_cucina_temperature
    below: 23
  - platform: state
    entity_id: device_tracker.iphone_matteo
    from: 'home'
    to: 'not_home'
  - platform: time
    at: '23:00'
  condition:
  - condition: state
    entity_id: fan.smartmi_fan
    state: 'on'
  action:
  - service: fan.turn_off
    entity_id: fan.smartmi_fan
  - service: notify.ios_iphone_matteo
    data:
      title: "Smart Home Alerts"
      message: 'Ventilatore OFF'