esphome:
libraries:
- ESP8266WiFi
- https://github.com/akaJes/AsyncPing#95ac7e4
external_components:
- source:
type: local
path: ./components
globals:
- id: global_network_error_s
type: int
restore_value: False
initial_value: '0'
sensor:
- platform: ping
# IP address of the target
ip_address: 114.114.114.114
# number of packets to send
num_attempts: 10
# the timeout. however, this is not what you usually expect from `ping`
# implementation: the timeout is also the interval to send packets. if you
# set this value to 10 sec, and the network is fine (no packet loss), then
# the component sends a packet at 10 sec interval, and the total time to
# finish would be 10 sec * num_attempts = 10 * 17 = 170 sec.
timeout: 10sec
loss:
# the name to be shown.
name: Packet loss
id: packet_loss
force_update: True
latency:
# the name to be shown.
name: Latency
id: latency
# this should be 3 as the value is float, unit is sec, and the raw
# values are in ms.
accuracy_decimals: 3
force_update: True
# the interval for checking the sensors. defaults to 60s.
update_interval: 130s
interval:
- interval: 10s
then:
- if:
condition:
- and:
- wifi.connected:
- lambda: "return id(global_restart_when_disconnected_index) == 1;"
- lambda: "return id(packet_loss).state == 100;"
then:
- if:
condition:
lambda: "return id(global_network_error_s) == 0;"
then:
- logger.log: "network error, restart ..................."
- switch.turn_off: relay1
- delay: 10s
- switch.turn_on: relay1
- globals.set:
id: global_network_error_s
value: !lambda return id(global_network_error_s) + 1;
- if:
condition:
lambda: "return id(global_network_error_s) >= 60;"
then:
- globals.set:
id: global_network_error_s
value: !lambda return 0;
else:
- globals.set:
id: global_network_error_s
value: !lambda return 0;
代码实现每隔约2分钟检测一次网络,如果断网了,关闭继电器,10秒后再开启继电器,然后10分钟内(10s x 60次 = 10分钟)不再处理,10分钟后如果仍然断网,再关闭继电器然后再打开,如此重复。