renemarc/home-assistant-config

View on GitHub
automations/notifications/humidity_notify.yaml

Summary

Maintainability
Test Coverage
#
# Check if humidity is too high or too low
#
# Comfort level should be between 35% and 55%.
# Mold grows above 55% relative humidity, and thrives above 70%.
# Only verify if doors and windows are closed.
#
# @subscribe group.door_sensors
# @subscribe input_boolean.windows_open
# @subscribe sensor.average_humidity
#
- id: humidity_notify
  alias: "Humidity NOTIFY"

  trigger:
    # When humidity is way too high.
    - platform: numeric_state
      entity_id: sensor.average_humidity
      above: 65
      for:
        seconds: 10

    # When humidity is getting too high.
    - platform: numeric_state
      entity_id: sensor.average_humidity
      above: 55
      for:
        seconds: 10

    # When humidity is getting too low.
    - platform: numeric_state
      entity_id: sensor.average_humidity
      below: 35
      for:
        seconds: 10

  condition:
    # If automation was not trigered lately (hey, do not nag).
    - condition: template
      value_template: >-
        {% set delay = 30 * 60 %}
        {% set current = as_timestamp(utcnow()) %}
        {% set last = as_timestamp(state_attr('automation.humidity_notify', 'last_triggered')) | float %}

        {{ current - last > delay }}

    # If doors have been closed for a while.
    - condition: state
      entity_id: group.door_sensors
      state: 'off'
      for:
        minutes: 60

    # If windows have been closed for a while.
    - condition: state
      entity_id: input_boolean.windows_open
      state: 'off'
      for:
        minutes: 60

  action:
    # Pin a warning message in the frontend.
    - service: persistent_notification.create
      data:
        notification_id: humidity
      data_template:
        title: >-
          {% set humidity = states('sensor.average_humidity') | int %}
          {% if (humidity > 65) %}
            Indoor humidity is WAY too high
          {% elif (humidity >= 55) %}
            Indoor humidity is too high
          {% else %}
            Indoor humidity is too low
          {% endif %}
        message: >-
          {% set humidity = states('sensor.average_humidity') | int %}
          {% if (humidity > 65) %}
            {{ states('sensor.average_humidity') }}% = Risk of mold!<br>
            <strong>Reduce the humidity NOW!<strong>
          {% elif (humidity >= 55) %}
            {{ states('sensor.average_humidity') }}% is excessive.<br>
            <strong>Try to reduce the humidity.<strong>
          {% else %}
            {{ states('sensor.average_humidity') }}% is too dry.<br>
            <strong>Check the humidifier.</strong>
          {% endif %}

    # Warn someone by text to take action.
    - service: notify.text
      data_template:
        title: >-
          {% set humidity = states('sensor.average_humidity') | int %}
          {% if (humidity > 65) %}
            Indoor humidity is WAY too high
          {% elif (humidity >= 55) %}
            Indoor humidity is too high
          {% else %}
            Indoor humidity is too low
          {% endif %}
        message: |
          {% set humidity = states('sensor.average_humidity') | int %}
          {% if (humidity > 65) %}
          {{ states('sensor.average_humidity') }}% = Risk of mold!
          Reduce the humidity NOW!
          {% elif (humidity >= 55) %}
          {{ states('sensor.average_humidity') }}% is excessive.
          Try to reduce the humidity.
          {% else %}
          {{ states('sensor.average_humidity') }}% is too dry.
          Check the humidifier.
          {% endif %}

    # Play a warning sound and message.
    - condition: state
      entity_id: input_boolean.quiet_mode
      state: 'off'
    - service: notify.sound
      data:
        message: 'alarm'
        data:
          method: 'alarm'
    - delay:
        seconds: 2
    - service: notify.speech
      data_template:
        message: >-
          {% set humidity = states('sensor.average_humidity') | int %}
          {% if (humidity > 65) %}
            Indoor humidity is way too high.
            Currently at {{ states('sensor.average_humidity') }}%.
            There is a risk of mold!
            Do reduce the humidity NOW!
          {% elif (humidity >= 55) %}
            Indoor humidity is too high.
            Currently at {{ states('sensor.average_humidity') }}% which is excessive.
            Try to reduce the humidity.
          {% else %}
            Indoor humidity is too low.
            Currently at {{ states('sensor.average_humidity') }}% which is too dry.
            Check the humidifier.
          {% endif %}