Create sensor or binary sensor update every 5 minutes

Hi All,

I want create a automation based on humidity sensor (bathroom sensor)
The humidity is changing all the time when its warm outside, rain, winter etc…

I have now this automation:

###########################################################################################
# CONTROL BATHROOM FAN WHEN HUM IS ABOVE 68
###########################################################################################
- id: "23762a09-2251-4de3-9670-68ce66645882"
  alias: "Control - Hum Bathroom above 68"
  trigger:
    - platform: numeric_state
      entity_id: sensor.bathroom_humidity
      above: 68

  action:
    - service: fan.set_preset_mode
      data:
        preset_mode: high
      target:
        entity_id: fan.afzuiging_badkamer

###########################################################################################
# CONTROL BATHROOM FAN WHEN HUM IS BELOW 65
###########################################################################################
- id: "23762a09-2251-4de3-9670-68ce66645382"
  alias: "Control - Hum Bathroom below 65"
  trigger:
    - platform: numeric_state
      entity_id: sensor.bathroom_humidity
      below: 65

  action:
    - service: fan.set_preset_mode
      data:
        preset_mode: low
      target:
        entity_id: fan.afzuiging_badkamer

The problem is when its warm outside the humidity is lower and then it takes more time before the value is above 68 or below 65.

I want create something that when start point humidity it for example 40 that the above value well be 45 and below will become 43

When normal start point humidity is 64 (because of weather change) then the above will be 68 and below 65

I hope someone can understand what I want… but how :slight_smile:

You could try using a template sensor, and if the difference between the outdoor and bathroom humidity is over/under (say) 5, then trigger the action. Something like this:

trigger:
  - platform: template
    value_template: >-
      {% set h1 = states('sensor.melbourne_humidity') | int %}
      {% set h2 = states('sensor.temp_1_hum') | int %}
      {% if h1 - h2 > 5 %}true{% endif %}

I don’t understand how updating every 5 minutes is going to address the issue you have .

It seems to me that the problem is that you need a reference point…
Do you have a second humidity sensor outside the bathroom or one from a weather integration?

With one of those you can create a template sensor that gives you an ordinal value for the bathroom humidity as it relates to your reference.

template:
  - sensor:
      - name: "Bathroom Humidity Ordinal"
        state: >
          {% set ref = states('sensor.bathroom_humidity_1hr_mean') | float(0) %}
          {% set low = ( ref * (11/12) + 19/3 ) | round(1, 0) %}
          {% set high = ( ref * (23/24) + 20/3 ) | round(1, 0) %}
          {% set humid = states("sensor.bathroom_humidity") | float(0) %}
          {% if humid > high %} Above
          {% elif humid < low %} Below
          {% else %} Between 
          {% endif %}
        availability: >
          {% set ref = states('sensor.bathroom_humidity_1hr_mean') | float('unavailable')  %}
          {% set humid = states("sensor.bathroom_humidity") | float('unavailable') %}
          {{ ref is number and humid is number }}  
- id: "a_new_unique_id"
  alias: "Control - Hum Bathroom"
  trigger:
    - platform: state
      entity_id: sensor.bathroom_humidity_ordinal
      to:
        - Above
        - Below
      for: "00:00:30"
  condition: []
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: '{{ trigger.to_state.state == ''Above''}}'
          sequence:
           - service: fan.set_preset_mode
             data:
               preset_mode: high
             target:
               entity_id: fan.afzuiging_badkamer
        - conditions:
            - condition: template
              value_template: '{{ trigger.to_state.state == ''Below''}}'
          sequence:
           - service: fan.set_preset_mode
             data:
               preset_mode: low
             target:
               entity_id: fan.afzuiging_badkamer
  mode: single

EDIT Corrected variable definitions. Modified template sensor to reference Statistics sensor proposed below.

Hi,

I don’t have a second sensor. Below you see the history of the sensor in my bathroom:

