Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

esphome:

name: sonofplug01
platform: ESP8266
board: esp01_1m
board_flash_mode: dout

wifi:
ssid: !secret wifi1SSID
password: !secret wifi1pass

manual_ip:
static_ip: !secret sonofplug01_ip
gateway: !secret localgateway
subnet: !secret localsubnet
dns1: !secret localdns1
dns2: !secret localdns2

status_led:
pin:
number: GPIO13
inverted: false

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API


api:

ota:

globals:
- id: trackingon
type: bool
restore_value: no

- id: trackingoff
type: bool
restore_value: no

- id: gl_ft_start_hr
type: int
restore_value: yes
- id: gl_ft_start_mn
type: int
restore_value: yes
- id: gl_ft_start_ss
type: int
restore_value: yes
- id: gl_ft_stop_hr
type: int
restore_value: yes
- id: gl_ft_stop_mn
type: int
restore_value: yes
- id: gl_ft_stop_ss
type: int
restore_value: yes
sensor:
- platform: wifi_signal
name: "Sonoff Plug 01 Signal"
update_interval: 60s

- platform: homeassistant
entity_id: sensor.ft_start_hr
id: id_ft_start_hr

- platform: homeassistant
entity_id: sensor.ft_start_mn
id: id_ft_start_mn

- platform: homeassistant
entity_id: sensor.ft_start_ss
id: id_ft_start_ss

- platform: homeassistant
entity_id: sensor.ft_stop_hr
id: id_ft_stop_hr

- platform: homeassistant
entity_id: sensor.ft_stop_mn
id: id_ft_stop_mn

- platform: homeassistant
entity_id: sensor.ft_stop_ss
id: id_ft_stop_ss

binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
id: push_button
on_press:
then:
- if:
condition:
- switch.is_off: relay
then:
- script.stop: relayison
- script.execute: relayisoff
else:
- script.stop: relayisoff
- script.execute: relayison

- platform: status
name: "Sonoff Plug 01 Status"

- platform: template
name: "Start Time Valid"
id: start_time_valid
lambda: |-
if (id(homeassistant_time).now().second >= id(gl_ft_start_ss)) {
if (id(homeassistant_time).now().minute >= id(gl_ft_start_mn)) {
if (id(homeassistant_time).now().hour >= id(gl_ft_start_hr)) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}

- platform: template
name: "Stop Time Valid"
id: stop_time_valid
lambda: |-
if (id(homeassistant_time).now().hour > id(gl_ft_stop_hr)) {
return false;
} else if (id(homeassistant_time).now().hour == id(gl_ft_stop_hr) &&
id(homeassistant_time).now().minute > id(gl_ft_stop_mn)) {
return false;
} else if (id(homeassistant_time).now().hour == id(gl_ft_stop_hr) &&
id(homeassistant_time).now().minute == id(gl_ft_stop_mn) &&
id(homeassistant_time).now().second > id(gl_ft_stop_ss)) {
return false;
} else {
return true;
}

- platform: template
name: "Run Time Valid"
id: run_time_valid
lambda: |-
if (id(start_time_valid).state && id(stop_time_valid).state) {
return true;
} else {
return false;
}

switch:
- platform: gpio
pin: GPIO12
id: relay
restore_mode: ALWAYS_OFF

- platform: template
name: "Sonoff Plug 01"
icon: mdi:television
lambda: |-
if (id(relay).state) {
return true;
} else {
return false;
}
turn_on_action:
- script.stop: relayison
- script.execute: relayisoff
turn_off_action:
- script.stop: relayisoff
- script.execute: relayison
- platform: restart
name: "Sonoff Plug 01 Restart"

time:
- platform: homeassistant
id: homeassistant_time

interval:
- interval: 1sec
then:
- script.execute: checkingstatuson
- script.execute: checkingstatusoff
- lambda: |-
id(gl_ft_start_hr) = id(id_ft_start_hr).state;
id(gl_ft_start_mn) = id(id_ft_start_mn).state;
id(gl_ft_start_ss) = id(id_ft_start_ss).state;
id(gl_ft_stop_hr) = id(id_ft_stop_hr).state;
id(gl_ft_stop_mn) = id(id_ft_stop_mn).state;
id(gl_ft_stop_ss) = id(id_ft_stop_ss).state;

script:
- id: relayisoff
then:
- if:
condition:
binary_sensor.is_off: run_time_valid
then:
- switch.turn_on: relay
- delay: 300s
- switch.turn_off: relay

else:
- switch.turn_on: relay

- id: relayison
then:
- if:
condition:
binary_sensor.is_off: run_time_valid
then:
- switch.turn_off: relay
else:
- switch.turn_off: relay
- delay: 5s
- switch.turn_on: relay

- id: checkingstatuson
then:
- if:
condition:
and:
- switch.is_off: relay
- binary_sensor.is_on: run_time_valid
- lambda: 'return id(trackingon) == false;'
then:
- script.stop: relayisoff
- script.stop: relayison
- switch.turn_on: relay
- globals.set:
id: trackingon
value: 'true'
- globals.set:
id: trackingoff
value: 'false'

- id: checkingstatusoff
then:
- if:
condition:
and:
- switch.is_on: relay
- binary_sensor.is_off: run_time_valid
- lambda: 'return id(trackingoff) == false;'
then:
- script.stop: relayisoff
- script.stop: relayison
- switch.turn_off: relay
- globals.set:
id: trackingoff
value: 'true'
- globals.set:
id: trackingon
value: 'false'

You might also like