Automation to alert specific humidity sensor, when corresponding room is too humid

Hi HA-Users,

I have a lot of H&T sensors around the house and would like to be alerted, if one room gets has a too high humidity. I would love to achieve this with just one automation and not an automation for every H&T sensor.

Right now I can create a trigger which is triggered, when any of the given entities reports a value too high.

alias: RegexFeuchtigkeit
description: Versuch mit Templates die Feuchtigkeit überall gleichzeitig zu überwachen
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.bad_humidity
      - sensor.govee_h_and_t_kinderzimmer_humidity
      - sensor.govee_h_and_t_wohnzimmer_humidity
    for:
      hours: 1
      minutes: 0
      seconds: 0
    above: 70
condition: []
action:
  - service: notify.email_mailbox
    data:
      title: Ein ist zu feucht
      message: Ein ist zu feucht
mode: single

Unfortunately I’m not able at this point to refer to the entity id that triggered the automation in the email.

Do you have any idea?

Thank you for your help.

PS: What i found so far, which is somehow similar is this: Automation to alert specific open windows when it is raining? - #13 by SandmanXX
But unfortunately the solution doesn’t work for non binary sensors I’m concerned…

The way I do this with only one Automation is by setting them all as triggers. Then in the actions use the if then action. Here’s a sample.

trigger:
  - platform: state
    entity_id:
      - input_door_8
      - input_door_9
      - input_door_10
      - input_door_11
      - input_door_12
    from: "on"
    to: "off"
condition: []
action:
  - if:
      - condition: state
        entity_id: input_door_8
        state: "off"
    then:
      - service: input_boolean.turn_on
        target:
          entity_id: input_door_8
        data: {}
  - if:
      - condition: state
        entity_id: input_door_9
        state: "off"

What this does is any of the triggers with start the automation, but only the device that send the trigger will get the action. This works for me perfectly 100% of the time.

I sure someone smarter may have a more effecient way of doing it but this works for me,.

1 Like

Also on each if action you can create a message for the particular sensor so you know when one it is triggered.

You can also do this with templates.

Can you explain what you mean by this? There is no restriction on templating sensors… and you can group numerical sensors like humidity sensors using a Group Helper which allows the state of the helper entity to reflect one of a number of data points such as Maximum, Minimum, Mean, etc.

You will need to tell us how you set those up and the specific goals of the automation for us to help you with that version.

There are ways to combine and structure template sensors and automation triggers to use the groups, but it seems to often be the case what little you gain in code maintenance by having a dynamic source, you lose in ease of understanding and possible points of failure.

alias: RegexFeuchtigkeit
description: Versuch mit Templates die Feuchtigkeit überall gleichzeitig zu überwachen
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.bad_humidity
      - sensor.govee_h_and_t_kinderzimmer_humidity
      - sensor.govee_h_and_t_wohnzimmer_humidity
    for:
      hours: 1
      minutes: 0
      seconds: 0
    above: 70
condition: []
action:
  - service: notify.email_mailbox
    data:
      title: Ein ist zu feucht
      message: "{{ area_name(trigger.entity_id) }} ist zu feucht"
mode: single
1 Like

Nice,
I will try this tomorrow.

Oh,
you’re right - I can group the sensors with the helpers.
I was definitely not up-to-date and will look into this. Hopefully tomorrow.

Thanks for your fast response!
Samuel

Okay.
tried this today as this seems more elegant. But I’m not quite there yet.

I fear, that the functionality lies in the old way of using groups, but I’m not sure about that.

  • I created a group humidity-sensors which has always the max humidity value of all sensors.
  • I tried to adjust the automation from the mentioned threat to notify me about the room with too high humidity, but failed.
    • I don’t know how to prompt the group sensor via selectattr do I want to get the state?
    • I don’t know how to get all sensors above the theshold, it seems like there is no above or geq in selectattr.

Do you have ideas?

My Automation looks like this at the moment:

alias: RegexFeuchtigkeit
description: >-
  Versuch mit groups die Feuchtigkeit in allen Wohnräumen mit einer
  Automatisierung zu überwachen.
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.humidity_sensors
    for:
      hours: 1
      minutes: 0
      seconds: 0
    above: 70
condition: []
action:
  - service: notify.email_mailbox
    data_template:
      title: Es sind Zimmer zu feucht.
      message: >
        {% set HT = expand('sensor.humidity_sensors') | selectattr('state', 'eq', 'on') | map(attribute='name') | list %} 
        Die Zimmer {{HT | join(', ')}} sind zu feucht.

There is nothing special about the old groups… The problem is that you are trying to use a template designed for binary sensors with boolean states on a group of sensors with numerical states. The humidity sensors in your group will never have the state value “on”, so using the filter selectattr('state', 'eq', 'on') removed them all from the list.

Jinja has a number of built-in Tests and Home Assistant also enables many custom tests. The available tests for “greater than” is gt or >. For “greater than or equal to” you use ge or >=. However, states are always strings and using mathematical comparison tests on strings doesn’t always return the results we would expect when comparing numerical values.

To extract a list of the humidity sensors above a certain value requires a multi-step approach.

  1. Extract the state values
  2. Convert them to a floating point numbers
  3. Select based on your desired threshold
  4. Convert them back to strings
  5. Select the entities whose states match the remaining values.
...
action:
  - service: notify.email_mailbox
    data_template:
      title: Es sind Zimmer zu feucht.
      message: >
        {%- set group = 'sensor.humidity_sensors' %}
        {%- set st_objs = expand(state_attr(group, 'entity_id') | select('has_value')) %}
        {%- set humid = st_objs | map(attribute='state') | map('float') 
        | select('ge', 70) | map('string') | list %}
        {%- set HT = st_objs | selectattr('state', 'in', humid) 
        | map(attribute='entity_id') | map('area_name') | join(', ') %}
        Die Zimmer {{ HT }} sind zu feucht.
1 Like

Wow, thank you all so much for your help.
It now seems to work - even though it was way more complicated than I initially thought.

For completeness I share the whole automation here:

alias: WohnraumFeuchtigkeit
description: >-
  Versuch mit groups die Feuchtigkeit in allen Wohnräumen mit einer
  Automatisierung zu überwachen.
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.humidity_sensors
    for:
      hours: 1
      minutes: 0
      seconds: 0
    above: 70
condition: []
action:
  - service: notify.email_mailbox
    data_template:
      title: Es sind Zimmer zu feucht.
      message: >
        {%- set group = 'sensor.humidity_sensors' %}
        {%- set st_objs = expand(state_attr(group, 'entity_id') | select('has_value')) %}
        {%- set humid = st_objs | map(attribute='state') | map('float') 
        | select('ge', 70) | map('string') | list %}
        {%- set HT = st_objs | selectattr('state', 'in', humid) 
        | map(attribute='entity_id') | map('area_name') | join(', ') %}
        Die Zimmer {{ HT }} sind zu feucht.

Thanks again for your kind help!

Ah, one little problem persists.

If the humidty a sensor reports is -for example - exactly 74 then the variable humid has is as 74.0.
In that situation selectattr('state', 'in', humid) doesn’t find the value, because it’s looking for 74.0 but there is only 74 - I guess…

Yes, that can be a problem. If all of your sensors report their value as integers in those cases, then you can map a replace function in the definition for the variable humid.

{% set humid = st_objs | map(attribute='state') | map('float') | select('gt', 70) 
| map('string') | map('regex_replace', '(\.0)$', '') | list %}

If some of your sensors report 74 and some report 74.0 it gets more complicated.

1 Like

Okay this seems to work! Thanks again for thinking this through with me!