Fan automation help needed

Hey guys and gals,

I am having issues trying to perfect my ceiling fan automation in the living room.

What I want is that it fires the automation when me or my wife are home and the temp is higher then 24 degrees, but when we turn it off manually (by switching the fan of through a button on the board/widget on the phone or through the remote of the fan), to pause the automation for an hour and turn of the fan when we are not home and turn it back on when we get home and the temp is 24+.

What I have now is the following, but I notice that it sometimes not works like it should (I also still have notifications to my phone in here to troubleshoot the automation which let me know when the trigger was manual or by the automation):

alias: TV Fan Automation
description: ""
trigger:
  - platform: state
    entity_id: fan.tv_fan
  - platform: state
    entity_id:
      - climate.woonkamer
    attribute: current_temperature
    enabled: true
  - platform: state
    entity_id:
      - person.fgod
    to: not_home
  - platform: state
    entity_id:
      - person.samantha
    to: not_home
  - platform: state
    entity_id:
      - person.samantha
    to: home
  - platform: state
    entity_id:
      - person.fgod
    to: home
condition:
  - condition: time
    after: "07:00:00"
    before: "23:00:00"
  - condition: or
    conditions:
      - condition: state
        entity_id: person.fgod
        state: home
      - condition: state
        entity_id: person.samantha
        state: home
      - condition: and
        conditions:
          - condition: state
            entity_id: person.fgod
            state: not_home
          - condition: state
            entity_id: person.samantha
            state: not_home
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
        sequence:
          - service: notify.mobile_app_moto_g200_5g
            data:
              message: "ParentID: None || UserID: None || ID: CONTENT"
              title: Likely Physical Switched
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
            enabled: true
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: numeric_state
                        entity_id: climate.woonkamer
                        above: 24
                        attribute: current_temperature
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: home
                          - condition: state
                            entity_id: person.samantha
                            state: home
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "off"
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "off"
                    then:
                      - type: turn_on
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
                      - service: fan.set_percentage
                        data:
                          percentage: 25
                        target:
                          entity_id: fan.tv_fan
              - conditions:
                  - condition: and
                    conditions:
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "on"
                      - condition: and
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: not_home
                          - condition: state
                            entity_id: person.samantha
                            state: not_home
                sequence:
                  - type: turn_off
                    device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                    entity_id: c2a476a49b1a8c63f897ecf704e27395
                    domain: fan
              - conditions:
                  - condition: numeric_state
                    entity_id: climate.woonkamer
                    below: 22
                    attribute: current_temperature
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "on"
                    then:
                      - type: turn_off
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
            enabled: true
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
            enabled: true
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
            enabled: true
        sequence:
          - service: notify.mobile_app_moto_g200_5g
            data:
              title: Likely Automation switched
              message: "ParentID: CONTENT || UserID: None || ID: CONTENT"
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: numeric_state
                        entity_id: climate.woonkamer
                        above: 24
                        attribute: current_temperature
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: home
                          - condition: state
                            entity_id: person.samantha
                            state: home
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "off"
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "off"
                    then:
                      - type: turn_on
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
                      - service: fan.set_percentage
                        data:
                          percentage: 25
                        target:
                          entity_id: fan.tv_fan
              - conditions:
                  - condition: and
                    conditions:
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "on"
                      - condition: and
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: not_home
                          - condition: state
                            entity_id: person.samantha
                            state: not_home
                sequence:
                  - type: turn_off
                    device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                    entity_id: c2a476a49b1a8c63f897ecf704e27395
                    domain: fan
              - conditions:
                  - condition: numeric_state
                    entity_id: climate.woonkamer
                    below: 22
                    attribute: current_temperature
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "on"
                    then:
                      - type: turn_off
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
        sequence:
          - service: notify.mobile_app_moto_g200_5g
            data:
              title: Likely UI switched
              message: "ParentID: None || UserID: CONTENT || ID: CONTENT"
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: numeric_state
                        entity_id: climate.woonkamer
                        above: 24
                        attribute: current_temperature
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: home
                          - condition: state
                            entity_id: person.samantha
                            state: home
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "off"
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "off"
                    then:
                      - type: turn_on
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
                      - service: fan.set_percentage
                        data:
                          percentage: 25
                        target:
                          entity_id: fan.tv_fan
              - conditions:
                  - condition: and
                    conditions:
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: fan.tv_fan
                            state: "on"
                      - condition: and
                        conditions:
                          - condition: state
                            entity_id: person.fgod
                            state: not_home
                          - condition: state
                            entity_id: person.samantha
                            state: not_home
                sequence:
                  - type: turn_off
                    device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                    entity_id: c2a476a49b1a8c63f897ecf704e27395
                    domain: fan
              - conditions:
                  - condition: numeric_state
                    entity_id: climate.woonkamer
                    below: 22
                    attribute: current_temperature
                sequence:
                  - if:
                      - condition: state
                        entity_id: fan.tv_fan
                        state: "on"
                    then:
                      - type: turn_off
                        device_id: 9ec08cb2d0a4c1b644f060ef30ee8fff
                        entity_id: fan.tv_fan
                        domain: fan
    default: []