As you can see sometimes its around 58, 50, less then that, sometimes higher
The sensor is updating when there is a difference of 0,5

So my idea was create a second sensor that kept this value and update every 2 minutes
Then a automation that calculate the current sensor with the second new one.

If difference is more then 5 then basically someone is taking a shower.

new sensor / sensor

58 / 59 = false
58 / 65 = true
50 / 45 = false (but will used for turning off fan that if sensor is lower then new sensor = true)

Its not easy for me to explain in English …

If the 2 minutes update is done then the difference is less, but as you can see on image. When someone is taking shower the humidity is going fast up (updates les then 1 minute)

If you don’t have a second physical sensor and using a weather integration isn’t appropriate for your situation, why not take advantage of some of Home Assistant’s built-in statistics and utility sensors?

For example, you could use a Statistics sensor set to capture the mean humidity over the last hour and then use that as the reference value for the template sensor and automation I posted above.

sensor:
  - platform: statistics
    name: "Bathroom humidity 1hr mean"
    entity_id: sensor.bathroom_humidity
    state_characteristic: mean
    max_age:
      hours: 1
    sampling_size: 60

You could use a shorter max_age, but if you make it too short, I think you will likely have the fan speed bouncing back and forth between low and high.

thanks,

Lets me see if this is what I looking for :slight_smile:

how about this:

When actual hum sensor is higher then the statistics sensor + 5

- id: "a_new_unique_id"
  alias: "Control - Hum Bathroom"
  trigger:
    platform:  template
    value_template: "{{ states('sensor.bathroom_humidity') | float() + 5 > states('sensor.bathroom_humidity_change_over_5_minutes') | float() }}"

and when fan is on then this one to turn it off:

- id: "a_new_unique_id"
  alias: "Control - Hum Bathroom 2"
  trigger:
    platform:  template
    value_template: "{{ states('sensor.bathroom_humidity') | float() < states('sensor.bathroom_humidity_change_over_5_minutes') | float() }}"

But I guess that I have to change the statistics max_age maybe to minutes: 2?
Im not knowable with platform: statistics

You really need a second sensor, because you need a baseline for your problem and as you state yourself the one sensor you have is not a good baseline.

Trying to base it on one sensor will create problems like the sensor only increase 4.9 steps in a timeslot and your setup only goes of at 5, but then when the sensor increase 0.1, so does your baseline, so you will never have it trigger then.
Trying to lower the value makes it trigger all to often when it is not wanted.

1 Like

yes. I will try things and see what happen.
im not that into coding. When I figure it out then its in my memory and gives me more options also into other automations.

I have a bathroom humidity sensor and I do the following:

  1. I create an averaged sensor that gives me the average over the last 12 hours. This handles seasonal and (most) weather related humidity changes just fine.
  2. I then create a binary sensor that triggers to true if the current humidity is >10% above the average.

This works really well for me.

- platform: filter
  name: "Bathroom Humidity Average"
  entity_id: sensor.bathroom_humidity
  filters:
    - filter: time_simple_moving_average
      window_size: "12:00:00"

- sensor:
  - name: "Bathroom Shower Active"
    unique_id: bathroom_shower_active
    # Default to 100 humidity on the average if unknown to avoid triggering if data is missing
    state: >-
      {{ (states('sensor.bathroom_humidity')|float(0)-states('sensor.bathroom_humidity_average')|float(100)) > 10.0 }}
    icon: >-
      {% if (states('sensor.bathroom_humidity')|float(0)-states('sensor.bathroom_humidity_average')|float(100)) > 10.0 %}
        mdi:circle-off-outline
      {% else %}
        mdi:weather-sunny
      {% endif %}

1 Like

I think you need to decide what overall method you want to use.

Your first 2 posts made it seem like it was important that you have dynamic thresholds. Now you are just looking for a difference of 5…

