Generic_thermostat for Humidity

Have you tested the value_template in Home Assistant’s Template Editor?

Paste these lines into Template Editor and observe the results.

Steckdose: {{states('input_number.schieber_steckdose1')}}

Steckdose Adjusted: {{states('input_number.schieber_steckdose1') + 2.5) | float}}

Humidity: {{states('sensor.steckdose1_am2301_humidity') | float}}

True or False: {{ (states('input_number.schieber_steckdose1') + 2.5) | float <= states('sensor.steckdose1_am2301_humidity') | float }}

For this, I get a result:

{{ states(‘input_number.schieber_steckdose1’)| float}}

Result: 54

For this part, I am getting an “Unknown error rendering template” Error:

{{(states(‘input_number.schieber_steckdose1’) + 2.5) | float}}

This was the solution:

value_template: “{{((states.input_number.schieber_steckdose1.state | float) + 2.5) <= states(‘sensor.steckdose1_am2301_humidity’) | float }}”

Now I only need to combine these two automations into one - is this possible?

  • id: steckdose1_luftfeuchtigkeit_on
    alias: “Steckdose 1 Luftfeuchtigkeit on“
    initial_state: true

    trigger:
    platform: template
    value_template: “{{((states.input_number.schieber_steckdose1.state | float) + 2.5) <= states(‘sensor.steckdose1_am2301_humidity’) | float }}”

    action:
    service: switch.turn_on
    entity_id: switch.steckdose1

  • id: steckdose1_luftfeuchtigkeit_off
    alias: “Steckdose 1 Luftfeuchtigkeit off“
    initial_state: true

    trigger:
    platform: template
    value_template: “{{((states.input_number.schieber_steckdose1.state | float) + 2.5) >= states(‘sensor.steckdose1_am2301_humidity’) | float }}”

    action:
    service: switch.turn_off
    entity_id: switch.steckdose1

Good! Glad to hear using the Template Editor helped you solve the problem.

Going forward, you should learn how to format your code for presentation on this forum. YAML indenting is critical and, in the way your code is shown, it is difficult to interpret it correctly.

