Switch and sensor based Humidifier Platform

Hi. I have added a Sonoff th16 switch to a dehumidifier and I’ve been searching for a platform to use a Humidifier Entity but no one found so I’ve create one based on a switch and a humidity sensor. Also to be able to use with homekit.

Download from GitHub

It’s very simple and generic. I hope it can be useful to somebody.

Installation

  • Copy all files in custom_components/switch_humidifier to your config/custom_components/switch_humidifier/ directory.
  • Restart Home-Assistant.
  • Add the configuration to your configuration.yaml file.

Usage

To use this component in your installation, add the following to your configuration.yaml file:

Example configuration.yaml entry

humidifier: 
  - platform: switch_humidifier
    name: Switch Deshumidifier
    switch_id: switch.deshumidifier
    sensor_id: sensor.humidity
    type: dehumidifier

Parameters

  • name (Optional): The platform name
  • switch_id (Required): The switch entintity id
  • sensor_id (Required): The humidity sensor entintity
  • type (Optional): Posible values are humidifier or dehumidifier
    • Default: dehumidifier

It also support homekit.

Homekit

To use with homekit you need to especify the humidity sensor as linked_humidity_sensor in the entity_config

homekit:
- filter:
    include_entities:
      - humidifier.switch_humidifier
      - sensor.humidity
  entity_config:
    humidifier.switch_humidifier:
      linked_humidity_sensor: sensor.humidity
4 Likes

This is great, just what I’ve been looking for. Thank you for your hard work.

@adrianBv it looks like home assistant doesn’t like the “linked_humidity_sensor:” portion of the HomeKit setup. Do you know of another way of linking the humid sensor to the switch for HomeKit?

Thank you!

Thank you for making this platform, it’s exactly what I needed! I have 5 humidifier entities defined in my configuration.yaml. I’m running into 2 issues though.

First, whenever I restart HA, the set humidity value for the entities gets reset. It seems to consistently be set to either 50 or 70. I’m not really sure where that number is coming from, but it certainly causes problems in the areas these devices are located in.

In my Home Assistant Core log I am getting this error logged

Failed to call service humidifier/set_humidity: 'SwitchHumidifier' object has no attribute '_persistence_json'

And the log entry

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 199, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1808, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1845, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 213, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 686, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 961, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 726, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/humidifier/__init__.py", line 206, in async_set_humidity
    await self.hass.async_add_executor_job(self.set_humidity, humidity)
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/switch_humidifier/humidifier.py", line 190, in set_humidity
    self.save_target()
  File "/config/custom_components/switch_humidifier/humidifier.py", line 135, in save_target
    self._persistence_json['target'] = self._target_humidity
AttributeError: 'SwitchHumidifier' object has no attribute '_persistence_json'

The second thing that seems to be happening is that after a restart the switches won’t activate unless I manually turn the entities to on. This isn’t that big of a deal since I have an automation to set everything to on when HA starts but I wonder if it would be helpful to have a “default state” attribute to assign to the entity.

I can’t find anything in any of my searches indicating anyone else out there is having this same problem with the humidity value getting reset on restart but neither can i find anything anywhere in any of my config that would be causing this to happen. Is there any way to fix this?

I switched over to the generic hygrostat integration. It works significantly more reliably, no issues with any of my restarts, scripts, or automations.