Added all the changes necessary to run chlorine pump again

This commit is contained in:
Nicolas Bachschwell 2024-05-24 16:00:34 +02:00
parent cb6a250d29
commit fd6d54697c
Signed by: NBSgamesAT
GPG Key ID: 2D73288FF7AEED2F
3 changed files with 54 additions and 37 deletions

View File

@ -2,11 +2,11 @@ esphome:
name: chlorine-pump name: chlorine-pump
project: project:
name: nbsgamesat.chlorine-pump name: nbsgamesat.chlorine-pump
version: "0.2" version: "0.7"
on_boot: on_boot:
priority: 600 priority: 600
then: then:
output.turn_on: whatever output.turn_on: wifi_status_led
esp8266: esp8266:
board: nodemcuv2 board: nodemcuv2
@ -21,7 +21,7 @@ external_components:
- source: - source:
type: local type: local
path: external_components/ path: external_components/
components: [ analog_orp, chlorine_pump ] components: [ chlorine_pump ]
globals: globals:
- id: last_pump_state - id: last_pump_state
@ -40,7 +40,9 @@ wifi:
password: !secret wifi_password password: !secret wifi_password
fast_connect: true fast_connect: true
on_connect: on_connect:
output.turn_off: whatever output.turn_off: wifi_status_led
on_disconnect:
output.turn_on: wifi_status_led
output: output:
- platform: gpio - platform: gpio
@ -50,7 +52,7 @@ output:
pin: pin:
number: D0 number: D0
inverted: true inverted: true
id: whatever id: wifi_status_led
binary_sensor: binary_sensor:
- platform: gpio - platform: gpio
@ -137,20 +139,22 @@ text_sensor:
id: cycle_text_info id: cycle_text_info
sensor: sensor:
- platform: analog_orp - platform: adc
name: Chlorine name: Chlorine
id: chlorine_sensor id: chlorine_sensor
unit_of_measurement: "mV"
icon: "mdi:test-tube"
pin: A0 pin: A0
zero_point: 673 update_interval: 200ms
inverted: true filters:
update_interval: 1s - multiply: 3.3
# print_raw: true - offset: -1.5
average: - multiply: 1000.0
mesurements: 15 - median:
send_state_every: 10 window_size: 25
# on_value_read: send_every: 25
# - lambda: |- send_first_at: 25
# ESP_LOGD("WHAT", "Chlorine_value, %.1f", x); - offset: 30.0
- platform: hx711 - platform: hx711
name: "Chlorine Canister Levels" name: "Chlorine Canister Levels"
dout_pin: D5 dout_pin: D5
@ -163,13 +167,19 @@ 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: 700 target: 700
disable_clock: true disable_clock: false
proportional_band: 400
on_pump_value: on_pump_value:
- lambda: |- - lambda: |-
if(pState != id(last_pump_state)){ if(pState != id(last_pump_state)){

View File

@ -1,8 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.components import output from esphome.components import output, sensor
from esphome.components.analog_orp.sensor import ChlorineSensor
from esphome.const import ( from esphome.const import (
CONF_ID, CONF_ID,
CONF_TRIGGER_ID CONF_TRIGGER_ID
@ -22,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_PUMP_TIME_DIVIDER = "pump_time_divider"
def to_proportional_band(value): def to_proportional_band(value):
try: try:
@ -29,7 +29,7 @@ def to_proportional_band(value):
except (TypeError, ValueError): except (TypeError, ValueError):
# pylint: disable=raise-missing-from # pylint: disable=raise-missing-from
raise cv.Invalid(f"") raise cv.Invalid(f"")
return cv.one_of(20, 50, 100, 150, 200, 250)(value) return cv.one_of(20, 50, 100, 150, 200, 300, 400)(value)
ChlorSensorOutputTrigger = chlorine_pump_ns.class_( ChlorSensorOutputTrigger = chlorine_pump_ns.class_(
"ChlorSensorOutputTrigger", automation.Trigger.template(cg.bool_, cg.int_) "ChlorSensorOutputTrigger", automation.Trigger.template(cg.bool_, cg.int_)
@ -42,15 +42,17 @@ PrimeAction = chlorine_pump_ns.class_("ChlorinePrime", automation.Action)
StartAction = chlorine_pump_ns.class_("ChlorineStart", automation.Action) StartAction = chlorine_pump_ns.class_("ChlorineStart", automation.Action)
StopAction = chlorine_pump_ns.class_("ChlorineStop", automation.Action) 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)
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(ChlorinePump), cv.GenerateID(): cv.declare_id(ChlorinePump),
cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput), cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput),
cv.Optional(CONF_SENSOR): cv.use_id(ChlorineSensor), 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_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_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band,
cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean, cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean,
cv.Optional(CONF_TARGET, 700): cv.int_range(300, 1400), 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.Optional(CONF_PUMP_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger), cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger),
} }
@ -82,6 +84,11 @@ async def stop_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren) return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action("chlorine_pump.manual_dose", StopAction, ACTION_SCHEMA)
async def stop_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action("chlorine_pump.set_target", SetTargetAction, ACTION_SCHEMA.extend({ @automation.register_action("chlorine_pump.set_target", SetTargetAction, ACTION_SCHEMA.extend({
cv.Required(CONF_TARGET): cv.templatable(cv.float_), cv.Required(CONF_TARGET): cv.templatable(cv.float_),
})) }))
@ -100,9 +107,9 @@ async def to_code(config):
#btnReset = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN]) #btnReset = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN])
#cg.add(var.set_input_pin(btnReset)) #cg.add(var.set_input_pin(btnReset))
if CONF_SENSOR in config:
sensor = await cg.get_variable(config[CONF_SENSOR]) sensor = await cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_sensor(sensor)) cg.add(var.set_sensor(sensor))
pump_out = await cg.get_variable(config[CONF_PUMP_OUT]) pump_out = await cg.get_variable(config[CONF_PUMP_OUT])
cg.add(var.set_pump_out(pump_out)) cg.add(var.set_pump_out(pump_out))

