I have this code below that I would like to trigger conditionally on a switch state, however it executes no matter what state the switch is in. The switch is a template switch (fans_auto_en). I’ve also tried to do it in the lambda with id(fans_auto_en).state but that yields the same result. The part that is not executing as intended is the if condition in the on_value block of the heatindex template sensor.
This automatation needs to be on the ESP device because wifi is spotty where it is located. It is using a ESP32U and is about 75m from the AP.
Any insite would be appreciated.
number:
- platform: template
id: trigger_fan_on
name: Trigger Fan On Temp
min_value: 25
max_value: 50
step: 0.5
optimistic: true
restore_value: true
on_value:
then:
- sensor.template.publish:
id: calculated_off_temp
state: !lambda 'return id(trigger_fan_on).state - id(offset_fan_off).state;'
- platform: template
id: offset_fan_off
name: Offset Fan Off By
min_value: 0
max_value: 10
step: 0.2
optimistic: true
restore_value: true
on_value:
then:
- sensor.template.publish:
id: calculated_off_temp
state: !lambda 'return id(trigger_fan_on).state - id(offset_fan_off).state;'
- platform: template
id: available_hour_start
name: Allow on after this hour
min_value: 7
max_value: 13
step: 1
optimistic: true
restore_value: true
- platform: template
id: available_hour_stop
name: Disallow on after this hour
min_value: 13
max_value: 22
step: 1
optimistic: true
restore_value: true
time:
- platform: sntp
id: sntp_time
timezone: America/Toronto
update_interval: 1hours
on_time_sync:
then:
logger.log: Time Synced
on_time:
- seconds: 0
minutes: 0
hours: 22
then:
- switch.turn_off:
id: plug1
- switch.turn_on:
id: fans_auto_en
output:
- platform: gpio
pin:
number: ${plug1}
inverted: true
id: out_plug1
switch:
- platform: output
id: plug1
name: Plug 1 Relay
output: out_plug1
- platform: template
id: fans_auto_en
name: Fan Automation Enabled
optimistic: True
restore_mode: RESTORE_DEFAULT_ON
- platform: template
name: Heat Index
id: heatindex
lambda: |-
float c1 = -8.78469475556;
float c2 = 1.61139411;
float c3 = 2.33854883889;
float c4 = -0.14611605;
float c5 = -0.012308094;
float c6 = -0.0164248277778;
float c7 = 0.002211732;
float c8 = 0.00072546;
float c9 = -0.000003582;
float T = id(temp).state;
float R = id(hum).state;
if (T >= 26.67 && R >= 40) {
return (c1 + (c2 * T) + (c3 * R) + (c4 * T * R) + (c5 * pow(T,2)) + (c6 * pow(R,2)) + ( c7 * pow(T,2) * R) + (c8 * T * pow(R,2)) + (c9 * pow(T,2) * pow(R,2)));
} else {
return (T);
}
unit_of_measurement: "°C"
icon: "mdi:water-percent"
device_class: "temperature"
state_class: "measurement"
on_value_range:
- above: !lambda 'return id(trigger_fan_on).state;'
then:
- if:
condition:
- switch.is_on: fans_auto_en
then:
- lambda: |-
auto time = id(sntp_time).now();
if(time.hour >= id(available_hour_start).state && time.hour <= id(available_hour_stop).state) {
id(plug1).turn_on();
}
- switch.turn_on: plug1
- below: !lambda 'return id(trigger_fan_on).state - id(offset_fan_off).state;'
then:
- switch.turn_off: plug1