You have not supplied the configuration for sensor.bathroom_humidity_change_over_5_minutes, so it is difficult to guess how your trigger will behave. If you are using the sample from the Statistics sensor docs page, this will not work for a few reasons:

  1. That example is using the change statistic. Using change, your automation above will trigger whenever the current humidity + 5 is above the 5 minute difference. So if the humidity doesn’t change for 5 min the equation will resolve to: {{ humidity + 5 > 0 }} which is true. Every time your humidity is stable for 5 minutes, the fan will turn on high.

  2. float filters require defaults. In your template all of the float() should be float(0)

I would not use that short an interval. The shorter the interval, the more likely it is that the fan speed will bounce between low/high/off because the current humidity has an out-sized effect on the baseline/reference.

David has posted a working example above where he uses a 12 hour average.

1 Like

Here the 2 sensors. The purple one is actual sensor and the blue one is the statistics one.
From 47 it goes to above 60. The statistics one also go very fast to new value.

I feel stupid but can’t see the logic this will work then…

No one can accurately help you if you do not provide the information we ask for…

We don’t know how you have configured the statistics sensor.

Sorry, I didn’t read good the posts…
This is now what I have

- platform: statistics
  name: "Humidity Statistics"
  entity_id: sensor.bathroom_humidity
  state_characteristic: mean
  max_age:
    hours: 1

Have you actually tried either solution that has been provided?


The graph looks as expected to me. The reason the two sensors show less separation than you might have expected is likely a combination of the max_age and the way your sensor updates its state. While a 1 hour max_age could be sufficient with a sensor that takes x samples/hour, in an earlier post you wrote:

If your humidity is relatively stable in the time period before someone takes a shower (or if your statistics sensor wasn’t given at least an hour to gather samples before the test) there will be a small number of samples contributing to the mean. When someone starts the shower the humidity changes rapidly, creating a lot of sample values. This skews the mean value of the statistics sensor towards the higher values. This is why a longer max_age might be needed for your application. Also, make sure you set a reasonable sampling_size, the default is 20. If your sensor updates every 0.5%, any change in humidity over +10%/hour will exceed your allotted samples.

Hi added the solution this evening. The action is a message only for now. To prevent the fan to go on and off all the time if there is some miscalculation or something else.

Thanks for all the help. im trying to see the logical idea behind the solution you gave me. Try to understand it also. Now its added as I say earlier.

That statistic sensor can really not be used.
You are not measuring the difference in one room compared to another room,but instead hwo fast it changes.

Implemented and the turn on part is working. The off part is to early.
My idea now is to do:

Idea 1:

  1. create a sensor that stores the sensor.bathroom_humidity value, update every 10 min
  2. use the statistics sensor to compare if the sensor.bathroom_humidity is above the statistic one.
  3. TO turn fan off check if sensor.bathroom_humidity is below the new sensor that is update every 10 min.

Idea 2:

  1. create a sensor that stores the sensor.bathroom_humidity value, update every 10 min
  2. when sensor.bathroom_humidity is above the new sensor then turn on fan
  3. directly create a sort of variable to store the new sensor value.
    (because when shower longer then 10 min the new sensor have the new value)
  4. Then to turn off the fan, compare the sensor.bathroom_humidity with the stored variable value.
    (of same or below then turn off fan)

I think idea 2 is most accurate to try but:

  1. i don’t know if i can create a sensor that update every 10 min.
  2. i can create inside a automation a stored variable that wont update until the compare has been done to turn of fan. When start automation the stored variable will get the new sensor value (start point) to compare again.

The two most used options for this problem I know are:

  1. Use a second sensor in another room to compare humidity. “Normal” vs “elevated” (already mentioned)
  2. Use a derivative or trend sensor to detect the sharp rise in humidity, rather than the actual humidity itself
    Derivative - Home Assistant
    Trend - Home Assistant

I getting more exited to create a solution. I only have 1 sensor in bathroom. So have to find a way to solve this. All ideas are good ideas and its a start :slight_smile:

Thanks to everyone to help and find a creative way to solve this