Simply enter three consecutive backquotes ``` on a separate line before your code and then another set of three backquotes, on their own line, after your code. This will instruct the forum’s software to display your code in a formatted appearance (and make it easier for others to read it).

Yes, it’s possible to combine the two automations into one.

Sure, you are right, thanks for this hint!

Can you help me to put this together? I’ve tried a few different ways, but none of them worked…

Thank you for your help!

- id: steckdose1_luftfeuchtigkeit_on
  alias: “Steckdose 1 Luftfeuchtigkeit on“
  initial_state: true

  trigger:
    platform: template
    value_template: "{{states('sensor.steckdose1_am2301_humidity') | float > ((states.input_number.schieber_steckdose1.state | float) + 2.5)  }}"

  action:
    service: switch.turn_on
    entity_id: switch.steckdose1



- id: steckdose1_luftfeuchtigkeit_off
  alias: “Steckdose 1 Luftfeuchtigkeit off“
  initial_state: true

  trigger:
    platform: template
    value_template: "{{states('sensor.steckdose1_am2301_humidity') | float < ((states.input_number.schieber_steckdose1.state | float) - 2.5)  }}"

  action:
    service: switch.turn_off
    entity_id: switch.steckdose1

Try this. It’s untested but it should be approximately correct. :slight_smile:

I created two temporary variables in the value_template just to make it easier to read the last line of the template.

- id: steckdose1_luftfeuchtigkeit
  alias: 'Steckdose 1 Luftfeuchtigkeit'
  trigger:
    platform: state
    entity_id: input_number.schieber_steckdose1, sensor.steckdose1_am2301_humidity
  action:
    service_template: >-
      {% set target = (states('input_number.schieber_steckdose1') | float) + 2.5 %}
      {% set humidity = states('sensor.steckdose1_am2301_humidity') | float %}
      switch.turn_{{'on' if target <= humidity else 'off'}}
    entity_id: switch.steckdose1

Thanks, but it only works with the “tolerance” of + 2.5, but not with - 2.5 :wink:

As I wrot in my first post: The machine should e.g. turn on when the humidity is over 60%, and turn off when it has reached 50% (mean value is 55%) --> so the input_number.schieber_steckdose1 would be 55 in this example

I followed what you posted in your second example which uses +2.5 for both turning the switch on and off.

I now see that in your first example, you used +2.5 for on and -2.5 for off.

Do you want help to fix the automation or do you feel ready to handle it yourself?

BTW, I’m confused by the example you provided. Doesn’t it represent a range of +/- 5? If you set the target to 55 it turns the dehumidifier on at 60 (target+5) and turns it off at 50 (target-5). The humidity can then rise up to 60 again until the dehumidifer is turned on.

Sorry for confusing you - I was also a little confused at the beginning, but finally found the right automation which is described in this comment.

The rule behind it: If I set the target to 55%, the dehumidifer turns on when humidity gets over 57.5% and turns off when it reaches 52.5%

In OpenHAB, I was able to just put two if-conditions into one rule, which would look like somthing familiar to the following automation in HomeAssistant, but this doesn’t work:

- id: steckdose1_luftfeuchtigkeit_both
  alias: “Steckdose 1 Luftfeuchtigkeit both“
  initial_state: true

  trigger:
    platform: template
    value_template: "{{states('sensor.steckdose1_am2301_humidity') | float > ((states.input_number.schieber_steckdose1.state | float) + 2.5)  }}"

  action:
    service: switch.turn_on
    entity_id: switch.steckdose1

  trigger:
    platform: template
    value_template: "{{states('sensor.steckdose1_am2301_humidity') | float < ((states.input_number.schieber_steckdose1.state | float) - 2.5)  }}"

  action:
    service: switch.turn_off
    entity_id: switch.steckdose1

Is there a way to simply put these two automations into one?
I know you already gave me an example, but I don’t really understand that.

Thank you for your help and your time!

Here is how I understand this system works:

  • D = Desired humidity
  • LO = Low humidity threshold (D - 2.5)
  • HI = High humidity threshold (D + 2.5)
           LO    D     HI
-----------|-----|-----|------------
    OFF    |  CURRENT  |     ON
OFF>>>>>>>>>>>>>>>>>>>>|
           |<<<<<<<<<<<<<<<<<<<<<<ON

There are three operating states:

  • OFF: Ambient humidity is less than LO so the dehumidifier is set to off.
  • ON: Ambient humidity is greater than HI so the dehumidifier is set to on.
  • CURRENT: Ambient humidity is within the range set by LO and HI. The off and on states overlap within the range (it can be either on or off within this range). It depends on whether the dehumidifier is currently on and decreasing the humidity from HI to LO, or if the dehumidifier is off and allowing humidity to rise from LO to HI.

Here’s an example:

  • Desired humidity is 55 so LO=53.5 and HI=57.5
  • Ambient humidity is 57.
  • Dehumidifier is off.
  • Humidity rises to 58 and triggers the automation.
  • Humidity now exceeds HI so the dehumidifier will be set to on.
  • Humidity falls to 57 and triggers the automation. Humidity is less than HI but more than LO. The dehumidifier is set to its CURRENT state which is on (the automation is obliged to assign a value to service_template) .
  • Humidity falls to 56 and triggers the automation. It is still within range and the humidifier is set to its CURRENT state. This pattern repeats until the humidity reaches the LO threshold.
  • Humidity falls to 53 and triggers the automation. It is now less than LO so the dehumidifier is set to off.
  • Humidity rises to 54 and triggers the automation. It is now higher than LO but less then HI. The dehumidifier is set to its CURRENT state which is off. This behavior continues until humidity exceeds HI and then the dehumidifier is set to on.

The following automation implements the behavior I just described. It is triggered either by changes to the sensor or to the input_number. It computes LO and HI thresholds and then determines if the humidity is below, within, or above the range. Depending on the result, it will call the correct service.

  • If humidity < LO then call switch.turn_off
  • If humidity > HI then call switch.turn_on
  • If humidity is between LO and HI then call the service corresponding to the dehumidifier’s CURRENT state.
- id: steckdose1_luftfeuchtigkeit
  alias: 'Steckdose 1 Luftfeuchtigkeit'
  trigger:
    platform: state
    entity_id:
      - input_number.schieber_steckdose1
      - sensor.steckdose1_am2301_humidity
  action:
    service_template: >-
      {% set hi = (states('input_number.schieber_steckdose1') | float) + 2.5 %}
      {% set lo = hi - 5 %}
      {% set humidity = states('sensor.steckdose1_am2301_humidity') | float %}
      {% if humidity > hi %}
        switch.turn_on
      {% elif humidity < lo %}
        switch.turn_off
      {% else %}
        switch.turn_{{states('switch.steckdose1') | lower}}
      {% endif %}
    entity_id: switch.steckdose1
5 Likes

perfect, this is exactly what I was looking for (also an example for setting variables so I can continue to try some different automations)

THANK YOU very much!

You’re most welcome!

You may wish to consider tagging the post as ‘solution’ so others can find it more easily.

I used openHAB for about a half-year then looked into Home Assistant to see how it differs. I found it more to my liking (but both are equally competent products with helpful communities).

You’ll find Home Assistant’s automations, with their YAML structure and Jinja2 templating, to be quite different from openHAB’s Rules DSL. I encourage you to review the examples in the Cookbook to become more comfortable with this different style of creating automation logic. There are more complex examples found throughout this forum as well as in the Github repos shared by Home Assistant users.

If you find Home Assistant’s automations and scripts to be limiting, you have the option to use python_scripts or AppDaemon or Node-Red.

You are right, it’s really different.
For me personally, the rules in OpenHAB were just easier to understand and implement, but seriously, this is the only thing that I am missing about OpenHAB when I compare it with Home Assistant.

Thanks again!

1 Like

Can you reshare the automations?

https://pastebin.com/6R49H1hz

@123
This is a very nice solution. I plan on taking what you have done here and use it for a video where I’m taking a Sonoff SV and building a humidifier control. IE I’m going to flip the ON and OFF command so the device powers when the humidity is low and make new names, but your elegant code will remain.
I’ll credit you and put a link to it here in this post.
Thanks for supporting the more programming-addled among us!

1 Like

https://youtu.be/2bRu4H6qo84
Here is the link to my YouTube video where I use this automation to sucessfully make my Humidistat solution to work!

Could you please share again your automations? Many thanks

Here you go: Ubuntu Pastebin

Card is updated too:

card_mod:
  class: top-level-card
entities:
  - entity: switch.lounge_dehumidifier
    state_color: true
  - entity: script.lounge_dehumidifier_emptied
    state_color: true
  - animation:
      state: 'off'
    columns: '1'
    direction: right
    entities:
      - entity: sensor.lounge_dehumidifier_power
        max: '800'
        min: '0'
        name: Power
        severity:
          - color: '#039BE5'
            from: '0'
            to: '299.9'
          - color: '#0da035'
            from: '300'
            to: '899.9'
          - color: '#e0b400'
            from: '900'
            to: '1799.9'
          - color: '#e45e65'
            from: '1800'
            to: '2400'
    entity_row: true
    height: 28px
    positions:
      icon: inside
      indicator: inside
      minmax: 'off'
      name: inside
      title: 'off'
      value: inside
    type: 'custom:bar-card'
    value_style:
      color: var(--primary-text-color)
      text-shadow: none
  - card_type: 'custom:fold-entity-row'
    entities:
      - entity: automation.lounge_dehumidifier_direct_automation
        state_color: true
      - entity: automation.lounge_dehumidifier_re_enable
        state_color: true
      - entity: input_number.lounge_dehumidifier_condensation_set
      - entity: input_number.lounge_dehumidifier_outside_temp_set
    head:
      label: Direct Automation
      type: section
    padding: 0
    type: 'custom:hui-element'
  - card_type: 'custom:fold-entity-row'
    entities:
      - entity: automation.lounge_dehumidifier_forecast_automation
        state_color: true
      - entity: input_datetime.lounge_dehumidifier_run_time
      - entity: input_number.lounge_dehumidifier_rh_set
      - entity: input_number.lounge_dehumidifier_forecast_temp_set
    head:
      label: Forecast Automation
      type: section
    padding: 0
    type: 'custom:hui-element'
  - card_type: 'custom:fold-entity-row'
    entities:
      - entity: input_boolean.lounge_dehumidifier_mute_for_movie
        state_color: true
      - entity: input_number.lounge_dehumidifier_run_time
    head:
      label: Common Configuration
      type: section
    padding: 0
    type: 'custom:hui-element'
show_header_toggle: false
title: Lounge Dehumidifier
type: entities

It won’t be this way for long. I plan to put an ESP32 in the dehumidifier for direct control and monitoring soon.

Hi i run ESPhome (not tasmota) and i use a relay to switch on and off the humidifier(not dehumidifier). My automation script is not working, can you help me with that? The humidifier needs to turn on when the dht11 sensor reads 45% or lower, and turn off if it reaches more then 52% humidity. For sanity check i need to paste this to the automation.yaml or create a new custom component and a new humidifier.yaml file?
The script:

- id: '*************'
  alias: Humidifier on
  description: ''
  trigger:
  - platform: time_pattern
    minutes: 5
  condition:
  - condition: numeric_state
    entity_id: sensor.humidity
    above: '45'
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    after: '10:00'
    before: '20:00'
  - condition: state
    entity_id: switch.relay_humidifier
    state: 'off'
  action:
  - type: turn_on
    device_id: *************
    entity_id: switch.relay_humidifier
    domain: switch
  mode: single
- id: '*************'
  alias: Humidifier off
  description: ''
  trigger:
  - platform: time_pattern
    minutes: 5
  condition:
  - condition: numeric_state
    entity_id: sensor.humidity
    above: '52'
  - condition: time
    after: '10:00'
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    before: '20:00'
  - condition: state
    entity_id: switch.relay_humidifier
    state: 'on'
  action:
  - type: turn_off
    device_id: ********************
    entity_id: switch.relay_humidifier
    domain: switch
  mode: single

It turns on and off every 5 minutes… i tested it with 10sec intervals because i didn’t have the time to sit and watch it that long. It works between the two values great but outside of the two value things are different.