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?
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.
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’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:
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.
@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!