Turn on fan if humidity high, then turn off when it goes down

I have created two automations: 1 to turn the fan on , then another to turn it off
However the fan has also a manual switch, so I want to be able to override anything with this manual switch
Currently, if i manually turn the switch on, and it the humidiy is low, it wil turn the fan off a few seconds later
What I want is to add a condition to turnfanoff, so only takes effect if the fan is on due to the turnfanon fan trigger
My current automations.yaml file looks like this:
- id: β€˜1549675365377’
alias: turnfanon
trigger:
- above: β€˜70’
entity_id: sensor.humidity
platform: numeric_state
condition: []
action:
- data: {}
entity_id: switch.bathroom_fan
service: switch.turn_on
- id: β€˜1550147571186’
alias: turnfanoff
trigger:
- below: β€˜60’
entity_id: sensor.humidity
platform: numeric_state
condition: []
action:
- data: {}
entity_id: switch.bathroom_fan
service: switch.turn_off

Thanks

Please read the blue banner at the top of the page.

done
what now?

Does that actually look like your code?

If so, the indentation is all wrong.

Just copied and pasted ( again):
- id: β€˜1549675365377’
alias: turnfanon
trigger:
- above: β€˜70’
entity_id: sensor.humidity
platform: numeric_state
condition: []
action:
- data: {}
entity_id: switch.bathroom_fan
service: switch.turn_on
- id: β€˜1550147571186’
alias: turnfanoff
trigger:
- below: β€˜60’
entity_id: sensor.humidity
platform: numeric_state
condition: []
action:
- data: {}
entity_id: switch.bathroom_fan
service: switch.turn_off

Why does this matter?
As it stands, it IS working - albeit not how I like it, so the formatting must be OK
Thanks

YAML is whitespace sensitive. The way your code currently looks, as posted, means it will never work. I’m sure in your file the white space is different, but nobody can tell what it is unless posted correctly.

As for the question, One way I’ve done it (though it’s probably not the best way) is that when my automation turns on a fan, it also sets a input_boolean true indicating that it was turned on via an automation, and my auto-off automation checks the condition of this bool before automatically turning it off. If I manually turned on the fan, that input_bool will be false, so the auto-off automation won’t execute.

silicon_avatar
Thanks for your input
I’m not sure what happened with the formating, as I said I just copied and pasted the text
Can you expand on your solution, maybe with some bits of coding as examples?
I have not used the input_boolean before

Thanks

Copy and paste your code in, then highlight all of the code, then press the </> symbol. Or put a β€œ```” before and a β€œ```” after your code. That will also format it correctly in the forum.

Sure.
So in my use case, I have two lights in my bedroom. One main light near the door, and a nightstand light.
When I control the main light, I always want the nightstand light to follow the main light.
I have an automation that turns on the nightstand light when the main light turn on.
I have another automation that turns off the nightstand light when the main light turns off.

I also have a button at the nightstand, that i want to control both lights independently.

My issue was, similar to yours in a sense, that if I turned off the main light from my nightstand button, my other automation took over and also turned off the nightstand light.

My solution was to have an input_boolean that if it is true will not fire the automation to turn off the nightstand with the bed light.
Then the automation that reacts to my nightstand button holds that input_boolean true for 2 seconds to stop the other automation from working.

I’ve since converted this all to appdaemon, but this is the code I found on my github that worked before the move.

//In configuration.yaml
input_boolean:
   bedroom_light_trigged_by_button:
     name: Disable turning off bedroom nightstand light by button
     initial: off

//In automations.yaml
- id: toggle_bedroom_main_light
  initial_state: true
  alias: Toggle Main Bedroom Light With Double Click
  hide_entity: true
  trigger:
    - platform: state
      entity_id: sensor.xiaomi_bedroom_button_1
  condition:
    condition: state
    entity_id: sensor.xiaomi_bedroom_button_1
    state: 'double'
  action:
    - service: switch.toggle
      data: { "entity_id": "switch.bedroom_main_light_power_switch" }
    - service: input_boolean.turn_on
      entity_id: input_boolean.bedroom_light_trigged_by_button
    - delay: '00:00:02'
    - service: input_boolean.turn_off
      entity_id: input_boolean.bedroom_light_trigged_by_button

- id: turn_off_nightstand_light_with_main_light
  initial_state: true
  alias: Turn Off Nightstand Light With Main Room Light Off
  trigger:
    platform: state
    entity_id: switch.bedroom_main_light_power_switch
    to: 'off'
  condition:  
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.bedroom_light_trigged_by_button
        state: 'off'
  action:
    service: light.turn_off
    data: { "entity_id": "light.bedroom_nightstand_light" }

Thanks for your insight
I think I have managed, see below:

- id: '1549675365377'
  alias: turnfanon
  trigger:
  - above: '70'
    entity_id: sensor.humidity
    platform: numeric_state
  condition: []
  action:
    - service: switch.turn_on
      data: { "entity_id": "switch.bathroom_fan" }
    - service: input_boolean.turn_on
      entity_id: input_boolean.fan_triggered
- id: '1550147571186'
  alias: turnfanoff
  trigger:
  - below: '60'
    entity_id: sensor.humidity
    platform: numeric_state
  condition:
      condition: state
      entity_id: input_boolean.fan_triggered
      state: 'on'
  action:
    - service: switch.turn_off
      data: { "entity_id": "switch.bathroom_fan" }
    - service: input_boolean.turn_off
      entity_id: input_boolean.fan_triggered