mode: single

ultimately it would also be very nice if I can let it set the fan speed to higher values if the nest thermostat is exceeding for example 26 degrees.

Anyway, tips and help are very welcome on how to improve this automation to work as intended.

I’m thinking you may be better with 2 automations to simplify this (others may disagree and have a better method).

Have an input_boolean helper to track your fan override (ie turn off for 1 hour if manually turned off by remote or HA dashboard). Something like this:

alias: Fan 1 Hour Override
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.fan_override
    to: "on"
    for:
      hours: 1
      minutes: 0
      seconds: 0
    id: override_expired
  - platform: state
    entity_id:
      - fan.tv_fan
    to: "off"
    id: fan_off
  - platform: state
    entity_id:
      - fan.tv_fan
    to: "on"
    id: fan_on_manual
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - fan_off
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.fan_override
      - conditions:
          - condition: trigger
            id:
              - fan_on_manual
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.fan_override
      - conditions:
          - condition: trigger
            id:
              - override_expired
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.fan_override
mode: single

Notes on this:

  1. This basicaly will track if your fan is turned off by remote or dashboard and set the override boolean on. Then if it has been on for an hour, turn it off.
  2. You only need the context_parent_id == none in your condition as if triggered by automation this is not none, if triggered by physical or dashboard it is none.
  3. You can adjust your override time in this automation.
  4. You can also cancel the override if you turn the fan back on.

Then your fan automation can be something like this (I think much simpler)

alias: Fan Example
description: ""
trigger:
  - platform: numeric_state
    entity_id: zone.home
    above: 0
    id: arrive_home
  - platform: state
    entity_id:
      - zone.home
    to: "0"
    id: leave_home
  - platform: state
    entity_id:
      - climate.wiser_lounge
    id: temp_change
  - platform: state
    entity_id:
      - input_boolean.fan_override
    id: override_change
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - leave_home
              - condition: numeric_state
                entity_id: climate.woonkamer
                below: 22
                attribute: current_temperature
              - condition: state
                entity_id: input_boolean.fan_override
                state: "on"
        sequence:
          - service: fan.turn_off
            data: {}
            target:
              entity_id: fan.tv_fan
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: climate.woonkamer
                above: 24
                attribute: current_temperature
              - condition: numeric_state
                entity_id: zone.home
                above: 0
              - condition: state
                entity_id: input_boolean.fan_override
                state: "off"
        sequence:
          - service: fan.turn_on
            target:
              entity_id: fan.tv_fan
            data:
              percentage: 25
mode: single

Notes on this:

  1. If you want to change fan speed on diff temp levels, you can add this as a template in the turn_on percentage. Something like:
percentage: "{{ min(((state_attr('climate.woonkamer', 'current_temperature') | int(0) - 23) * 25), 100) }}"

Not fully tested but should give you a direction to go. Hope it helps.

damn, there are so many things in there that I was unaware off! like the fact that you use a number for a zone instead of the state of a person, I was completely unaware that this was possible and makes it so much easier then tracking all devices states!

