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: on_boot:
priority: 600 priority: 600
then: 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: esp8266:
board: nodemcuv2 board: nodemcuv2
@ -175,19 +178,15 @@ sensor:
- 47608 -> 0 - 47608 -> 0
- 590566 -> 100 - 590566 -> 100
unit_of_measurement: "%" unit_of_measurement: "%"
- platform: template
name: debug_last_raw_value
id: debug_last_raw_value
accuracy_decimals: 0
unit_of_measurement: "points"
chlorine_pump: chlorine_pump:
pump: pump_switch pump: pump_switch
sensor: chlorine_sensor sensor: chlorine_sensor
id: chlorine_pump_component id: chlorine_pump_component
target: 600
disable_clock: false disable_clock: false
proportional_band: 400 proportional_band: 200
target: 650
# get_target: !lambda return id(chlorine_target).state;
on_pump_value: on_pump_value:
- lambda: |- - lambda: |-
if(pState != id(last_pump_state)){ 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_CYCLE_START = "on_cycle_start"
CONF_DISABLE_CLOCK="disable_clock" CONF_DISABLE_CLOCK="disable_clock"
CONF_TARGET="target" CONF_TARGET="target"
CONF_TARGET_LAMBDA="get_target"
CONF_PUMP_TIME_DIVIDER = "pump_time_divider" CONF_PUMP_TIME_DIVIDER = "pump_time_divider"
def to_proportional_band(value): 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) SetTargetAction = chlorine_pump_ns.class_("ChlorineSetTarget", automation.Action)
ManualDoseAction = chlorine_pump_ns.class_("ChlorineDose", automation.Action) ManualDoseAction = chlorine_pump_ns.class_("ChlorineDose", automation.Action)
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ def validate_config(config):
cv.GenerateID(): cv.declare_id(ChlorinePump), if (config.get(CONF_TARGET_LAMBDA, None) is not None) is not (config.get(CONF_TARGET, None) not in config):
cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput), raise cv.Invalid("Either a fixed target or an get_target lambda must be set to get a target")
cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor), return config
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, CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean, cv.COMPONENT_SCHEMA.extend({
cv.Optional(CONF_TARGET, 700): cv.int_range(300, 1400), cv.GenerateID(): cv.declare_id(ChlorinePump),
cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5), cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({ cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger), 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_CYCLE_START): automation.validate_automation({ cv.Optional(CONF_TARGET): cv.int_range(300, 1400),
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorCycleTrigger), 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_prop_band(config[CONF_PUMP_PROPORTIONAL_BAND]))
#cg.add(var.set_state(config[CONF_STATE])) #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: if CONF_PUMP_VALUE in config:

View File

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