I hope this time formatting is OK

Thanks again

Good formatting!
Yes, that looks good from a first glance.
The automation to turn the fan off will only trigger if the fan is turned on via the other automation.

Based on the above statement, you do NOT want the Automation to turn OFF the switch if it was turned on MANUALLY?

Yep, that’s it
I think I have achieved that with the above automation

Thanks

1 Like

For some reason my automation doesn’t work anymore
I had to rebuild the HA system, as somehow it was not booting, so rebuilt it from scratch, then restored from snapshot
Unfortunately the rebuild used the last version ( 88.1) - originally was 87.0
Now both fan on and fan off Automations are never triggered: the actions are OK, though
There are no issues when I check the configuration. I have reduced the values, to see if they are triggered, but nothing happens

I’m at a loss, so any help to figure out what’s going on will be greatly appreciated
see my automations.yaml file:

- id: '1549675365377'

alias: turnfanon
trigger:

  • above: β€˜40’
    entity_id: sensor.bathroom_humidity
    platform: numeric_state
    condition: []
    action:
  • data:
    entity_id: switch.bathroom_fan
    service: switch.turn_on
  • entity_id: input_boolean.fan_triggered
    service: input_boolean.turn_on
  • id: β€˜1550147571186’
    alias: turnfanoff
    trigger:
    • below: β€˜30’
      entity_id: sensor.bathroom_humidity
      platform: numeric_state
      condition:
    • condition: state
      entity_id: input_boolean.fan_triggered
      state: β€˜on’
      action:
    • data:
      entity_id: switch.bathroom_fan
      service: switch.turn_off
    • entity_id: input_boolean.fan_triggered
      service: input_boolean.turn_off

Thanks again

Correct formatting:

  • id: β€˜1549675365377’
    alias: turnfanon
    trigger:
    • above: β€˜40’
      entity_id: sensor.bathroom_humidity
      platform: numeric_state
      condition: []
      action:
    • data:
      entity_id: switch.bathroom_fan
      service: switch.turn_on
    • entity_id: input_boolean.fan_triggered
      service: input_boolean.turn_on
    • id: β€˜1550147571186’
      alias: turnfanoff
      trigger:
      • below: β€˜30’
        entity_id: sensor.bathroom_humidity
        platform: numeric_state
        condition:
      • condition: state
        entity_id: input_boolean.fan_triggered
        state: β€˜on’
        action:
      • data:
        entity_id: switch.bathroom_fan
        service: switch.turn_off
      • entity_id: input_boolean.fan_triggered
        service: input_boolean.turn_off

nope. You need to use the </> button after you select the text

I did, but for some reason didnt work!
Lets try again:

  • id: β€˜1549675365377’
    alias: turnfanon
    trigger:
    • above: β€˜70’
      entity_id: sensor.bathroom_humidity
      platform: numeric_state
      condition: []
      action:
    • data:
      entity_id: switch.bathroom_fan
      service: switch.turn_on
    • entity_id: input_boolean.fan_triggered
      service: input_boolean.turn_on
  • id: β€˜1550147571186’
    alias: turnfanoff
    trigger:
    • below: β€˜65’
      entity_id: sensor.bathroom_humidity
      platform: numeric_state
      condition:
    • condition: state
      entity_id: input_boolean.fan_triggered
      state: β€˜on’
      action:
    • data:
      entity_id: switch.bathroom_fan
      service: switch.turn_off
    • entity_id: input_boolean.fan_triggered
      service: input_boolean.turn_off

Just doednt want to play ball:
- id: β€˜1549675365377’
alias: turnfanon
trigger:

  • above: β€˜70’
    entity_id: sensor.bathroom_humidity
    platform: numeric_state
    condition: []
    action:
  • data:
    entity_id: switch.bathroom_fan
    service: switch.turn_on
  • entity_id: input_boolean.fan_triggered
    service: input_boolean.turn_on
  • id: β€˜1550147571186’
    alias: turnfanoff
    trigger:
    • below: β€˜65’
      entity_id: sensor.bathroom_humidity
      platform: numeric_state
      condition:
    • condition: state
      entity_id: input_boolean.fan_triggered
      state: β€˜on’
      action:
    • data:
      entity_id: switch.bathroom_fan
      service: switch.turn_off
    • entity_id: input_boolean.fan_triggered
      service: input_boolean.turn_off

I give up!

You need to click the button again while its highlighted. Sometimes it gets messed up but you can see the preview of how it will look to the right ----->

- id: '1549675365377'
  alias: turnfanon
  trigger:
  - above: '70'
    entity_id: sensor.bathroom_humidity
    platform: numeric_state
  condition: []
  action:
  - data:
      entity_id: switch.bathroom_fan
    service: switch.turn_on
  - entity_id: input_boolean.fan_triggered
    service: input_boolean.turn_on
- id: '1550147571186'
  alias: turnfanoff
  trigger:
  - below: '65'
    entity_id: sensor.bathroom_humidity
    platform: numeric_state
  condition:
  - condition: state
    entity_id: input_boolean.fan_triggered
    state: 'on'
  action:
  - data:
      entity_id: switch.bathroom_fan
    service: switch.turn_off
  - entity_id: input_boolean.fan_triggered
    service: input_boolean.turn_off