Trying to control fan speed by temp

This is a relatively complex automation for a first stab at HA. I would recommend starting with something much more simple until you’ve got that working, then step up to something like this.

Where are you putting the yaml code?

I am putting it in a blank automation - I’m starting with an empty automation, then switching over to “edit in YAML” and pasting it there. Is that correct?

OK, I think that might be your problem. I don’t typically use the GUI for creating automations. I write the YAML to an automations.yaml file, which is then included in my config.

I think the issue is when you click Edit in YAML, you are only editing the section you clicked on (trigger, condition, etc.). But then you’re pasting in the yaml for the entire automation.

You could try pasting in each individual section from the yaml I provided into the correct section in the GUI. Or you could open your configuration.yaml file and paste it under the automation: heading. If you don’t have automation: in there, you can add it to the bottom.

Re-reading your original post… Now that I realize that this is all brand new and you are using the GUI, it might be easier to just create a separate automation for each scenario in the GUI. Your first one would be something like:

Trigger: numeric state of sensor above 75
Condition: numeric state of sensor below 79
Action: fan on low

And the next would be something like:

Trigger: numeric state of sensor above 78
Condition: numeric state of sensor below 82
Action: fan on medium

Etc…

Since they would be separate automations, you just want to use the correct trigger temperature and conditions to make sure they are mutually exclusive so they aren’t tripping over each other. That’s about the best I can do for you. As I said, I don’t use the GUI. But you should be able to accomplish your goal in it. Or you could go the yaml route like I mentioned in my last post. Good luck to you!

AH! That did it…that was my problem - doing it in the automations tab doesn’t work.

I have read a bit about YAML, and I totally understand how your script works…it was just that everything I was trying was throwing errors…and all because I was putting it in the wrong place.

I have modified your script with my entities and will test it tonight.

Thank you so much for your help and patience…it was a huge help and I just learned a ton.

Have a great night!!!

-David

1 Like

Hey, we all started somewhere! I still ask for help in the forum all the time. It’s a great resource!

Let me know if you get all of this to work!

Yup! I am looking forward to getting a bit more proficient with all this.

I tested it by heating the temp sensor, but the fan never turned on. I am going to go through and make sure I didn’t mess something up.

Hmmmm…well, it won’t fire on its own. When I hit RUN ACTIONS in Automations, it works, but won’t run on its own. In this case, I’m sure its something I am not doing correctly, but is there anything else I need to do to get an automation to run on its own? It is turned on in automations, but not sure if I am forgetting something else.

Thank you!!

# Molly Fan On/Off
- id: molly_fan_on_off
  alias: 'Molly Fan On/Off'
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.molly_temp
  condition:
    condition: and
    conditions:
      - condition: time # Weekdays
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
          - sun
      - condition: time
        after: '18:00:00'  # Fires between 6:00 pm - 6:00 am
      - condition: time
        before: '06:00:00'  # Fires between 6:00 pm - 6:00 am

  action:
    - choose:
      - conditions:
          - condition: numeric_state
            entity_id: fan.molly_s_bedroom_ceiling_fan
            value_template: "{{ states('sensor.molly_temp') | float }}"
            above: 76  # Temperature in office is above 76
        sequence:
          - service: fan.set_percentage
            entity_id: fan.molly_s_bedroom_ceiling_fan
            data:
              percentage: >
                {% if states('sensor.molly_temp') | float >= 80 %} 100
                {% elif states('sensor.molly_temp') | float > 79 %} 75
                {% elif states('sensor.molly_temp') | float > 78 %} 50
                {% else %} 25
                {% endif %}

      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            value_template: "{{ states('sensor.molly_temp') | float }}"
            below: 75  # Temperature in office is below 75
        sequence:
          - service: fan.turn_off
            entity_id: fan.molly_s_bedroom_ceiling_fan

Combine the 2 conditions as

      - condition: time
        after: '18:00:00'  
        before: '06:00:00' 

See the note in Conditions - Home Assistant (home-assistant.io)

Note that if only before key is used, the condition will be true from midnight until the specified time. If only after key is used, the condition will be true from the specified time until midnight. Time condition windows can span across the midnight threshold if both after and before keys are used.

So, effectively, the conditions as you wrote them are mutually exclusive.

Adding a little to the previous person’s observation, you can probably omit the weekday condition since you have every day of the week listed.

Thanks, guys. I made those changes and changed the parameters to test this morning, but it still won’t fire unless I run it manually. Everything seems right, but just not sure what is preventing it from firing automatically.

# Molly Fan On/Off
- id: molly_fan_on_off
  alias: 'Molly Fan On/Off'
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.molly_temp
  condition:
    condition: and
    conditions:
      #- condition: time # Weekdays
        #weekday:
          #- mon
          #- tue
          #- wed
          #- thu
          #- fri
          #- sat
          #- sun
      - condition: time
        after: '18:00:00'  # Fires between 6:00 pm - 6:00 am
        before: '06:00:00'  # Fires between 6:00 pm - 6:00 am

  action:
    - choose:
      - conditions:
          - condition: numeric_state
            entity_id: fan.molly_s_bedroom_ceiling_fan
            value_template: "{{ states('sensor.molly_temp') | float }}"
            above: 76  # Temperature in office is above 76
        sequence:
          - service: fan.set_percentage
            entity_id: fan.molly_s_bedroom_ceiling_fan
            data:
              percentage: >
                {% if states('sensor.molly_temp') | float >= 80 %} 100
                {% elif states('sensor.molly_temp') | float > 79 %} 75
                {% elif states('sensor.molly_temp') | float > 78 %} 50
                {% else %} 25
                {% endif %}

      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            value_template: "{{ states('sensor.molly_temp') | float }}"
            below: 75  # Temperature in office is below 75
        sequence:
          - service: fan.turn_off
            entity_id: fan.molly_s_bedroom_ceiling_fan

