cg089
(Cg089)
August 7, 2021, 2:15pm
1
Hi,
I have a Gosund SP111 power switch on my fridge for alarm purposes.
After I found out, the power switch is always off after power loss (whch is not good on a fridge for obvious reasons) I didn’t want to store the previsous state on its flash since it wears out the flash.
However, I added:
esphome:
name: $devicename
platform: ESP8266
arduino_version: 2.5.1
board: esp8285
on_boot:
priority: -10
# ...
then:
- switch.turn_on: relay
This works, the switch is on after a power loss.
However, the status in home assistant shows the device as “off”. If I click on it, it shows as on, without triggering the switch (it just stays as is). After that, everything works normally.
How can I get the correct on/off state after power loss?
valvex
August 8, 2021, 12:19am
2
Why not set restore_mode: ALWAYS_ON
on the switch directly?
cg089
(Cg089)
August 8, 2021, 1:49pm
3
valvex:
restore_mode: ALWAYS_ON
Thats the same result, the switch is turned on but initially shows as “off”.
tom_l
August 8, 2021, 1:54pm
4
Can you show your relay config?
You could also try decreasing the priority (try -100) to make sure everything is ready so that the state can be sent to home assistant.
cg089
(Cg089)
August 8, 2021, 2:19pm
5
Yes, sure, thanks.
I will try!
# Enable Home Assistant API
api:
ota:
password: "xxx"
wifi:
ssid: "xxx"
password: "xxx"
fast_connect: on
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "xxx"
password: "xxx"
captive_portal:
substitutions:
devicename: "gosund_1"
upper_devicename: "Gosund 1"
# Higher value gives lower watt readout
current_res: "0.00244"
# Lower value gives lower voltage readout
voltage_div: "715"
esphome:
name: $devicename
platform: ESP8266
arduino_version: 2.5.1
board: esp8285
on_boot:
priority: -10
# ...
then:
- switch.turn_on: relay
# Enable logging
logger:
baud_rate: 0
# see: https://esphome.io/components/time.html
time:
- platform: homeassistant
id: homeassistant_time
# Enable Web server
web_server:
port: 80
text_sensor:
- platform: version
name: "${devicename} - Version"
icon: mdi:cube-outline
binary_sensor:
- platform: status
name: "${devicename} - Status"
device_class: connectivity
# toggle relay on/off
- platform: gpio
pin:
number: GPIO13
mode: INPUT_PULLUP
inverted: True
id: "${devicename}_button_state"
on_press:
- switch.toggle: button_switch
sensor:
- platform: wifi_signal
name: "${devicename} - Wifi Signal"
update_interval: 60s
icon: mdi:wifi
- platform: uptime
name: "${devicename} - Uptime"
update_interval: 60s
icon: mdi:clock-outline
- platform: total_daily_energy
name: "${devicename} - Todays Usage"
power_id: "power_wattage"
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
unit_of_measurement: kWh
icon: mdi:calendar-clock
- platform: adc
pin: VCC
name: "${devicename} - VCC Volt"
icon: mdi:flash-outline
- platform: hlw8012
sel_pin:
number: GPIO12
inverted: True
cf_pin: GPIO05
cf1_pin: GPIO04
change_mode_every: 4
current_resistor: ${current_res}
voltage_divider: ${voltage_div}
update_interval: 3s
current:
name: "${devicename} - Ampere"
unit_of_measurement: A
accuracy_decimals: 3
icon: mdi:current-ac
voltage:
name: "${devicename} - Volt"
unit_of_measurement: V
accuracy_decimals: 1
icon: mdi:flash-outline
power:
name: "${devicename} - Watt"
unit_of_measurement: W
id: "power_wattage"
icon: mdi:gauge
status_led:
pin:
number: GPIO02
inverted: True
id: led_blue
output:
- platform: gpio
pin: GPIO00
inverted: true
id: led_red
switch:
- platform: template
name: "${devicename} - Switch"
icon: mdi:power
optimistic: true
id: button_switch
turn_on_action:
- switch.turn_on: relay
turn_off_action:
- switch.turn_off: relay
- platform: gpio
pin: GPIO15
id: relay
# restore_mode: ALWAYS_ON
# tried that one as well with same result
cg089
(Cg089)
August 8, 2021, 2:22pm
6
Still shows as off with -100
tom_l
August 8, 2021, 2:26pm
7
Your switch has no state feedback from the relay. That’s why it is getting out of sync. Instead of using optimistic: true
use a state template:
switch:
- platform: template
name: "${devicename} - Switch"
icon: mdi:power
lambda: |-
if (id(relay).state) {
return true;
} else {
return false;
}
id: button_switch
turn_on_action:
- switch.turn_on: relay
turn_off_action:
- switch.turn_off: relay
Now the switch knows the state of the relay if something other than the switch changes it (like your boot sequence).
2 Likes
cg089
(Cg089)
August 8, 2021, 2:32pm
8
I understand. Thanks for explaining!
That does the trick!
1 Like