Skip to content

Instantly share code, notes, and snippets.

@agozie
Forked from MatthewFlamm/Alarm system.md
Created January 21, 2023 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agozie/4e35c64c034a31de40a04ca15605fba4 to your computer and use it in GitHub Desktop.
Save agozie/4e35c64c034a31de40a04ca15605fba4 to your computer and use it in GitHub Desktop.
Esp32 home alarm system with home assistant and esphome

Alarm system using esp32 with esphome and home assistant

# secrets inside templates not allowed
# must use hard coded 'code' on home assistant panel
# esphome code does not need to match HA code
alarm_control_panel:
- platform: template
panels:
home_alarm:
value_template: "{{ states('sensor.alarm_condition') }}"
arm_home:
- condition: template
value_template: "{{ code == 'MYCODE' }}"
- service: esphome.alarm_system_arm_home
data:
code: !secret esp_alarm_code
disarm:
- condition: template
value_template: "{{ code == 'MYCODE' }}"
- service: esphome.alarm_system_disarm
data:
code: !secret esp_alarm_code
substitutions:
alarm_code: !secret alarm_code
disarmed_id: "0"
pending_id: "1"
triggered_id: "2"
armed_home_id: "3"
esphome:
name: alarm_system
# restore state
# if trigger sequence reset to armed for now.
# should it retrigger sequence?
on_boot:
then:
- lambda: |-
if (id(state_int) == ${disarmed_id}) {
id(alarm_condition).publish_state("disarmed");
} else if (id(state_int) == ${pending_id}){
id(alarm_condition).publish_state("armed_home");
id(state_int) = ${armed_home_id};
} else if (id(state_int) == ${triggered_id}){
id(alarm_condition).publish_state("armed_home");
id(state_int) = ${armed_home_id};
} else if (id(state_int) == ${armed_home_id}){
id(alarm_condition).publish_state("armed_home");
} else{
id(alarm_condition).publish_state("disarmed");
id(state_int) = ${disarmed_id};
}
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: !secret alarm_system_ip
gateway: !secret wifi_gateway
subnet: !secret wifi_subnet
sensor:
- platform: uptime
name: Uptime Sensor
# Enable logging
logger:
# Enable Home Assistant API
api:
password: !secret api_password
# add arm and disarm services to ha
services:
- service: arm_home
variables:
code: string
then:
- if:
condition:
lambda: "return code == \"${alarm_code}\";"
then:
- script.execute: alarm_arm
- service: disarm
variables:
code: string
then:
- if:
condition:
lambda: "return code == \"${alarm_code}\";"
then:
- script.execute: alarm_disarm
ota:
password: !secret api_password
# global for state restore
globals:
- id: state_int
type: int
restore_value: yes
initial_value: '0'
binary_sensor:
- platform: gpio
pin:
number: 32
mode: INPUT_PULLUP
name: "Motion Detector Stairs Living Room"
device_class: motion
on_press:
then:
script.execute: trigger_alarm
- platform: gpio
pin:
number: 25
mode: INPUT_PULLUP
name: "Motion Detector Playroom"
device_class: motion
on_press:
then:
script.execute: trigger_alarm
- platform: gpio
pin:
number: 19
mode: INPUT_PULLUP
name: "Front Door"
device_class: door
on_press:
then:
script.execute: trigger_alarm
- platform: gpio
pin:
number: 23
mode: INPUT_PULLUP
name: "Sliding Door"
device_class: door
on_press:
then:
script.execute: trigger_alarm
- platform: gpio
pin:
number: 26
mode: INPUT_PULLUP
name: "Garage Door"
device_class: door
on_press:
then:
script.execute: trigger_alarm
- platform: gpio
pin:
number: 2
mode: INPUT_PULLUP
name: "Basement Window"
device_class: door
on_press:
then:
script.execute: trigger_alarm
output:
- platform: gpio
pin: 22
id: output_buzzer_front_door
# warning chirp when pending
switch:
- platform: template
turn_on_action:
- switch.template.publish:
id: buzzer_front_door
state: ON
- while:
condition:
switch.is_on: buzzer_front_door
then:
- output.turn_on: output_buzzer_front_door
- delay: 10ms
- output.turn_off: output_buzzer_front_door
- delay: 990ms
turn_off_action:
- switch.template.publish:
id: buzzer_front_door
state: OFF
- output.turn_off: output_buzzer_front_door
id: buzzer_front_door
# holds alarm condition for arm/disarm/pending/triggered
text_sensor:
- platform: template
name: "Alarm Condition"
id: "alarm_condition"
script:
# when arming and disarming, turn off all sirens and buzzers
# also, cancel any trigger sequence
- id: alarm_arm
then:
- text_sensor.template.publish:
id: alarm_condition
state: "armed_home"
- script.stop: trigger_alarm_execute
- switch.turn_off: buzzer_front_door
- lambda: |-
id(state_int) = ${armed_home_id};
- id: alarm_disarm
then:
- text_sensor.template.publish:
id: alarm_condition
state: "disarmed"
- script.stop: trigger_alarm_execute
- switch.turn_off: buzzer_front_door
- lambda: |-
id(state_int) = ${disarmed_id};
# triggering sequence
- id: trigger_alarm_execute
then:
- text_sensor.template.publish:
id: alarm_condition
state: "pending"
- switch.turn_on: buzzer_front_door
- lambda: |-
id(state_int) = ${pending_id};
- delay: 30s
- text_sensor.template.publish:
id: alarm_condition
state: "triggered"
- switch.turn_off: buzzer_front_door
- lambda: |-
id(state_int) = ${triggered_id};
- delay: 3600s
# TODO: turn on siren
# only execute triggering if armed and not already running
- id: trigger_alarm
then:
if:
condition:
and:
- not:
script.is_running: trigger_alarm_execute
- lambda: |-
return id(state_int) == ${armed_home_id};
then:
script.execute: trigger_alarm_execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment