Spotty performance with an automation that uses a template as a trigger

Have a look at this solution and see if it can be adapted for your needs. The requirement was to keep the humidity within a given range.