diff --git a/basement_bathroom_sensor.yaml b/basement_bathroom_sensor.yaml index 6e1016c..2ea8cd9 100644 --- a/basement_bathroom_sensor.yaml +++ b/basement_bathroom_sensor.yaml @@ -40,6 +40,12 @@ binary_sensor: pin: number: D0 mode: INPUT_PULLDOWN_16 + + - platform: template + id: motion_delay_3s + lambda: return id(motion).state; + filters: + - delayed_off: 3s on_press: then: - if: @@ -62,7 +68,7 @@ binary_sensor: service: homeassistant.turn_on data: entity_id: light.basement_shower_light - + - platform: template id: motion_delay_30s lambda: return id(motion).state; diff --git a/bed_sensor.h b/bed_sensor.h index 9743706..18382c3 100644 --- a/bed_sensor.h +++ b/bed_sensor.h @@ -7,9 +7,8 @@ class BedSensor : public PollingComponent { const int sensorGpio = A0; public: - BinarySensor *melissaInBed = new BinarySensor(); - BinarySensor *chrisInBed = new BinarySensor(); - BinarySensor *someoneInBed = new BinarySensor(); + Sensor *melissaBedSensor = new Sensor(); + Sensor *chrisBedSensor = new Sensor(); BedSensor() : PollingComponent(5000) { } @@ -22,15 +21,12 @@ class BedSensor : public PollingComponent { digitalWrite(melissaGpio, HIGH); digitalWrite(chrisGpio, LOW); int melissaValue = analogRead(sensorGpio); - ESP_LOGD("bed_sensor_melissa", "The value of sensor is: %i", melissaValue); - melissaInBed->publish_state(melissaValue < 300); digitalWrite(melissaGpio, LOW); digitalWrite(chrisGpio, HIGH); int chrisValue = analogRead(sensorGpio); - ESP_LOGD("bed_sensor_chris", "The value of sensor is: %i", chrisValue); - chrisInBed->publish_state(chrisValue < 300); - someoneInBed->publish_state(!melissaInBed->state && !chrisInBed->state && melissaValue < 800 && chrisValue < 800); + melissaBedSensor->publish_state(melissaValue); + chrisBedSensor->publish_state(chrisValue); } }; \ No newline at end of file diff --git a/master_bed.yaml b/master_bed.yaml index 92c2600..8603bc2 100644 --- a/master_bed.yaml +++ b/master_bed.yaml @@ -18,36 +18,124 @@ esphome: binary_sensor: - !include common/binary_sensor/status.yaml - - platform: custom - lambda: |- - auto bedSensor = new BedSensor(); - App.register_component(bedSensor); - return {bedSensor->melissaInBed, bedSensor->chrisInBed, bedSensor->someoneInBed}; - binary_sensors: - - name: "Melissa is in Bed" - id: melissa_in_bed - device_class: occupancy - - name: "Chris is in Bed" - id: chris_in_bed - device_class: occupancy - - name: "Someone is in Bed" - id: someone_in_bed - device_class: occupancy + + - platform: template + name: "Melissa is in Bed" + id: melissa_in_bed + device_class: occupancy + on_state: + then: + - sensor.template.publish: + id: master_bed_count + state: !lambda 'return id(melissa_in_bed).state + id(chris_in_bed).state + id(someone_in_bed).state;' + - platform: template + name: "Chris is in Bed" + id: chris_in_bed + device_class: occupancy + on_state: + then: + - sensor.template.publish: + id: master_bed_count + state: !lambda 'return id(melissa_in_bed).state + id(chris_in_bed).state + id(someone_in_bed).state;' + - platform: template + name: "Someone is in Bed" + id: someone_in_bed + device_class: occupancy + on_state: + then: + - sensor.template.publish: + id: master_bed_count + state: !lambda 'return id(melissa_in_bed).state + id(chris_in_bed).state + id(someone_in_bed).state;' sensor: - !include common/sensor/uptime.yaml - !include common/sensor/wifi.yaml + + - platform: custom + lambda: |- + auto bedSensor = new BedSensor(); + App.register_component(bedSensor); + return {bedSensor->melissaBedSensor, bedSensor->chrisBedSensor}; + sensors: + - name: "Melissa Bed Sensor" + id: melissa_bed_sensor + on_value: + then: + - sensor.template.publish: + id: melissa_bed_percent + state: !lambda 'return (1024 - x) / 1024 * 100;' + - name: "Chris Bed Sensor" + id: chris_bed_sensor + on_value: + then: + - sensor.template.publish: + id: chris_bed_percent + state: !lambda 'return (1024 - x) / 1024 * 100;' + - binary_sensor.template.publish: + id: someone_in_bed + state: !lambda 'return !id(melissa_in_bed).state && !id(chris_in_bed).state && id(melissa_bed_percent).state > 40 && id(chris_bed_percent).state > 40;' + + - platform: template + name: "Melissa Bed Percent" + id: melissa_bed_percent + icon: "mdi:hotel" + unit_of_measurement: "%" + accuracy_decimals: 0 + on_value: + then: + - binary_sensor.template.publish: + id: melissa_in_bed + state: !lambda 'return x > 75;' + - binary_sensor.template.publish: + id: someone_in_bed + state: !lambda 'return x <= 75 && !id(chris_in_bed).state && x > 40 && id(chris_bed_percent).state > 40;' + - platform: template + name: "Chris Bed Percent" + id: chris_bed_percent + icon: "mdi:hotel" + unit_of_measurement: "%" + accuracy_decimals: 0 + on_value: + then: + - binary_sensor.template.publish: + id: chris_in_bed + state: !lambda 'return x > 75;' + - binary_sensor.template.publish: + id: someone_in_bed + state: !lambda 'return !id(melissa_in_bed).state && x <= 75 && id(melissa_bed_percent).state > 40 && x > 40;' + - platform: template name: "Master Bed Count" id: master_bed_count accuracy_decimals: 0 icon: "mdi:hotel" - lambda: |- - return id(melissa_in_bed).state + id(chris_in_bed).state + id(someone_in_bed).state; + on_value: + then: + - text_sensor.template.publish: + id: master_bed_status + state: !lambda |- + if (x == 0) { + return "Empty"; + } else if (x == 2) { + return "Melissa and Chris"; + } else if (id(melissa_in_bed).state) { + return "Melissa"; + } else if (id(chris_in_bed).state) { + return "Chris"; + } else if (id(someone_in_bed).state) { + return "Someone"; + } else { + return "Unknown"; + } switch: - !include common/switch/restart.yaml text_sensor: - !include common/text_sensor/version.yaml - - !include common/text_sensor/wifi.yaml \ No newline at end of file + - !include common/text_sensor/wifi.yaml + + - platform: template + name: "Master Bed Status" + id: master_bed_status + icon: "mdi:hotel" diff --git a/pool_pumps.yaml b/pool_pumps.yaml new file mode 100644 index 0000000..32eb495 --- /dev/null +++ b/pool_pumps.yaml @@ -0,0 +1,148 @@ +substitutions: + device_id: pool_pumps + device_name: Pool Pumps + platform: ESP8266 + board: modwifi + ip_address: !secret pool_pumps_ip + ota_pwd: !secret pool_pumps_ota_pwd + api_pwd: !secret pool_pumps_api_pwd + ap_wifi_pwd: !secret pool_pumps_ap_wifi_pwd + +esphome: + <<: !include common/esphome.yaml + +<<: !include common/common.yaml +<<: !include common/logger/logger_debug_no_serial.yaml + +binary_sensor: + - !include common/binary_sensor/status.yaml + +i2c: + sda: GPIO12 + scl: GPIO14 + +sensor: + - !include common/sensor/uptime.yaml + - !include common/sensor/wifi.yaml + - platform: ade7953 + voltage: + name: ${device_name} Voltage + current_a: + name: Pool Cleaner Current + current_b: + name: Pool Pump Current + active_power_a: + name: Pool Cleaner Power + filters: + - multiply: -1 + active_power_b: + name: Pool Pump Power + filters: + - multiply: -1 + update_interval: 2s + - platform: ntc + sensor: temp_resistance_reading + name: ${device_name} Temperature + unit_of_measurement: "°C" + accuracy_decimals: 1 + icon: "mdi:thermometer" + calibration: + b_constant: 3350 + reference_resistance: 10kOhm + reference_temperature: 298.15K + - platform: resistance + id: temp_resistance_reading + sensor: temp_analog_reading + configuration: DOWNSTREAM + resistor: 32kOhm + - platform: adc + id: temp_analog_reading + pin: A0 + - platform: template + name: "Pool Mode Id" + id: pool_mode_id + internal: true + +status_led: + pin: + number: GPIO0 + inverted: yes + +switch: + - !include common/switch/restart.yaml + - platform: gpio + pin: GPIO4 + name: "Pool Pump" + id: pool_pump + on_turn_off: + then: + - switch.turn_off: pool_cleaner + - platform: gpio + pin: GPIO15 + name: "Pool Cleaner" + id: pool_cleaner + on_turn_on: + then: + - switch.turn_on: pool_pump + +text_sensor: + - !include common/text_sensor/version.yaml + - !include common/text_sensor/wifi.yaml + - platform: homeassistant + name: "Pool Mode Text" + entity_id: input_select.pool_mode + on_value: + then: + - sensor.template.publish: + id: pool_mode_id + state: !lambda 'return x == "Off" ? 0 : x == "Normal" ? 1 : x == "Continuous Without Heat" ? 2 : x == "Continuous With Heat" ? 3 : id(pool_mode_id).state;' + +time: + - platform: homeassistant + id: the_time + on_time: + seconds: 0 + minutes: 0,15,30,45 + then: + lambda: |- + if (id(pool_mode_id).state == 1) { + auto now = id(the_time).now(); + int hour = now.hour; + int minute = now.minute; + + if (hour == 5 && minute == 0) { + auto ha_service = new api::HomeAssistantServiceCallAction<>(api_apiserver); + ha_service->set_service("switch.turn_off"); + ha_service->set_data({api::KeyValuePair("entity_id", "switch.pool_heater")}); + ha_service->play(); + id(pool_pump).turn_on(); + id(pool_cleaner).turn_on(); + } + else if (hour == 7 && minute == 0) { + id(pool_pump).turn_on(); + id(pool_cleaner).turn_off(); + auto ha_service = new api::HomeAssistantServiceCallAction<>(api_apiserver); + ha_service->set_service("switch.turn_on"); + ha_service->set_data({api::KeyValuePair("entity_id", "switch.pool_heater")}); + ha_service->play(); + } + else if (hour >= 8 && hour <= 21 && (minute == 0 || minute == 30)) { + id(pool_pump).turn_off(); + } + else if (hour >= 8 && hour <= 21 && (minute == 15 || minute == 45)) { + id(pool_pump).turn_on(); + id(pool_cleaner).turn_off(); + auto ha_service = new api::HomeAssistantServiceCallAction<>(api_apiserver); + ha_service->set_service("switch.turn_on"); + ha_service->set_data({api::KeyValuePair("entity_id", "switch.pool_heater")}); + ha_service->play(); + } + else if (hour == 22 && minute == 0) { + auto ha_service = new api::HomeAssistantServiceCallAction<>(api_apiserver); + ha_service->set_service("switch.turn_off"); + ha_service->set_data({api::KeyValuePair("entity_id", "switch.pool_heater")}); + ha_service->play(); + id(pool_cleaner).turn_off(); + id(pool_pump).turn_off(); + } + } \ No newline at end of file