That means the automation will trigger for each temperature change and only on temperature change.
Did you take that into account?

Also, both your conditions (above/below) can be simplified:

into

          - condition: numeric_state
            entity_id: sensor.molly_temp
            above: 76  # Temperature in office is above 76

Ah, ok. So the simplified condition will take care of the numeric state, but is there something else I should be using as the trigger? Basically, I want it to trigger any time the temp gets above a set temperature and then adjusts the fan speed as the temp increases. Finally, the fan stops when the temp gets below a low temp.

Thank you!

No, what you do should work.
Just keep in mind for your tests that the automation will only be triggered when the state of sensor.molly_temp changes.

OK, I will try heating the sensor and seeing if it triggers (I’ll change the time condition)

This is what I have right now after making the change you suggested:

# Molly Fan On/Off
- id: molly_fan_on_off
  alias: 'Molly Fan On/Off'
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.molly_temp
  condition:
    condition: and
    conditions:
      #- condition: time # Weekdays
        #weekday:
          #- mon
          #- tue
          #- wed
          #- thu
          #- fri
          #- sat
          #- sun
      - condition: time
        after: '18:00:00'  # Fires between 6:00 pm - 6:00 am
        before: '06:00:00'  # Fires between 6:00 pm - 6:00 am

  action:
    - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            above: 76  # Temperature in office is above 76
        sequence:
          - service: fan.set_percentage
            entity_id: fan.molly_s_bedroom_ceiling_fan
            data:
              percentage: >
                {% if states('sensor.molly_temp') | float >= 80 %} 100
                {% elif states('sensor.molly_temp') | float > 79 %} 75
                {% elif states('sensor.molly_temp') | float > 78 %} 50
                {% else %} 25
                {% endif %}

      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            value_template: "{{ states('sensor.molly_temp') | float }}"
            below: 75  # Temperature in office is below 75
        sequence:
          - service: fan.turn_off
            entity_id: fan.molly_s_bedroom_ceiling_fan

You can simplify that one, too

Ah, ok. Does that look better? And can I comment out the time condition like this for testing purposes?

# Molly Fan On/Off
- id: molly_fan_on_off
  alias: 'Molly Fan On/Off'
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.molly_temp
  condition:
    condition: and
    conditions:
      #- condition: time # Weekdays
        #weekday:
          #- mon
          #- tue
          #- wed
          #- thu
          #- fri
          #- sat
          #- sun
      #- condition: time
        #after: '18:00:00'  # Fires between 6:00 pm - 6:00 am
        #before: '06:00:00'  # Fires between 6:00 pm - 6:00 am

  action:
    - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            above: 76  # Temperature in office is above 76
        sequence:
          - service: fan.set_percentage
            entity_id: fan.molly_s_bedroom_ceiling_fan
            data:
              percentage: >
                {% if states('sensor.molly_temp') | float >= 80 %} 100
                {% elif states('sensor.molly_temp') | float > 79 %} 75
                {% elif states('sensor.molly_temp') | float > 78 %} 50
                {% else %} 25
                {% endif %}

      - conditions:
          - condition: numeric_state
            entity_id: sensor.molly_temp
            below: 75  # Temperature in office is below 75
        sequence:
          - service: fan.turn_off
            entity_id: fan.molly_s_bedroom_ceiling_fan

Comment out everything, or you’ll get syntax errors

OK! Did a test and at trigger temp, the fan turned to low and the fan speed increased at each temp condition, so I think this one is working!! I am going to test it again and if all looks good, then I will put the time condition back in. Here is where I ended up:

- id: molly_fan_on_off
  alias: Molly Fan On/Off
  initial_state: true
  trigger:
  - platform: state
    entity_id: sensor.molly_temp
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.molly_temp
        above: 76
      sequence:
      - service: fan.set_percentage
        entity_id: fan.molly_s_bedroom_ceiling_fan
        data:
          percentage: '{% if states(''sensor.molly_temp'') | float >= 80 %} 100 {%
            elif states(''sensor.molly_temp'') | float > 79 %} 75 {% elif states(''sensor.molly_temp'')
            | float > 78 %} 50 {% else %} 25 {% endif %}

            '
    - conditions:
      - condition: numeric_state
        entity_id: sensor.molly_temp
        below: 75
      sequence:
      - service: fan.turn_off
        entity_id: fan.molly_s_bedroom_ceiling_fan

I did see that my commented out time conditions are completely gone. Does the editor remove a section if the entire thing is commented out?

Thanks,
David

1 Like

Glad you pointed that out, @koying! I’m not sure how I ended up using a value template when I could have just gone straight to the state.