Automation trigger problem using multiple triggers

I would like to trigger an automation whenever a detected dehumidifier humidity level changes and take different action depending on the current humidity level compared to the target humidity level.

The following code is what I would like to do but I’m not permitted to check for the change in humidity level in the condition section as I have it here (this errors).

If I leave out the condition the automation continuously triggers - not what I want to do.

How would you suggest I change this automation to work properly?

alias: Set 30L Dehumidifer Speed
description: ""
triggers:
  - trigger: template
    value_template: >-
      {{
      (states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
      | int) >
       ((states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity') | int) + 4)
      }}
    id: highspeed
  - trigger: template
    value_template: >-
      {{
      (states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
      | int) <=
       ((states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity') | int) + 4)
      }}
    id: lowspeed
conditions:
  - condition: state
    entity_id: sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity
    from:
      - anything
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lowspeed ...

I think you would probably be better off using State triggers where you can use from for the sensor and you could incorporate a duration requirement for the number so you don’t get multiple triggers while adjusting the value.

State conditions do not have access to previous state data, so that’s a non-starter. A Template Condition can have access to the previous state in certain circumstances, but there wouldn’t be access if it was the change of the number entity that caused the template to render. Can you clarify the goal of the condition?

The goal of the automation is: If the humidity level is equal to or less than 4 percentage points above the target humidity I would like to put the dehumidifier on low speed. If it is higher than that point I would like to put the dehumidifier on high speed. I would like to have the automation triggered whenever the humidity level changes. The automation action will set the fan speed on the dehumidifier dependent on the trigger results.

The goal of the condition is to check if the humidity level changed so the automation only runs if there is a change.

(I have another humidity sensor in the room and will eventually take the average of the two sensors (template) but for now I’m just using the dehumidifier recorded humidity level.)

Hopefully, this is clear. The bottom line is I want to change the speed of the dehumidifer fan.

I think the approach that I would suggest is to add duration requirements to your triggers. That will prevent the “continuous” triggers. You might want to check your dehumidifier’s manual to see if there are any recommendations for minimal cycle length, and set the duration to at lease that.

alias: Set 30L Dehumidifer Speed
description: ""
triggers:
  - trigger: template
    value_template: >-
      {{
      (states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
      | int) >
       ((states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity') | int) + 4)
      }}
    for: "00:05:00"
    id: highspeed
  - trigger: template
    value_template: >-
      {{
      (states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
      | int) <=
       ((states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity') | int) + 4)
      }}
    for: "00:05:00"
    id: lowspeed
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - lowspeed ...

You have put the templates into developer tools to test?

Here’s a template trigger I use for a humidifier ON:

{{states(‘sensor.upstairs_current_humidity’) |int + 3 <
states(‘sensor.humidifier_target’) |int + states(‘input_number.humidifier_adjustment’) |int}}

Yes these templates work. The only part that doesn’t work is the condition.

That should definitely work - thank you.

So you have defined it in that text:

Tigger on the current state of the humidifier (can be from anything)
Use Choose to check the condition of the current humidity against target
Only set the preset mode if it wasn’t already set to that mode

alias: Set 30L Dehumidifer Speed (Percentage Offset)
description: "High speed at Target + 4% of target"
triggers:
  - trigger: state
    entity_id: sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity
conditions:
  # Only run if the numerical state actually changed
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  - condition: template
    value_template: "{{ is_number(states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')) }}"
actions:
  - vars:
      current: "{{ states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity') | float(0) }}"
      target: "{{ states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity') | float(0) }}"
      # This calculates 4 percent and adds to target
      threshold: "{{ target + ((target * 4) / 100) }}"
  - choose:
      # HIGH SPEED: Current is above the calculated threshold
      - conditions:
          - condition: template
            value_template: "{{ current > threshold }}"
          - condition: template
            value_template: "{{ not is_state_attr('fan.pro_breeze_30l_dehumidifier', 'preset_mode', 'high') }}"
        sequence:
          - action: fan.set_preset_mode
            target:
              entity_id: fan.pro_breeze_30l_dehumidifier
            data:
              preset_mode: "high"

      # LOW SPEED: Current is at or below the threshold
      - conditions:
          - condition: template
            value_template: "{{ current <= threshold }}"
          - condition: template
            value_template: "{{ not is_state_attr('fan.pro_breeze_30l_dehumidifier', 'preset_mode', 'low') }}"
        sequence:
          - action: fan.set_preset_mode
            target:
              entity_id: fan.pro_breeze_30l_dehumidifier
            data:
              preset_mode: "low"

I’ll give this a try tomorrow and let you know how things work out. Reading through it looks great.

I’m going to have to put this aside for a couple of days. I made some changes to the use of variables but I still have an error when I try to add to the variable on this line threshold: "{{ target + 4 }}" I get Error: TypeError: can only concatenate str (not “int”) to str. I haven’t used variables enough to know how to fix this yet and with the holiday I don’t have the time to research further at the moment.
Current code:

alias: Set 30L Dehumidifer Speed
description: Turn on high speed at Target + 4
triggers:
  - trigger: state
    entity_id: sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity
conditions:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  - condition: template
    value_template: >-
      {{
      is_number(states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity'))
      }}
actions:
  - variables:
      current: >-
        "{{
        states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
         | float(0) }}"
      target: >-
        "{{
        states('number.pro_breeze_30l_compressor_dehumidifier_target_humidity')
         | float(0) }}"
      threshold: "{{ target +  4 }}"
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ not
              (states('sensor.pro_breeze_30l_compressor_dehumidifier_water_tank_is_full'))
              == 4 }}
          - condition: template
            value_template: >-
              {{ not
              (states('sensor.pro_breeze_30l_compressor_dehumidifier_water_tank_is_full'))
              == 8 }}
          - condition: template
            value_template: "{{ current <= threshold }}"
          - condition: template
            value_template: >-
              "{{ not
              (states('select.pro_breeze_30l_compressor_dehumidifier_wind_speed'))
              == 'Low' }}"
        sequence:
          - action: tts.speak
            data:
              cache: true
              message: Dehumidifier is turning to low speed
              media_player_entity_id: media_player.mylaptop
            target:
              entity_id: tts.google_en_com
            enabled: true
          - action: select.select_last
            metadata: {}
            target:
              entity_id: select.pro_breeze_30l_compressor_dehumidifier_wind_speed
            data: {}
      - conditions:
          - condition: template
            value_template: >-
              {{ not
              (states('sensor.pro_breeze_30l_compressor_dehumidifier_water_tank_is_full'))
              == 4 }}
          - condition: template
            value_template: >-
              {{ not
              (states('sensor.pro_breeze_30l_compressor_dehumidifier_water_tank_is_full'))
              == 8 }}
          - condition: template
            value_template: "{{ current > threshold }}"
          - condition: template
            value_template: >-
              "{{ not
              (states('select.pro_breeze_30l_compressor_dehumidifier_wind_speed'))
              == 'High' }}"
        sequence:
          - action: tts.speak
            data:
              cache: true
              message: Dehumidifier is turning to high speed
              media_player_entity_id: media_player.mylaptop
            target:
              entity_id: tts.google_en_com
            enabled: true
          - action: select.select_first
            metadata: {}
            target:
              entity_id: select.pro_breeze_30l_compressor_dehumidifier_wind_speed
            data: {}
mode: single

The cause of the error is that you are using both multi-line quotes indicators >- and double quotes " when you are defining the variables as well as in a number of your template conditions. Use one or the other, not both.

So instead of:

      current: >-
        "{{
        states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
         | float(0) }}"

It should be:

      current: >-
        {{ states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity')
        | float(0) }}

or:

      current: "{{ states('sensor.pro_breeze_30l_compressor_dehumidifier_current_humidity') | float(0) }}"

Thanks that should be a quick fix I can make after the holidays.

Kudos to both @SJ20035 and @Didgeridrew.

@SJ20035 I used your logic with a few syntax changes - many thanks for helping me see a different way to code this in HA.

@Didgeridrew thank you for two things - one for pointing out my quote problem with templates. I’m pretty sure you’ve corrected me with this problem before - thank you for having the patience to do so again. Secondly, when the humidity level changes on the Pro-breeze 30L dehumidifier I’ve found that it wildly changes from multiple levels in the matter of a few minutes. So I’ve also included your suggestion to add a delay to my trigger.

I was really stuck on this automation and can’t thank you both enough!