├── img ├── t.txt ├── end.jpg ├── Washing-Machine-Sonoff-Pow-R2-Esphome.mp4 └── 2020-04-25 22_09_59-Panoramica - Home Assistant.png ├── lovelace_entity_card.txt ├── home_assistant_w_machine.yaml ├── README.md └── sonoff_pow_r2_w_machine.yaml /img/t.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /img/end.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gio-dot/Washing-Machine-Sonoff-Pow-R2-Esphome/HEAD/img/end.jpg -------------------------------------------------------------------------------- /img/Washing-Machine-Sonoff-Pow-R2-Esphome.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gio-dot/Washing-Machine-Sonoff-Pow-R2-Esphome/HEAD/img/Washing-Machine-Sonoff-Pow-R2-Esphome.mp4 -------------------------------------------------------------------------------- /img/2020-04-25 22_09_59-Panoramica - Home Assistant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gio-dot/Washing-Machine-Sonoff-Pow-R2-Esphome/HEAD/img/2020-04-25 22_09_59-Panoramica - Home Assistant.png -------------------------------------------------------------------------------- /lovelace_entity_card.txt: -------------------------------------------------------------------------------- 1 | entities: 2 | - entity: sensor.sonoff_pow_voltage 3 | - entity: sensor.sonoff_pow_current 4 | - entity: sensor.sonoff_pow_power 5 | - entity: sensor.sonoff_pow_r2_total_daily_energy 6 | name: Sonoff POW Daily Energy 7 | - entity: sensor.sonoff_pow_r2_year_energy_peak 8 | name: Sonoff POW Year Energy 9 | - entity: switch.sonoff_pow_relay 10 | - entity: binary_sensor.end 11 | - entity: binary_sensor.drain 12 | - entity: binary_sensor.centrifuge 13 | - entity: binary_sensor.washing 14 | - entity: binary_sensor.run 15 | show_header_toggle: false 16 | title: Washing machine 17 | type: entities 18 | -------------------------------------------------------------------------------- /home_assistant_w_machine.yaml: -------------------------------------------------------------------------------- 1 | utility_meter: 2 | 3 | ############################################################## 4 | ## Totale annuo kWh Sonoff Pow r2 lavatrice ## 5 | ############################################################## 6 | 7 | sonoff_pow_r2_year_energy: # sensore diretto dal pow r2 esphome 8 | source: sensor.sonoff_pow_r2_total_daily_energy 9 | cycle: yearly 10 | tariffs: 11 | - peak 12 | 13 | automation: 14 | - alias: washing machine ending 15 | trigger: 16 | - platform: state 17 | entity_id: binary_sensor.run 18 | from: 'on' 19 | to: 'off' 20 | action: 21 | - service: media_player.volume_set 22 | data_template: 23 | entity_id: media_player.googlehome7895 24 | volume_level: 0.6 25 | - service: tts.google_translate_say 26 | entity_id: media_player.googlehome7895 27 | data_template: 28 | message: > 29 | La lavatrice ha finito 30 | language: 'it' 31 | cache: false 32 | - alias: washing machine start 33 | trigger: 34 | - platform: state 35 | entity_id: binary_sensor.run 36 | from: 'off' 37 | to: 'on' 38 | action: 39 | - service: media_player.volume_set 40 | data_template: 41 | entity_id: media_player.googlehome7895 42 | volume_level: 0.6 43 | - service: tts.google_translate_say 44 | entity_id: media_player.googlehome7895 45 | data_template: 46 | message: > 47 | La lavatrice ha iniziato 48 | language: 'it' 49 | cache: false 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Washing-Machine-Sonoff-Pow-R2-Esphome 2 | 3 | I use a Sonoff Pow R2 to monitor my washing machine activity in Home assistant; traditional approach to do so is to decode washing phases using Home assistant sensors/templates. Thanks to ESPHome firmware this can be easily made directly in the Sonoff Pow R2 cleaning up Home assistant configuration. 4 | Five binary sensors (RUN, WASHING, CENTRIFUGE, DRAIN, END) are automatically exposed by the Sonoff Pow to Home assistant frontend. 5 | 6 | RUN or END sensors can be easily used in Home assistant automations to send messages to Telegram or Google home etc. to warn that the cycle has ended. See Home assistant example: [home_assistant_w_machine.yaml](https://github.com/Gio-dot/Washing-Machine-Sonoff-Pow-R2-Esphome/blob/master/home_assistant_w_machine.yaml) 7 | 8 | For instructions about ESPHome installation see: https://esphome.io/index.html 9 | 10 | ### New Esphome and Home assistant code 10/06/2020 11 | Changelog: 12 | 1. Optimized washing phases detection. 13 | 2. Removed "washing" phase. 14 | 3. Optimized Home assistant yaml. 15 | 16 | Use this yaml code to create your ESPHome firmware [sonoff_pow_r2_w_machine.yaml](https://github.com/Gio-dot/Washing-Machine-Sonoff-Pow-R2-Esphome/blob/master/sonoff_pow_r2_w_machine.yaml) 17 | 18 | ## How it works 19 | 20 | 21 | This image show Home assitant card from Sonoff Pow. Washing phases are shown in sequence from bottom to top. At the end of the cycle all phases (except RUN) remains lighted. At next cycle start they are resetted. 22 | Sonoff Pow blue Led is lighted when a cycle is running and turned off at the cycle end. 23 | 24 | ## ESPHome firmware notes 25 | 26 | Line 5: `esp8266_restore_from_flash: true` to restore previous relay state after a Sonoff POW R2 power cycle. 27 | 28 | Lines 48-167: washing phases; phases detection can be optimized changing power thresholds and delay filters. 29 | 30 | ESPHome `total_daily_energy` isn't retained through Sonoff power cycles. For this reason and also to store washing machine consumption hystorical data, i used an utility meter in Home assistant Configuration feeding it with `total_daily_energy` sensor from Sonoff POW. 31 | 32 | 33 | -------------------------------------------------------------------------------- /sonoff_pow_r2_w_machine.yaml: -------------------------------------------------------------------------------- 1 | esphome: 2 | name: sonoff_pow_r2_lavatrice 3 | platform: ESP8266 4 | board: esp01_1m 5 | # esp8266_restore_from_flash: true 6 | 7 | wifi: 8 | ssid: !secret wifi_ssid 9 | password: !secret wifi_pw 10 | 11 | # Enable fallback hotspot (captive portal) in case wifi connection fails 12 | ap: 13 | ssid: "Sonoff Pow R2 Lavatrice" 14 | password: "BaOG47iOIGPC" 15 | 16 | captive_portal: 17 | 18 | # Enable logging 19 | logger: 20 | baud_rate: 0 21 | 22 | uart: 23 | rx_pin: RX 24 | baud_rate: 4800 25 | 26 | # Enable Home Assistant API 27 | api: 28 | 29 | ota: 30 | 31 | time: 32 | - platform: sntp 33 | id: my_time 34 | 35 | ################################################################################ 36 | 37 | binary_sensor: 38 | - platform: gpio 39 | pin: 40 | number: GPIO0 41 | mode: INPUT_PULLUP 42 | inverted: True 43 | name: "Sonoff POW Button" 44 | internal: true 45 | on_press: 46 | - switch.toggle: relay 47 | 48 | ################################################################################ 49 | # Machine running # 50 | ################################################################################ 51 | - platform: template 52 | name: "RUN" 53 | id: machine_running 54 | filters: 55 | - delayed_on: 60s # MINIMUM TIME TO AVOID FALSE POSITIVES 56 | - delayed_off: 300s 57 | lambda: |- 58 | if (isnan(id(power).state)) { 59 | return {}; 60 | } else if (id(power).state > 4) { 61 | // Running 62 | return true; 63 | } else { 64 | // Not running 65 | return false; 66 | } 67 | on_press: 68 | then: # at cycle start i reset all binary sensors 69 | - lambda: |- 70 | { 71 | id(machine_end).publish_state(false); 72 | id(minimum_working_time).publish_state(false); 73 | id(cycle_centrifughe).publish_state(false); 74 | id(cycle_drain).publish_state(false); 75 | } 76 | - light.turn_on: led 77 | on_release: 78 | then: # at the end of cycle turn on end cycle 79 | - lambda: |- 80 | { 81 | id(machine_end).publish_state(true); 82 | } 83 | ################################################################################ 84 | # Minimum duration cycle # 85 | ################################################################################ 86 | - platform: template 87 | name: "Washing Machine Minimum time" 88 | internal: true # minimum working time 30m 89 | id: minimum_working_time 90 | filters: 91 | - delayed_on: 30min 92 | - delayed_off: 180s 93 | lambda: |- 94 | if (isnan(id(power).state)) { 95 | return {}; 96 | } else if (id(power).state > 4) { 97 | // Running 98 | return true; 99 | } else { 100 | // Not running (le due graffe significa che non fa nulla) 101 | return {}; 102 | } 103 | 104 | ################################################################################ 105 | # Centrifuge # 106 | ################################################################################ 107 | - platform: template 108 | name: "CENTRIFUGE" 109 | id: cycle_centrifughe 110 | filters: 111 | - delayed_on: 60s 112 | # >< 90W-400W for 60s (centrifuge) 010520 changed threshold to 100W 113 | lambda: |- 114 | if (isnan(id(power).state)) { 115 | return {}; 116 | } else if ( (id(power).state > 100)&&(id(power).state < 400)&&(id(minimum_working_time).state) ){ 117 | // Running 118 | return true; 119 | } else { 120 | // Not running (le due graffe significa che non fa nulla) 121 | return {}; 122 | } 123 | ################################################################################ 124 | # Water drain # 125 | ################################################################################ 126 | - platform: template 127 | name: "DRAIN" 128 | id: cycle_drain 129 | filters: 130 | - delayed_on: 60s 131 | # >< 15W-50W for 60s (drain pump) 132 | lambda: |- 133 | if (isnan(id(power).state)) { 134 | return {}; 135 | } else if ( (id(power).state > 15) && (id(power).state < 60) && (id(cycle_centrifughe).state) ){ 136 | // Running 137 | return true; 138 | } else { 139 | // Not running (le due graffe significa che non fa nulla) 140 | return {}; 141 | } 142 | ################################################################################ 143 | # Machine end # 144 | ################################################################################ 145 | - platform: template 146 | name: "END" 147 | id: machine_end 148 | on_press: 149 | - light.turn_off: led 150 | 151 | ################################################################################ 152 | 153 | switch: 154 | - platform: gpio 155 | id: relay 156 | pin: GPIO12 157 | name: "Sonoff POW Relay" 158 | restore_mode: ALWAYS_ON 159 | 160 | output: 161 | - platform: esp8266_pwm 162 | id: pow_blue_led 163 | pin: 164 | number: GPIO13 165 | inverted: True 166 | 167 | light: 168 | - platform: monochromatic 169 | name: "Sonoff POW Blue LED" 170 | internal: true 171 | output: pow_blue_led 172 | id: led 173 | 174 | sensor: 175 | - platform: wifi_signal 176 | name: "Sonoff POW WiFi Signal" 177 | update_interval: 60s 178 | - platform: uptime 179 | name: "Sonoff POW Uptime" 180 | - platform: cse7766 181 | update_interval: 2s 182 | current: 183 | name: "Sonoff POW Current" 184 | id: curr 185 | internal: true 186 | voltage: 187 | name: "Sonoff POW Voltage" 188 | id: volt 189 | internal: true 190 | power: 191 | name: "Sonoff POW Power" 192 | id: power 193 | internal: true 194 | # on_value_range: 195 | # - above: 4.0 196 | # then: 197 | # - light.turn_on: led 198 | # - below: 3.0 199 | # then: 200 | # - delay: 60s 201 | # - light.turn_off: led 202 | - platform: total_daily_energy ############################ 203 | name: "Sonoff POW R2 Total Daily Energy" 204 | power_id: power 205 | filters: 206 | # Multiplication factor from W to kW is 0.001 207 | - multiply: 0.001 208 | unit_of_measurement: kWh 209 | accuracy_decimals: 1 210 | 211 | ################################################################################ 212 | # Slow sensors for Home assistant # 213 | ################################################################################ 214 | 215 | - platform: template ######################### 216 | name: "Sonoff Pow Current Slow" 217 | lambda: |- 218 | if (id(curr).state) { 219 | return (id(curr).state); 220 | } else { 221 | return 0; 222 | } 223 | unit_of_measurement: A 224 | icon: "mdi:alpha-a-circle" 225 | update_interval: 90s 226 | 227 | - platform: template ######################### 228 | name: "Sonoff Pow Voltage Slow" 229 | lambda: |- 230 | if (id(volt).state) { 231 | return (id(volt).state); 232 | } else { 233 | return 0; 234 | } 235 | unit_of_measurement: V 236 | icon: "mdi:alpha-v-circle" 237 | update_interval: 90s 238 | 239 | - platform: template ######################### 240 | name: "Sonoff Pow Power Slow" 241 | lambda: |- 242 | if (id(power).state) { 243 | return (id(power).state); 244 | } else { 245 | return 0; 246 | } 247 | unit_of_measurement: W 248 | icon: "mdi:alpha-w-circle" 249 | update_interval: 90s 250 | 251 | text_sensor: 252 | - platform: version 253 | name: "Sonoff POW ESPHome Version" --------------------------------------------------------------------------------