Advertisement
FALLINGAWAY38

Wiegand/Keypad Esphome

Dec 15th, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 20.55 KB | None | 0 0
  1. esphome:
  2.   name: keypad
  3.   friendly_name: keypad
  4.  
  5. esp32:
  6.   board: esp32dev
  7.   framework:
  8.     type: arduino
  9.  
  10. # Enable logging
  11. logger:
  12. # Enable Home Assistant API
  13. api:
  14.   encryption:
  15.     key: "6c7jRAZCFNm9iaRuXNIujFbwB3bst0Ple8lRVGWjp/w="
  16.  
  17. ota:
  18.   - platform: esphome
  19.     password: "49f36bca21d4d0a8609cd3fa297e6e73"
  20.  
  21. wifi:
  22.   on_connect:
  23.     then:
  24.      - text_sensor.template.publish:
  25.         id: last_user
  26.         state: !lambda |-
  27.           return id(last_user_access).value;
  28.  
  29.        ###  - dfplayer.play: !lambda return atoi(x.c_str());  ###
  30.  
  31.   networks:
  32.     - ssid: linksys-barn
  33.       password: "XXXXXX"
  34.  
  35.     - ssid: linksys-baker
  36.       password: "XXXXXX"
  37.  
  38.   manual_ip:
  39.     static_ip: 192.168.0.186
  40.     gateway: 192.168.0.1
  41.     subnet: 255.255.255.0
  42.     dns1: 192.168.0.1    
  43.     dns2: 192.168.0.194
  44.  
  45.   # Enable fallback hotspot (captive portal) in case wifi connection fails
  46.   #ap:      
  47.   #  ssid: "Keypad"
  48.   #  password: "XXXXXX"
  49.  
  50. captive_portal:
  51.  
  52. web_server:
  53.   port: 80
  54.   version: 3
  55.   auth:
  56.     username: "Justin"
  57.     password: "XXXXXX"
  58.  
  59. ################ Guest Mode Timers  #############
  60. #
  61. # Use both Start and Stop timers to specify which times of the day are allowed for the guest.
  62. #
  63. # Example: Start Time 08:00:00Am - Stop Time 05:00:00Pm Will Only Allow Access between Those Times (9 Hours)
  64.  
  65. datetime:
  66.  - platform: template
  67.    id: guest_mode_start_time
  68.    type: time
  69.    name: "Guest Mode Time On"
  70.    optimistic: yes
  71.    initial_value: "08:00:00"
  72.    restore_value: true
  73.    on_time:
  74.      - if:
  75.         condition:
  76.           - switch.is_on: guest_active    
  77.         then:
  78.           - switch.turn_on: guest_active
  79.  
  80.  - platform: template
  81.    id: guest_mode_off_time
  82.    type: time
  83.    name: "Guest Mode Time Off"
  84.    optimistic: yes
  85.    initial_value: "05:00:00"
  86.    restore_value: true
  87.    on_time:
  88.      - if:    
  89.         condition:
  90.           and:
  91.             - text_sensor.state:
  92.                 id: ha_cover_state
  93.                 state: open
  94.  
  95.             - switch.is_on: guest_active
  96.         then:
  97.           - button.press: transmit_rf
  98.         else:
  99.           - switch.turn_off: guest_active
  100.  
  101. number:
  102.   - platform: template
  103.     name: "Set New Code"  
  104.     id: new_keypad_code
  105.     min_value: 0
  106.     max_value: 9999
  107.     step: 1
  108.     mode: box
  109.     restore_value: false
  110.     optimistic: true
  111.     on_value:
  112.       if:
  113.         condition:
  114.           - switch.is_on: guest_active
  115.         then:
  116.           - globals.set:
  117.               id: temp_code
  118.               value: !lambda 'return x;'
  119.    
  120.  
  121.  
  122.  
  123.  
  124. sun:
  125.    latitude: 40.173568
  126.    longitude: -86.0225536
  127.    on_sunset:
  128.     - light.turn_on:
  129.         id: barn_overhead_led
  130.         brightness: 50%
  131.         white: 100%
  132.     - switch.turn_on:
  133.         id: outside_barn_lights
  134.      
  135.        
  136.    on_sunrise:
  137.     - light.turn_off:
  138.         id: barn_overhead_led
  139.      
  140.     - switch.turn_off:
  141.         id: outside_barn_lights
  142.  
  143. time:
  144.   - platform: homeassistant  
  145.     id: homeassistant_time
  146.  
  147.                                            
  148. remote_transmitter:
  149.   pin: 4
  150.   carrier_duty_percent: 100%  
  151.  
  152. button:
  153.   - platform: template
  154.     name: "test send number"
  155.     id: test_keysend
  156.     on_press:
  157.       then:
  158.         - lambda: |-
  159.             id(pincode_reader).send_key('1');
  160.         - delay: 250ms
  161.      
  162.         - lambda: |-
  163.             id(pincode_reader).send_key('2');
  164.         - delay: 250ms
  165.    
  166.         - lambda: |-
  167.             id(pincode_reader).send_key('3');
  168.         - delay: 250ms
  169.  
  170.         - lambda: |-
  171.             id(pincode_reader).send_key('4');
  172.         - delay: 50ms
  173.          
  174.         - lambda: |-
  175.             id(pincode_reader).send_key('#');
  176.  
  177.   - platform: template
  178.     id: transmit_rf
  179.     name: "Overhead Door RF"
  180.     on_press:
  181.       then:
  182.         - remote_transmitter.transmit_rc_switch_raw:
  183.             code: '00101001100111110101101'          ####  Test Code Only  '00100100100001'  #######      
  184.             protocol: 1  
  185.             repeat:
  186.               times: 1
  187.               wait_time: 0ms  
  188.  
  189.   - platform: restart
  190.     id: keypad_restart
  191.     name: "Keypad Restart"
  192.  
  193.   - platform: safe_mode
  194.     id: keypad_safemode
  195.     name: "Keypad (Safe Mode)"          
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. switch:
  203.   - platform: template
  204.     assumed_state: False
  205.     id: reset_password
  206.     name: "Reset Keypad Pin"
  207.  
  208.  
  209.   - platform: template
  210.     name: "Auto-Restart 2x"
  211.     id: auto_restart_keypad
  212.     optimistic: true
  213.     restore_mode: RESTORE_DEFAULT_ON
  214.  
  215.   - platform: template
  216.     id: guest_active
  217.     name: "Guest Mode"
  218.     optimistic: True  
  219.     restore_mode: DISABLED      
  220.     on_turn_off:
  221.       then:
  222.         - number.set:
  223.             id: new_keypad_code
  224.             value: 0
  225.  
  226.   - platform: template  
  227.     id: ovrhead
  228.     optimistic: true
  229.     on_turn_on:
  230.       - homeassistant.service:
  231.           service: switch.toggle
  232.           data:
  233.            entity_id: switch.overhead_door_rf_barn_overhead_door
  234.  
  235.   - platform: gpio
  236.     pin:      
  237.       number: 25
  238.     #mode:
  239.       inverted: true
  240.     name: 'Barn Door Lights'
  241.     id: outside_barn_lights
  242.     #optimistic: true
  243.  
  244.   - platform: gpio
  245.     name: "Barn Switch 1"
  246.     id: barn_switch1
  247.     pin:    
  248.       number: 15
  249.       inverted: false
  250.     device_class: switch
  251.  
  252.  
  253.  
  254.   - platform: gpio
  255.     name: "Barn Switch 2"
  256.     id: barn_switch2
  257.     pin:    
  258.       number: 5
  259.       inverted: false
  260.     device_class: switch
  261.  
  262.   - platform: template
  263.     name: "Barn Lights"
  264.     id: barn_inside_lights
  265.     optimistic: true
  266.     restore_mode: RESTORE_DEFAULT_OFF
  267.    
  268.     turn_on_action:
  269.        - switch.turn_on: barn_switch1
  270.        - switch.turn_on: barn_switch2
  271.  
  272.     turn_off_action:
  273.        - switch.turn_off: barn_switch1
  274.        - switch.turn_off: barn_switch2
  275.      
  276.              
  277.    
  278.  
  279.  
  280. binary_sensor:      
  281.  
  282.   - platform: gpio
  283.     name: "Small Barn Door"
  284.     pin:    
  285.       number: 12      
  286.       mode:
  287.         input: true
  288.         pullup: true
  289.       inverted: true
  290.     id: barn_door_small
  291.     device_class: door  
  292.  
  293. #  - platform: gpio
  294. #    name: "Light Switch1"
  295. #    id: barn_switch_sensor1
  296. #    pin:      
  297. #      number: 23
  298. #      mode:
  299. #        input: true
  300. #        pullup: true
  301. #      inverted: False
  302. #    filters:
  303. #      - settle: 1.5s
  304. #      - delayed_on_off: 1.5s          
  305. #    on_state:          
  306. #      - if:
  307. #          condition:          
  308. #            - switch.is_off:
  309. #                id: barn_switch1          
  310. #          then:
  311. #            - switch.turn_on:
  312. #                id: barn_switch1
  313. #          else:                        
  314. #            - switch.turn_off: barn_switch1
  315.              
  316.              
  317.  
  318.           #      - switch.template.publish:
  319.           #          id: barn_inside_lights
  320.           #          state: OFF
  321.          
  322.  
  323.  
  324.   - platform: gpio
  325.     name: "Light Switch2"
  326.     id: barn_switch_sensor2
  327.     pin:      
  328.       number: 14
  329.       mode:
  330.         input: true
  331.         pullup: true
  332.       inverted: true
  333.     filters:
  334.       - settle: 1.5s
  335.       - delayed_on_off: 1.5s
  336.     on_state:        
  337.       then:
  338.         - switch.toggle: barn_switch1
  339.         - switch.toggle: barn_switch2
  340.  
  341.   - platform: status
  342.     id: keypad_status
  343.     name: Barn Keypad Status  
  344.  
  345.  
  346.  
  347. text_sensor:
  348.  
  349.   - platform: sun
  350.     name: Next Sunrise
  351.     type: sunrise
  352.     id: next_sunrise1
  353.  
  354.   - platform: sun
  355.     name: Next Sunset
  356.     type: sunset
  357.     id: next_sunset
  358.  
  359.   - platform: template
  360.     name: "Current Time"
  361.     id: current_time
  362.     update_interval: 5s
  363.     lambda: return  id(homeassistant_time).utcnow();             # .now();           #######  .now().strftime("%I%M%p");  #######
  364.  
  365.   - platform: homeassistant
  366.     entity_id: cover.overhead_door_rf_overhead
  367.     id: ha_cover_state
  368.     name: "Overhead Door"
  369.     internal: false                    
  370.                
  371.  
  372.   - platform: template
  373.     name: Uptime Wg26
  374.     id: uptime_human_wg26
  375.     icon: mdi:clock-start
  376.  
  377.   - platform: wifi_info
  378.     ip_address:
  379.       name: Keypad IP Address
  380.     ssid:
  381.       name: Keypad Connected SSID    
  382.    # mac_address:
  383.     #  name: Keypad Mac Address
  384.     scan_results:
  385.       name: Keypad Latest Scan Results    
  386.     dns_address:
  387.       name: "dns used"
  388.        
  389.  
  390.  
  391.  
  392.   - platform: template
  393.     name: "Door Access Method"
  394.     id: door_access_method                        
  395.  
  396.   - platform: template
  397.     name: "Alarm State"
  398.     id: alarm_state
  399.     icon: mdi:account-multiple
  400.  
  401.   - platform: template
  402.     name: Last User
  403.     id: last_user
  404.     icon: mdi:clock-start  
  405.    
  406.          
  407.      
  408.  
  409.  
  410.  
  411. globals:
  412.   - id: method
  413.     type: std::string
  414.     restore_value: yes
  415.     max_restore_data_length: 13
  416.     initial_value: ""
  417.  
  418.   # Example for global string variable
  419.  
  420.   - id: last_user_access
  421.     type: std::string
  422.     restore_value: yes
  423.     max_restore_data_length: 13
  424.     initial_value: ""
  425.  
  426.   - id: temp_code
  427.     type: int
  428.     restore_value: yes
  429.     max_restore_data_length: 4
  430.     initial_value: ""
  431.  
  432.  
  433.  
  434. sensor:
  435.  
  436.   - platform: duty_time
  437.     id: small_door_open
  438.     name: "Door Open Time"
  439.     # Support logical sources (optional): 'binary_sensor'
  440.     sensor: barn_door_small        
  441.     restore: true                                      # Sensor for last turn-on time (optional)
  442.     last_time:
  443.       name: "Last Time Door Open"
  444.  
  445.   - platform: template
  446.     name: Keypad Code
  447.     id: keyCode    
  448.     on_value:  
  449.       - if:
  450.          condition:
  451.            - lambda: 'return id(keyCode).state == 1050;'
  452.          then:
  453.            - lambda: |-
  454.                   {        
  455.                   id(ovrhead).toggle();
  456.                   id(last_user).publish_state("Justin");  
  457.                   id(door_access_method ).publish_state("Pin Code");
  458.                   }                    
  459.                      
  460.       - if:
  461.           condition:
  462.            
  463.               - lambda: 'return id(keyCode).state != 1050;'
  464.               - lambda: 'return id(keyCode).state != 1955;'
  465.               - lambda: 'return id(keyCode).state != 6800;'
  466.               - lambda: 'return id(keyCode).state != 2014;'
  467.               - lambda: 'return id(keyCode).state != id(temp_code);'
  468.           then:
  469.              - text_sensor.template.publish:    
  470.                 id: last_user
  471.                 state: "Invalid Code"
  472.  
  473.       - if:
  474.           condition:
  475.             - lambda: 'return id(keyCode).state == 6800;'
  476.           then:
  477.             - lambda: |-
  478.                   {            
  479.                   id(ovrhead).toggle();
  480.                   id(last_user).publish_state("Paula");
  481.                   id(door_access_method ).publish_state("Pin Code");
  482.                   }
  483.              
  484.                    
  485.       - if:
  486.           condition:
  487.             - lambda: 'return id(keyCode).state == 1955;'
  488.           then:
  489.              - lambda: |-
  490.                   {            
  491.                   id(ovrhead).toggle();
  492.                   id(last_user).publish_state("Mike");
  493.                   id(door_access_method ).publish_state("Pin Code");
  494.                   }
  495.  
  496.              
  497.                    
  498.       - if:
  499.           condition:
  500.             - lambda: 'return id(keyCode).state == 2014;'
  501.           then:
  502.              - lambda: |-
  503.                   {            
  504.                   id(ovrhead).toggle();
  505.                   id(last_user).publish_state("Olive");
  506.                   id(door_access_method ).publish_state("Pin Code");
  507.                   }
  508.  
  509.       - if:
  510.           condition:
  511.             and:
  512.               - switch.is_on: guest_active
  513.               - lambda: 'return id(keyCode).state == id(temp_code);'
  514.           then:
  515.             - logger.log: "KeyCode equals temp code!"      
  516.             - text_sensor.template.publish:
  517.                id: last_user
  518.                state: "Temp. Code"                
  519.  
  520.                
  521.            #  - lambda: |-
  522.             #     if (x == "6789"); {            
  523.              #     id(ovrhead).toggle();
  524.               #    id(last_user).publish_state("Temp. Code");
  525.                #   }
  526.                  # id(last_user_access) = "Temp Code";
  527.                                                      
  528.      
  529.       - if:
  530.           condition:
  531.             and:
  532.             # - alarm_control_panel.is_armed: acp1
  533.               - lambda: 'return id(keyCode).state == 6753957;'
  534.           then:
  535.            # - alarm_control_panel.disarm:
  536.             #    id: acp1
  537.              #   code: "2014"
  538.             - delay: 1s    
  539.             - switch.toggle: ovrhead
  540.  
  541.             - text_sensor.template.publish:
  542.                 id: last_user
  543.                 state: "Justin Tag"
  544.            
  545.  
  546.             - text_sensor.template.publish:    
  547.                 id: alarm_state
  548.                 state: "Disarmed By Justin"
  549.                
  550.            
  551.           else:
  552.         #   if:
  553.           #    condition:
  554.            #     not:
  555.             #      - alarm_control_panel.is_armed: acp1
  556.              # then:
  557.                 if:
  558.                   condition:                  
  559.                       - lambda: 'return id(keyCode).state == 6753957;'
  560.                   then:  
  561.                      - lambda: |-
  562.                               {        
  563.                               id(ovrhead).toggle();
  564.                               id(last_user).publish_state("Justin");  
  565.                               id(door_access_method).publish_state("RFID Tag");
  566.                               }                          
  567.                              
  568.                                                  
  569.  
  570.  
  571.  
  572.       - if:
  573.             condition:
  574.             - lambda: 'return id(keyCode).state == 6422842;'
  575.             then:  
  576.                      - lambda: |-
  577.                               {        
  578.                               id(ovrhead).toggle();
  579.                               id(last_user).publish_state("Paula");
  580.                               id(door_access_method ).publish_state("RFID Tag");
  581.                               }                          
  582.  
  583.       - if:
  584.           condition:
  585.             - lambda: 'return id(keyCode).state == 6491970;'
  586.           then:  
  587.                      - lambda: |-
  588.                               {        
  589.                               id(ovrhead).toggle();
  590.                               id(last_user).publish_state("Mike");
  591.                               id(door_access_method ).publish_state("RFID Tag");
  592.                               }
  593.                                                    
  594.  
  595.       - if:
  596.           condition:
  597.             - lambda: 'return id(keyCode).state == 5553549;'
  598.           then:  
  599.                      - lambda: |-
  600.                                {        
  601.                                id(ovrhead).toggle();
  602.                                id(last_user).publish_state("Golf Cart Key1");
  603.  
  604.                                }
  605.                                      
  606.          
  607.       - if:
  608.           condition:
  609.             - lambda: 'return id(keyCode).state == 5627758;'
  610.           then:
  611.                      - lambda: |-
  612.                                {
  613.                                id(ovrhead).toggle();
  614.                                id(last_user).publish_state("Golf Cart Key2");
  615.                                }
  616.      
  617.          
  618.      
  619.  
  620.      
  621.  
  622.  
  623.  
  624.   - platform: wifi_signal    
  625.     id: wifi_signal_db
  626.     update_interval: 300s
  627.     entity_category: "diagnostic"
  628.     internal: true
  629.  
  630.   - platform: copy
  631.     source_id: wifi_signal_db
  632.     name: "WiFi Signal Keypad"
  633.     filters:
  634.       - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
  635.     unit_of_measurement: "Signal %"
  636.     entity_category: "diagnostic"
  637.     id: wifiSignalWG26
  638.  
  639.   - platform: uptime #Uptime in Seconds
  640.     name: Barn Keypad Uptime
  641.     id: uptime_sensor_wiegand
  642.     update_interval: 240s
  643.     internal: True
  644.     on_raw_value:
  645.       then:
  646.         - text_sensor.template.publish:
  647.             id: uptime_human_wg26
  648.             state: !lambda |-
  649.               int seconds = round(id(uptime_sensor_wiegand).raw_state);
  650.               int days = seconds / (24 * 3600);
  651.               seconds = seconds % (24 * 3600);
  652.               int hours = seconds / 3600;
  653.               seconds = seconds % 3600;
  654.               int minutes = seconds /  60;
  655.               seconds = seconds % 60;
  656.               return (
  657.                 (days ? String(days) + "d " : "") +
  658.                 (hours ? String(hours) + "h " : "") +
  659.                 (minutes ? String(minutes) + "m " : "") +
  660.                 (String(seconds) + "s")
  661.               ).c_str();
  662.  
  663. wiegand:
  664.   - id: mykeypad
  665.     d0: 21 ## Grn
  666.     d1: 19 ## Wht
  667.     on_key:
  668.       - lambda: ESP_LOGI("KEY", "received key %d", x);
  669.     on_tag:
  670.       - lambda: ESP_LOGI("TAG", "received tag %s", x.c_str());
  671.       - sensor.template.publish:
  672.          id: keyCode
  673.          state: !lambda "return parse_number<float>(x).value();"  
  674.       - if:
  675.           condition:    
  676.             and:
  677.               - lambda: 'return id(keyCode).state != 5553549;'
  678.               - lambda: 'return id(keyCode).state != 6422842;'
  679.               - lambda: 'return id(keyCode).state != 6491970;'
  680.               - lambda: 'return id(keyCode).state != 6753957;'
  681.    #          - lambda: 'return id(keyCode).state != #######;'
  682.    #          - lambda: 'return id(keyCode).state != #######;'
  683.    #          - lambda: 'return id(keyCode).state != #######;'
  684.                
  685.           then:
  686.             - text_sensor.template.publish:
  687.                 id: last_user
  688.                 state: "Invalid Tag Scanned"
  689.          
  690.     on_raw:
  691.       - lambda: ESP_LOGI("RAW", "received raw %d bits, value %llx", bits, value);
  692.  
  693. key_collector:
  694.   - id: pincode_reader
  695.     source_id: mykeypad
  696.     min_length: 4
  697.     max_length: 5
  698.     end_keys: "#"
  699.     end_key_required: true
  700.     clear_keys: "*"
  701.     allowed_keys: "0123456789"
  702.     timeout: 5s
  703.     on_progress:
  704.       - logger.log:
  705.           format: "input progress: '%s', started by '%c'"
  706.           args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
  707.     on_result:    
  708.       then:
  709.         - sensor.template.publish:
  710.             id: keyCode
  711.             state: !lambda "return parse_number<float>(x).value();"
  712.        
  713.     on_timeout:
  714.       - logger.log:
  715.           format: "input timeout: '%s', started by '%c'"
  716.           args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]    
  717.  
  718.  
  719.  
  720. # D1
  721.  
  722.  
  723. light:
  724.   - platform: neopixelbus
  725.     type: RGB
  726.     variant: WS2811
  727.     pin: 17
  728.     num_leds: 20
  729.     id: barn_overhead_led
  730.     name: "Barn Door LED Strip"
  731.    
  732.    
  733. select:
  734.   - platform: template
  735.     id: barn_led_effect
  736.     name: "Barn Light Effect"
  737.     options:
  738.       - "R/W/B"
  739.       - "B/R/W"
  740.       - ""
  741.     initial_option: ""
  742.     optimistic: True
  743.     on_value:
  744.       then:
  745.         if:
  746.           condition:
  747.             - lambda: 'return id(barn_led_effect).state == "R/W/B";'
  748.           then:
  749.             - light.addressable_set:
  750.                 id: barn_overhead_led
  751.                 range_from: 0
  752.                 range_to: 7
  753.                 red: 100%
  754.                 green: 0%
  755.                 blue: 0%
  756.                 color_brightness: 100%
  757.  
  758.             - light.addressable_set:
  759.                 id: barn_overhead_led
  760.                 range_from: 8
  761.                 range_to: 14
  762.                 red: 0%
  763.                 green: 0%
  764.                 blue: 0%
  765.                 white: 100%
  766.                 color_brightness: 100%
  767.  
  768.             - light.addressable_set:
  769.                 id: barn_overhead_led
  770.                 range_from: 15
  771.                 range_to: 20
  772.                 red: 0%
  773.                 green: 0%
  774.                 blue: 100%
  775.                 color_brightness: 100%
  776.                
  777.  
  778.                
  779.  
  780.  
  781. interval:
  782.  - interval: 1min
  783.    then:
  784.      - if:
  785.          condition:
  786.            and:
  787.              - sun.is_below_horizon:
  788.              - light.is_off:
  789.                  id: barn_overhead_led
  790.          then:
  791.            - light.turn_on:
  792.                id: barn_overhead_led
  793.                brightness: 75%
  794.                red: 0%
  795.                green: 0%
  796.                blue: 100%
  797.  
  798.      - if:
  799.          condition:
  800.            and:        
  801.              - sun.is_above_horizon:
  802.              - light.is_on:
  803.                  id: barn_overhead_led
  804.          then:
  805.            - light.turn_off:
  806.                id: barn_overhead_led    
  807.  
  808.      - if:
  809.          condition:
  810.            and:        
  811.              - sun.is_above_horizon:
  812.              - switch.is_on:
  813.                  id: outside_barn_lights
  814.          then:
  815.            - switch.turn_off:
  816.                id: outside_barn_lights                          
Tags: wiegand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement