Run automation when humidity changes by x%

I am trying to integrate a humidity sensor with an external API. I have a script (as in a Home Assistant ScriptTM - not Bash or Perl) using a template that updates an API as the humidity changes. What I want to do now is create an automation that only runs the script when the humidity changes by 5% to keep the sampling under control. An example:

If the current humidity is 45% and raises to 47% or lowers to 41% - the automation should not run. That first sampled value of 45% is essentially a bench mark. So for the next 10000 years if the humidity never deviates more that +/- 5% from the first value taken, don’t run the automation. As soon as the humidity rises to 53%, the automation should run and 53% is the new bench mark and the automation won’t run so long as the humidity doesn’t change by +/- 5%. Then the humidity jumps to 87% - that’s the new bench mark etc and so forth for the rest of time.

Is there a way to do this?

I think the best way is to set an input_number to the current benchmark.

then in the automation trigger use a template to test that the current humidity is +/- 5% from the input_number.

then if the automation runs then set the input_number to the new current humidity number as an action of the automation

What about a trend sensor? Trigger the update when the trend sensor is above or below a certain value for a given time.

Ah, excellent idea. I created an input_number and a template conditional:

{{ ((states('sensor.govee_master_shower_ht_sensor_humidity')| round ) -
      (states('input_number.master_bath_humidity_latest_bench_mark')| int ) ) | abs >= 5}}

And the automation has

action:
  - service: script.update_master_bath_humidity_value
    data: {}
  - service: input_number.set_value
    data_template:
      entity_id: input_number.master_bath_humidity_latest_bench_mark
      value: "{{ states('sensor.govee_master_shower_ht_sensor_humidity')| round }}"

I think this may just do it. Thank you!

Edit: It turns out my original code had abs in the template goofed up. Updated the template code to be correct.

2 Likes

Previous post on how I’m doing this: Quick Bathroom Fan Humidity Switch Control

1 Like

This gives me some fantastic ideas for lux sensor use to prevent flapping/create hysteresis at threshold points.

And here is more text as apparently I need to make this different enough as I screws up an edit.