I am going to try this out for the coming days and disable mine and see what happens :slight_smile: Thx a lot @msp1974!

And like you said, maybe somebody else has better ideas on this, who knows, more ideas are always welcome, but I am all in for simpler ways to deal with this as this was getting pretty big and I was kinda losing my insight in the whole thing :sweat_smile:

Quick question about this though… The way you setup the conditions with 2 options to choose from, looking at the second part when it needs to turn on the fan, if I would include the fan speed part, wouldn’t this be ignored due to the condition check for if the fan is turned off? I mean, once the fan is on at 24 degrees, it would just skip the fan speed setup as it fails on the conditions because the fan is already on right?

I initially did the fan off/on check due to the frustrating beep sound the fan made when a command was send, but I removed the buzzer a few weeks back and replaced it with a led (as it was part of the active circuit which broke the fan if I just removed the buzzer alone :sweat_smile: ) so the check was not really needed anymore as the beep was not present anymore, or at least not to get as less beeps as possible :stuck_out_tongue:

No. Its not checking if the fan is off, only if the fan override is off. As such it will send commands to the fan on every temp change (0.1C). It may need some tweaking to see if it has increased by 1C before it sends another on command.

oh right, totally misread that part XD but yeah there must be ways to let it check by 0,5 degrees or 1 degree changes instead of every 0,1 degree, but other then that, so far it seems to do what it needs to do from what I am testing now with the dev tools :slight_smile:

Maybe try something like this.

Adds another chooser that will run action if automation triggered by temp change with a condition that temp has to be more than 1C different. Also adds trigger condition to last chooser so that it only runs if changes to zone count and override if temp hasnt changed when these happen. I think the logic is right.

description: "Fan Automation"
mode: single
trigger:
  - platform: numeric_state
    entity_id: zone.home
    above: 0
    id: arrive_home
  - platform: state
    entity_id:
      - zone.home
    to: 0
    id: leave_home
  - platform: state
    entity_id:
      - climate.wiser_lounge
    id: temp_change
  - platform: state
    entity_id:
      - input_boolean.fan_override
    id: override_change
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - leave_home
              - condition: numeric_state
                entity_id: climate.woonkamer
                below: 22
                attribute: current_temperature
              - condition: state
                entity_id: input_boolean.fan_override
                state: "on"
        sequence:
          - service: fan.turn_off
            data: {}
            target:
              entity_id: fan.tv_fan
      - conditions:
          - condition: trigger
            id:
              - temp
          - condition: numeric_state
            entity_id: zone.home
            above: 0
          - condition: numeric_state
            entity_id: climate.woonkamer
            above: 24
          - condition: state
            entity_id: input_boolean.fan_override
            state: "off"
          - condition: template
            value_template: >-
              {{ trigger.from_state.attributes.current_temperature | int(0) !=
              trigger.to_state.attributes.current_temperature | int(0) }}
        sequence:
          - service: fan.turn_on
            target:
              entity_id: fan.tv_fan
            data:
              percentage: >-
                {{ min(((state_attr('climate.woonkamer', 'current_temperature')
                | int(0) - 23) * 25), 100) }}
      - conditions:
          - condition: trigger
            id:
              - arrive_home
              - override_change
          - condition: numeric_state
            entity_id: climate.woonkamer
            above: 24
            attribute: current_temperature
          - condition: numeric_state
            entity_id: zone.home
            above: 0
          - condition: state
            entity_id: input_boolean.fan_override
            state: "off"
        sequence:
          - service: fan.turn_on
            target:
              entity_id: fan.tv_fan
            data:
              percentage: 25
1 Like

this does indeed make more sense to split the home/override state with and the temp state :slight_smile:

Edit: I spotted 2 errors in your code though (apart from the initial climate change which needs to be climate.woonkamer for me :wink: ) Just pointing it out for when somebody is looking for a similar thing and copies this over :slight_smile:

The change to for the zone.home part needs to be in between "" as you did with the first try and the condition trigger was stating temp instead of temp_change

1 Like