Fixed a bug for chlorPump.yaml where the target would not properly load on start

This commit is contained in:
Nicolas Bachschwell 2024-05-27 14:43:10 +02:00
parent 1bd3b7eb90
commit d21229fa73
Signed by: NBSgamesAT
GPG Key ID: 2D73288FF7AEED2F
3 changed files with 63 additions and 30 deletions

View File

@ -6,7 +6,10 @@ esphome:
on_boot:
priority: 600
then:
output.turn_on: wifi_status_led
- output.turn_on: wifi_status_led
- chlorine_pump.set_target:
target: !lambda return id(chlorine_target).state;
esp8266:
board: nodemcuv2
@ -175,19 +178,15 @@ sensor:
- 47608 -> 0
- 590566 -> 100
unit_of_measurement: "%"
- platform: template
name: debug_last_raw_value
id: debug_last_raw_value
accuracy_decimals: 0
unit_of_measurement: "points"
chlorine_pump:
pump: pump_switch
sensor: chlorine_sensor
id: chlorine_pump_component
target: 600
disable_clock: false
proportional_band: 400
proportional_band: 200
target: 650
# get_target: !lambda return id(chlorine_target).state;
on_pump_value:
- lambda: |-
if(pState != id(last_pump_state)){

View File

@ -21,6 +21,7 @@ CONF_PUMP_VALUE = "on_pump_value"
CONF_CYCLE_START = "on_cycle_start"
CONF_DISABLE_CLOCK="disable_clock"
CONF_TARGET="target"
CONF_TARGET_LAMBDA="get_target"
CONF_PUMP_TIME_DIVIDER = "pump_time_divider"
def to_proportional_band(value):
@ -44,24 +45,33 @@ StopAction = chlorine_pump_ns.class_("ChlorineStop", automation.Action)
SetTargetAction = chlorine_pump_ns.class_("ChlorineSetTarget", automation.Action)
ManualDoseAction = chlorine_pump_ns.class_("ChlorineDose", automation.Action)
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(ChlorinePump),
cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
cv.Optional(CONF_PUMP_CYCLE_TIME, default=360): cv.int_range(min=30, max=1400),
cv.Optional(CONF_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band,
cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean,
cv.Optional(CONF_TARGET, 700): cv.int_range(300, 1400),
cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5),
cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger),
}
),
cv.Optional(CONF_CYCLE_START): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorCycleTrigger),
}
),
})
def validate_config(config):
if (config.get(CONF_TARGET_LAMBDA, None) is not None) is not (config.get(CONF_TARGET, None) not in config):
raise cv.Invalid("Either a fixed target or an get_target lambda must be set to get a target")
return config
CONFIG_SCHEMA = cv.All(
cv.COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(ChlorinePump),
cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
cv.Optional(CONF_PUMP_CYCLE_TIME, default=360): cv.int_range(min=30, max=1400),
cv.Optional(CONF_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band,
cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean,
cv.Optional(CONF_TARGET): cv.int_range(300, 1400),
cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5),
cv.Optional(CONF_TARGET_LAMBDA, 2): cv.templatable(cv.float_),
cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger),
}
),
cv.Optional(CONF_CYCLE_START): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorCycleTrigger),
}
),
}),
validate_config
)
@ -123,6 +133,11 @@ async def to_code(config):
cg.add(var.set_prop_band(config[CONF_PUMP_PROPORTIONAL_BAND]))
#cg.add(var.set_state(config[CONF_STATE]))
if CONF_TARGET not in config:
template_ = await cg.templatable(config[CONF_TARGET_LAMBDA], [], float)
cg.add(var.set_target_lambda(template_))
else:
cg.add(var.set_target(config[CONF_TARGET]))
if CONF_PUMP_VALUE in config:

View File

@ -22,8 +22,9 @@ class ChlorinePump : public Component {
CallbackManager<void(int, int)> callback_cycle_;
int tOn = 0;
int tOff = 0;
float target_ = 700.0f;
float target_ = -1;
float tOn_divider_;
std::function<float()> target_lambda_ = nullptr;
public:
@ -62,6 +63,11 @@ class ChlorinePump : public Component {
});
}
}
void set_target_lambda(std::function<float()> callback){
target_lambda_ = std::move(callback);
}
void prime() {
tOn = 30;
tOff = 2;
@ -81,6 +87,7 @@ class ChlorinePump : public Component {
}
void setMillisPrecies(unsigned long waitPeriod){
if(last_action + waitPeriod + waitPeriod > millis()){
@ -94,9 +101,17 @@ class ChlorinePump : public Component {
int calculatePumpTimeSeconds(float last_mesurement){
if(last_mesurement >= target_) return 0;
float used_target = -1;
if(target_ != -1){
used_target = target_;
}
else{
used_target = target_lambda_();
}
float currentVal = target_ - last_mesurement;
if(last_mesurement >= used_target) return 0;
float currentVal = used_target - last_mesurement;
float proportionalValue = (currentVal / (float) prop_band);
float timeInSeconds = proportionalValue * (float) cycle_time;
@ -154,6 +169,10 @@ class ChlorinePump : public Component {
setMillisPrecies(1000);
}
}
void dump_config() override {
}
};
class ChlorSensorOutputTrigger : public Trigger<bool, int> {