View File

@ -2,7 +2,6 @@
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
#include "esphome/components/gpio/output/gpio_binary_output.h" #include "esphome/components/gpio/output/gpio_binary_output.h"
#include "esphome/components/analog_orp/analog_orp.h"
static const char *const TAG = "chlorine_pump"; static const char *const TAG = "chlorine_pump";
@ -12,7 +11,7 @@ namespace chlorine_pump {
class ChlorinePump : public Component { class ChlorinePump : public Component {
protected: protected:
analog_orp::ChlorineSensor *sensor_; sensor::Sensor *sensor_;
gpio::GPIOBinaryOutput *pump_out; gpio::GPIOBinaryOutput *pump_out;
int cycle_time; int cycle_time;
int prop_band; int prop_band;
@ -24,6 +23,7 @@ class ChlorinePump : public Component {
int tOn = 0; int tOn = 0;
int tOff = 0; int tOff = 0;
float target_ = 700.0f; float target_ = 700.0f;
float tOn_divider_;
public: public:
@ -35,9 +35,12 @@ class ChlorinePump : public Component {
void disable_clock(bool disable_clock){ void disable_clock(bool disable_clock){
this->disable_clock_ = disable_clock; this->disable_clock_ = disable_clock;
} }
void set_sensor(analog_orp::ChlorineSensor *sensor){ void set_sensor(sensor::Sensor *sensor){
this->sensor_ = sensor; this->sensor_ = sensor;
} }
void set_pump_time_divivder(float pump_time_divider){
this->tOn_divider_ = pump_time_divider;
}
void set_pump_out(gpio::GPIOBinaryOutput *pump_out){ void set_pump_out(gpio::GPIOBinaryOutput *pump_out){
this->pump_out = pump_out; this->pump_out = pump_out;
} }
@ -54,10 +57,7 @@ class ChlorinePump : public Component {
void setup() override { void setup() override {
last_action = millis(); last_action = millis();
if(disable_clock_ && sensor_ != nullptr){ if(disable_clock_ && sensor_ != nullptr){
if(!sensor_->has_averager()) sensor_->add_on_state_callback([=](float val) -> void { sensor_->add_on_state_callback([=](float val) -> void {
this->tick_time(val);
});
if(sensor_->has_averager()) sensor_->add_average_change_callback([=](float val) -> void {
this->tick_time(val); this->tick_time(val);
}); });
} }
@ -119,14 +119,13 @@ class ChlorinePump : public Component {
if(tOn == 0 && tOff == 0 && state){ if(tOn == 0 && tOff == 0 && state){
int seconds = calculatePumpTimeSeconds(last_mesurement); int seconds = calculatePumpTimeSeconds(last_mesurement);
tOn = seconds;
tOff = cycle_time - tOn;
if(seconds == 0){ if(seconds == 0){
tOn = 0; tOn = 0;
tOff = 30; tOff = 360;
} }
tOn = seconds;
tOff = cycle_time - tOn;
ESP_LOGD(TAG, "Time => tOn: %d, tOff: %d", tOn, tOff); ESP_LOGD(TAG, "Time => tOn: %d, tOff: %d", tOn, tOff);
this->callback_cycle_.call(tOn, tOff); this->callback_cycle_.call(tOn, tOff);
@ -149,7 +148,8 @@ class ChlorinePump : public Component {
if(millis() - last_action > 1000){ if(millis() - last_action > 1000){
tick_time(sensor_->get_state()); if(sensor_->has_state())
tick_time(sensor_->get_state());
setMillisPrecies(1000); setMillisPrecies(1000);
} }