Hi,
I’m currently working on a small project with an Arduino Nano ESP32 to control a fan based on temperature. The device is also integrated into HA.
I added input numbers in HA to set the temperature thresholds (low and high) and the corresponding sensors in ESPHome.
Everything works fine, except for the initial values.
If the Nano powers up while HA is ON, then the initial values are provided by HA.
But if the Nano powers up while HA is OFF, I would like the Nano to operate with the same thresholds.
So, how can I do that? I searched the forum, but all I got is someone saying that I have to do some black magic with lambda … Not sure what he meant.
Please help,
Here is the ESPHome code:
substitutions:
minimum_fan_speed: "10" # What is the minimum fan speed, as a percentage
esphome:
name: instruments
friendly_name: Instruments
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "zzz"
ota:
- platform: esphome
password: "zzz"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Instruments"
password: "zzz"
captive_portal:
# Add a temperature sensor on pin D2
one_wire:
- platform: gpio
pin: GPIO5
sensor:
- platform: dallas_temp
name: "Temperature"
id: temperature
icon: "mdi:temperature-celsius"
update_interval: 5s
on_value:
then:
- script.execute: set_fan_state
# Add a temperature threshold low
- platform: homeassistant
name: "Temperature Threshold Low"
entity_id: sensor.instruments_temperature_threshold_low
id: temperature_threshold_low
icon: "mdi:temperature-celsius"
# Add a temperature threshold high
- platform: homeassistant
name: "Temperature Threshold High"
entity_id: sensor.instruments_temperature_threshold_high
id: temperature_threshold_high
icon: "mdi:temperature-celsius"
# Add a RPM sensor on pin D3
- platform: pulse_counter
pin: GPIO6
name: "Fans RPM"
id: fans_rpm
icon: "mdi:rotate-3d-variant"
unit_of_measurement: 'RPM'
filters:
multiply: 0.5
count_mode:
rising_edge: INCREMENT
falling_edge: DISABLE
update_interval: 3s
# Add a sensor template to display PWM duty ccycle in HA
- platform: template
name: "Fans PWM Duty Cycle"
id: fans_pwm_duty_cycle
lambda: |-
if (id(fans).state == false)
{
return 0;
}
else
{
return {};
}
icon: "mdi:rotate-3d-variant"
unit_of_measurement: "%"
accuracy_decimals: 0
# Add a PWM signal on pin D10
output:
- platform: ledc
pin: GPIO21
frequency: 25000 Hz
id: fans_pwm
# Add a fan
fan:
- platform: speed
output: fans_pwm
name: "Fans"
id: fans
icon: "mdi:fan"
on_turn_off:
- sensor.template.publish:
id: fans_pwm_duty_cycle
state: !lambda 'return 0;'
on_speed_set:
- sensor.template.publish:
id: fans_pwm_duty_cycle
state: !lambda 'return id(fans).speed;'
script:
- id: set_fan_state
then:
- if:
condition:
lambda: |-
return id(temperature).state < id(temperature_threshold_low).state;
then:
- fan.turn_off: fans
else:
- fan.turn_on:
id: fans
speed: !lambda |-
if (id(temperature).state >= id(temperature_threshold_high).state) {
return 100;
}
else {
float calc_speed = ((100-id(${minimum_fan_speed})) / (id(temperature_threshold_high).state-id(temperature_threshold_low).state))*(id(temperature).state-id(temperature_threshold_low).state)+id(${minimum_fan_speed});
return calc_speed;
}
Here is the HA configuration.yaml
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
input_number:
instruments_temperature_threshold_low:
name: "Instruments Temperature Threshold Low"
initial: 25
min: 20
max: 60
step: 1
instruments_temperature_threshold_high:
name: "Instruments Temperature Threshold High"
initial: 40
min: 20
max: 60
step: 1
sensor:
- platform: template
sensors:
instruments_temperature_threshold_low:
value_template: '{{ states.input_number.instruments_temperature_threshold_low.state | float }}'
instruments_temperature_threshold_high:
value_template: '{{ states.input_number.instruments_temperature_threshold_high.state | float }}'