Hi,
I am using a PIR with 8266-01s, to activate a switch, wait for 30 seconds, then deactive the switch.
I am confused about PIR / timer re-enter problem:
If the switch is activated, and in 30 seconds, another PIR is detected again, the behaviour is strange, the delay of the switch is uncertain.
I tried to use a global variant to identify the status of in previous timer, but it does not work.
Please help, thank a lot.
Here is my template:
substitutions:
hostname: '8266-01s'
esphome:
name: '$hostname'
name_add_mac_suffix: true
# ESP 01s
esp8266:
board: esp01_1m
framework:
version: recommended
# Enable logging
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: '$hostname Hotspot'
password: "pass"
# Enable Home Assistant API
api:
password: !secret api_password
ota:
password: !secret ota_password
# Enable logging
logger:
captive_portal:
# Begin
# if the switch is turned manually
globals:
- id: gManual
type: int
restore_value: no
initial_value: '0'
- id: gInTimer
type: int
restore_value: no
initial_value: '0'
switch:
- platform: gpio
pin: GPIO0
id: gpio_0
inverted: true
- platform: template
name: '$hostname switch'
id: innerSwitch
lambda: 'return id(gManual) == 1;'
turn_on_action:
- logger.log: 'gManual ==> true'
- switch.turn_on: gpio_0
- lambda: |-
id(gManual) = 1;
turn_off_action:
- logger.log: 'gManual ==> false'
- switch.turn_off: gpio_0
- lambda: |-
id(gManual) = 0;
- platform: restart
name: '$hostname restart'
sensor:
- platform: wifi_signal
name: '$hostname WiFi Signal'
update_interval: 60s
binary_sensor:
- platform: status
name: '$hostname status'
- platform: gpio
name: '$hostname PIR'
id: 'thisPIR'
device_class: motion
pin:
number: GPIO2
mode:
input: true
pullup: true
on_state:
then:
- if:
# PIR is triggered?
condition:
binary_sensor.is_on: thisPIR
then:
- if:
# Is in manual mode?
condition:
lambda: 'return id(gManual) == 1;'
then:
logger.log: 'gManual is true, ignore PIR.'
else:
- if:
# Is still in previous timer?
condition:
lambda: 'return id(gInTimer) == 1;'
then:
- logger.log: 'Still in previous timer, ingore PIR.'
else:
- lambda: |-
id(gInTimer) = 1;
- logger.log: 'gManual is false, using PIR.'
- switch.turn_on: gpio_0
- delay: 30s
- lambda: |-
id(gInTimer) = 0;
- logger.log: 'Time is off, turn off gpio_0.'
- switch.turn_off: gpio_0