Turning humidifier on or off based on BLE humidity sensor percentage

I have an indoor grow tent with an exhaust fan and Levoit Dual 200S humidifier (using FormatBCE’s code) to manage the environment for my vegetables and fruit trees during the cold months. Now that I have the coding framework in place, I’m looking to automate the humidifier.

There are a few inter-dependencies here, and I want to make sure I have the correct code so the readings aren’t competing with one another. Essentially, I want to run the humidifier if the humidity reading falls below 50% (read from a BLE device), then turn it off once the reading reaches 50%; but only if the exhaust fan is not running.

The exhaust fan is going to draw humidity out, naturally, so I took that into consideration by checking to see if the fan is running in a condition. Below is my code. Will this work from a code perspective?

alias: Grow Tent Humidifier Trigger
trigger:
  platform: numeric_state
  entity_id: sensor.ble_humidity_e33280c3688a
  below: 50
condition:
  condition: state
  entity_id: switch.grow_tent_fan
  state: "off"
action:
  service: homeassistant.turn_{{ 'on' if numeric_state == '49' else 'off' }}
  entity_id: switch.grow_tent_humidifier

I came back to this, looking to refine the code. As far as I can tell, the new code should turn the humidifier on or off if the humidity reading falls below or rises above the threshold. I checked it against the template editor and it came back clean. However, it still doesn’t run as expected. Any ideas on where the snag might be?

- id: humidifier_auto
  alias: Grow Tent Humidifier Trigger
  trigger:
    platform: numeric_state
    entity_id: sensor.h5179_688a_humidity
    below: 50
  condition:
    condition: state
    entity_id: switch.grow_tent_fan
    state: "off"
  action:
    - service: >
        {% if states('sensor.h5179_688a_humidity') | float < 50 %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: humidifier.grow_tent_humidifier

There’s no “switch.[entity]” entity ID for the humidifier, but I do have an entity card for “humidifier.grow_tent_humidifier” that does toggle the humidifier on and off manually. So I’ve confirmed “humidifier.grow_tent_humidifier” does toggle properly.

I suggest you use the GUI to add the rule, and compare the result to what you have in your post, and go from there.

But based on rules I have created via the GUI and my minmal knowledge of HA rules, you’ll need one rule to turn the humidity on, and one to turn it off.

I’m not sure how to limit the trigger to only run if the humidity is not in its desired state, my best guess is below (use your condition).

Your current rule as setup can only turn the humidity on, and then I don’t know what that if in your action is doing.

If not using the GUI, I’d try this to turn it on once humidity is below 50%, with no condition and then verify it’s working:

- id: humidifier_auto
  alias: Grow Tent Humidifier Trigger
  trigger:
    platform: numeric_state
    entity_id: sensor.h5179_688a_humidity
    below: 50
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    target:
      # You can use an entity_id or device_id here.
      entity_id: humidifier.grow_tent_humidifier

And then if that works, try it with a condition - I suspect this is not correct:

- id: humidifier_auto
  alias: Grow Tent Humidifier Trigger
  trigger:
    platform: numeric_state
    entity_id: sensor.h5179_688a_humidity
    below: 50
  condition: []
  - condition: state
    entity_id: switch.grow_tent_fan
    state: "off"
  action:
  - service: switch.turn_on
    data: {}
    target:
      # You can use an entity_id or device_id here.
      entity_id: humidifier.grow_tent_humidifier

And then add a similar rule for a trigger above 60% or so to turn it off.

Edit:

Oh, it looks like you might need to turn both the humidifier and fan on - I don’t know if / how to add multiple actions in yaml: I suggest trying one entitity at a time, and using the GUI.

It’s a bit late in the game, but couldn’t you use the generic hygrostat integration?

Myself, I have a smallish cool mist humidifier, a Wilfa HU-35W, in the bedroom, hooked up to a Meross MSS315 smart plug (Matter-compatible!). The humidity is measured by a cheapie Xiaomi LYWSD03MMC (reprogrammed to use Zigbee, but no matter) on the bedside table. It just works.

I used the example configuration with only minor changes. The humidifier is kept on when humidity is below target – 30% in my case – and switched off for at least a minute till humidity drops below target again.

I hope this helps.