From b9dde9ad51d6bfb9488bc1740acd2cff8eae47bd Mon Sep 17 00:00:00 2001
From: Jeffrey Stone <thejeffreystone@gmail.com>
Date: Fri, 21 Aug 2020 23:28:38 -0400
Subject: [PATCH] Trying my hardware lightning detector

---
 config/binary_sensors/general.yaml |  24 +++++
 config/packages/weather.yaml       | 156 +++++++++++++++--------------
 config/sensors/weather.yaml        |  79 ++++++++++-----
 3 files changed, 157 insertions(+), 102 deletions(-)
 create mode 100755 config/binary_sensors/general.yaml

diff --git a/config/binary_sensors/general.yaml b/config/binary_sensors/general.yaml
new file mode 100755
index 0000000..cf1777d
--- /dev/null
+++ b/config/binary_sensors/general.yaml
@@ -0,0 +1,24 @@
+- platform: mqtt
+  name: "Lightning Detected" 
+  state_topic: "house/acurite-6045M/lightning_active"
+  payload_on: "1"
+  payload_off: "0"
+  device_class: safety
+- platform: mqtt
+  name: "Back Porch Sensor Battery" 
+  state_topic: "house/acurite-6045M/battery"
+  payload_on: "1"
+  payload_off: "0"
+  device_class: battery
+- platform: mqtt
+  name: "Garage Sensor Battery" 
+  state_topic: "house/acurite-10968/battery"
+  payload_on: "1"
+  payload_off: "0"
+  device_class: battery
+- platform: mqtt
+  name: "Crawlspace Sensor Battery" 
+  state_topic: "house/acurite-158/battery"
+  payload_on: "1"
+  payload_off: "0"
+  device_class: battery
\ No newline at end of file
diff --git a/config/packages/weather.yaml b/config/packages/weather.yaml
index 179b228..d0139f2 100755
--- a/config/packages/weather.yaml
+++ b/config/packages/weather.yaml
@@ -36,9 +36,9 @@ input_boolean:
   weather_alert_texts:
     name: Weather Alert Texts
     icon: mdi:telegram
-  # lightning_warning:
-  #   name: Lightning Warning
-  #   icon: mdi:flash-alert
+  lightning_warning:
+    name: Lightning Warning
+    icon: mdi:flash-alert
 
 ### You first need to find either your NWS Zone ID or County ID. I’m not sure which is better but I used my Zone ID here.
 ### You can find your Zone ID by going to https://alerts.weather.gov/ 7, scroll down to your state and click on the “zone list” then look for the entry for your county.
@@ -146,7 +146,6 @@ sensor:
 
 automation:
   - alias: 'NWS Weather Alert Pop Up Control'
-    initial_state: 'on'
     trigger:
       platform: state
       entity_id: sensor.nws_alerts
@@ -188,7 +187,6 @@ automation:
           {% endif %}
   
   - alias: 'NWS Weather Sensor Refresh'
-    initial_state: 'on'
     trigger:
       platform: state
       entity_id: sensor.nws_alerts
@@ -196,7 +194,6 @@ automation:
       - service: script.refresh_weather_alert_sensors
 
   - alias: NWS Notification Weather Alert
-    initial_state: 'on'
     trigger:
       platform: state
       entity_id: sensor.nws_alerts
@@ -228,7 +225,6 @@ automation:
             {% endif %}
 
   - alias: NWS Announce Weather Alert
-    initial_state: 'on'
     trigger:
       - platform: state
         entity_id: sensor.nws_alerts
@@ -285,7 +281,6 @@ automation:
             {% endif %}
 
   - alias: NWS Announce Weather Alert for Tornado
-    initial_state: 'on'
     trigger:
       - platform: state
         entity_id: sensor.nws_alerts
@@ -319,47 +314,58 @@ automation:
           title: "Tornado Warning!"
           message: "The National Weather Service Has issued a Tornado Warning for our area."
 
-  # - alias: Lightning Detected
-  #   initial_state: 'on'
-  #   trigger:
-  #     - platform: state
-  #       entity_id: binary_sensor.lightning_active
-  #       to: 'on'
-  #   action:
-  #     - service: script.text_notify
-  #       data_template:
-  #         who: "jeff"
-  #         title: "Lightning Detected!"
-  #         message: "Lightning has been detected within 15 miles of Anchorage House"
+  - alias: Lightning Detected
+    trigger:
+      - platform: state
+        entity_id: binary_sensor.lightning_detected
+        from: 'off'
+        to: 'on'
+      - platform: template
+        value_template: '{{ states.sensor.lightning_strike_count.state | int > states.sensor.lightning_total_count.state | int }}'
+    condition:
+      - condition: template
+        value_template: '{{ states.sensor.lightning_strike_count.state | int > states.sensor.lightning_total_count.state | int }}'
+      - condition: state
+        entity_id: input_boolean.lightning_warning
+        state: 'off'
+    action:
+      - service: script.text_notify
+        data_template:
+          who: "jeff"
+          title: "Lightning Detected!"
+          message: "Lightning has been detected within 20 miles of Anchorage House"
+      - service: input_boolean.turn_on
+        entity_id: input_boolean.lightning_warning
   #     - service: script.lightning_warning_audible
 
-  # - alias: Lightning Warning Off
-  #   initial_state: 'on'
-  #   trigger:
-  #     - platform: state
-  #       entity_id: binary_sensor.lightning_active
-  #       to: 'off'
-  #   action:
-  #     - service: script.text_notify
-  #       data_template:
-  #         who: "jeff"
-  #         title: "Lightning Threat Over."
-  #         message: "No more Lightning Strikes detected."
-  #     - service: script.lightning_clear_audible
-  
-  # - alias: 'lightning_update'
-  #   initial_state: 'on'
-  #   trigger:
-  #     - platform: time_pattern
-  #       minutes: '/5'
-  #   action:
-  #     - service: homeassistant.update_entity
-  #       entity_id: sensor.lightning_strike_count
-  #     - service: homeassistant.update_entity
-  #       entity_id: binary_sensor.lightning_active
+
+  - alias: Lightning Warning Off
+    trigger:
+      - platform: state
+        entity_id: binary_sensor.lightning_detected
+        from: 'on'
+        to: 'off'
+      - platform: template
+        value_template: '{{ states.sensor.lightning_strike_count.state | int == states.sensor.lightning_total_count.state | int }}'
+        for:
+          minutes: 20
+    condition:
+      - condition: template
+        value_template: '{{ states.sensor.lightning_strike_count.state | int == states.sensor.lightning_total_count.state | int }}'
+      - condition: state
+        entity_id: input_boolean.lightning_warning
+        state: 'on'
+    action:
+      - service: script.text_notify
+        data_template:
+          who: "jeff"
+          title: "Lightning Threat Over."
+          message: "No more Lightning Strikes detected."
+      - service: input_boolean.turn_off
+        entity_id: input_boolean.lightning_warning
+      #- service: script.lightning_clear_audible
 
   - alias: NWS Freeze Warning
-    initial_state: 'on'
     trigger:
       - platform: state
         entity_id: sensor.nws_alerts
@@ -375,7 +381,6 @@ automation:
         entity_id: input_boolean.freeze_warning
 
   - alias: Forecast Low Near Freezing
-    initial_state: 'on'
     trigger:
       - platform: numeric_state
         entity_id: sensor.nws_overnight_low
@@ -392,7 +397,6 @@ automation:
         entity_id: input_boolean.freeze_warning
   
   - alias: Forecast Low Above Freezing
-    initial_state: 'on'
     trigger:
       - platform: numeric_state
         entity_id: sensor.nws_overnight_low
@@ -409,7 +413,6 @@ automation:
         entity_id: input_boolean.freeze_warning
       
   - alias: Activate Tornado alarm 
-    initial_state: 'on'
     trigger:
     - platform: state
       entity_id: input_boolean.tornado_alarm 
@@ -420,7 +423,6 @@ automation:
       entity_id: script.tornado_alarm
 
   - alias: Deactivate Tornado alarm 
-    initial_state: 'on'
     trigger:
     - platform: state
       entity_id: input_boolean.tornado_alarm 
@@ -470,34 +472,32 @@ script:
           media_content_id: http://192.168.7.40/audio/tornado_alarm.mp3
           media_content_type: "music"
   
-  # lighning_warning_audible:
-  #   sequence: 
-  #     - condition: state
-  #       entity_id:  binary_sensor.day
-  #       state: 'on'
-  #     - service: script.twitter_notify_image
-  #       data_template:
-  #         tweet: '{{ [ "Did you see that flash? I did. Letting everyone know there is lightning nearby.", 
-  #         "When Lightning is near I notify the residents of Anchorage House to keep them safe.", 
-  #         "If the residents of Anchorage House didnt hear the thunder I just gave them a heads up.",
-  #         "Everyone inside! If you are near Anchorage House that is. Lightning is near." ] | random }}'
-  #         image: >-
-  #           {{ [ "/config/www/tweet_images/lightning.jpg",
-  #                "/config/www/tweet_images/lightning-bolt.jpg"] | random }}
-  #     - service: script.ah_report
-  #       data:
-  #         call_interuption: 1
-  #         call_lightning_alert: 1
+  lightning_warning_audible:
+    sequence: 
+      - condition: state
+        entity_id:  binary_sensor.day
+        state: 'on'
+      - service: script.twitter_notify_image
+        data_template:
+          tweet: '{{ [ "Did you see that flash? I did. Letting everyone know there is lightning nearby.", 
+          "When Lightning is near I notify the residents of Anchorage House to keep them safe.", 
+          "If the residents of Anchorage House didnt hear the thunder I just gave them a heads up.",
+          "Everyone inside! If you are near Anchorage House that is. Lightning is near." ] | random }}'
+          image: >-
+            {{ [ "/config/www/tweet_images/lightning.jpg",
+                 "/config/www/tweet_images/lightning-bolt.jpg"] | random }}
+      - service: script.ah_report
+        data:
+          call_lightning_alert: 1
   
-  # lighning_clear_audible:
-  #   sequence: 
-  #     - condition: state
-  #       entity_id:  binary_sensor.day
-  #       state: 'on'
-  #     - service: script.ah_report
-  #       data:
-  #         call_interuption: 1
-  #         call_lightning_clear: 1
+  lightning_clear_audible:
+    sequence: 
+      - condition: state
+        entity_id:  binary_sensor.day
+        state: 'on'
+      - service: script.ah_report
+        data:
+          call_lightning_clear: 1
 
   refresh_weather_alert_sensors:
     sequence: 
@@ -512,4 +512,6 @@ script:
       - service: homeassistant.update_entity
         entity_id: sensor.flood_warning
       - service: homeassistant.update_entity
-        entity_id: sensor.flood_watch 
\ No newline at end of file
+        entity_id: sensor.flood_watch 
+
+  
\ No newline at end of file
diff --git a/config/sensors/weather.yaml b/config/sensors/weather.yaml
index b7f1865..450d6b8 100755
--- a/config/sensors/weather.yaml
+++ b/config/sensors/weather.yaml
@@ -34,40 +34,69 @@
 #     minutes: 5
 #     seconds: 0
 #     milliseconds: 0
-## Accurite 158 - Back Porch
+## Accurite 158 - Crawlspace
 - platform: mqtt
-  name: "Accurite Back Porch Temperature"
-  state_topic: "house/accurite-158/temperature"
+  name: "Crawlspace Temperature"
+  state_topic: "house/acurite-158/temperature"
   device_class: temperature
 - platform: mqtt
-  name: "Accurite Back Porch Humidity"
-  state_topic: "house/accurite-158/humidity"
+  name: "Crawlspace Humidity"
+  state_topic: "house/acurite-158/humidity"
+  device_class: humidity
+- platform: mqtt
+  name: "Crawlspace Model"
+  state_topic: "house/acurite-158/model"
+- platform: mqtt
+  name: "Crawlspace lupdate"
+  state_topic: "house/acurite-158/last_update"
+  device_class: timestamp
 
-# - platform: mqtt
-#   name: "Accurite Back Porch Status"
-#   state_topic: "house/accurite-158/status"
-# - platform: mqtt
-#   name: "Accurite Back Porch Battery"
-#   state_topic: "house/accurite-158/battery"
+## Accurite 10968 - Garage
 - platform: mqtt
-  name: "Accurite Back Porch Model"
-  state_topic: "house/accurite-158/model"
-- platform: mqtt
-  name: "Accurite Back Porch lupdate"
-  state_topic: "house/accurite-158/lastupdate"
-- platform: mqtt
-  name: "Accurite Garage Temperature"
-  state_topic: "house/accurite-10968/temperature"
+  name: "Garage Temperature"
+  state_topic: "house/acurite-10968/temperature"
   device_class: temperature
 - platform: mqtt
-  name: "Accurite Garage Humidity"
-  state_topic: "house/accurite-10968/humidity"
+  name: "Garage Humidity"
+  state_topic: "house/acurite-10968/humidity"
+  device_class: humidity
 - platform: mqtt
-  name: "Accurite Garage Model"
-  state_topic: "house/accurite-10968/model"
+  name: "Garage Model"
+  state_topic: "house/acurite-10968/model"
 - platform: mqtt
-  name: "Accurite Garage lupdate"
-  state_topic: "house/accurite-10968/lastupdate"
+  name: "Garage lupdate"
+  state_topic: "house/acurite-10968/last_update"
+  device_class: timestamp
+
+
+## Accurite 6045M - Back Porch
+- platform: mqtt
+  name: "Back Porch Temperature"
+  state_topic: "house/acurite-6045M/temperature"
+  device_class: temperature
+- platform: mqtt
+  name: "Back Porch Humidity"
+  state_topic: "house/acurite-6045M/humidity"
+  device_class: humidity
+- platform: mqtt
+  name: "Back Porch Model"
+  state_topic: "house/acurite-6045M/model"
+- platform: mqtt
+  name: "Back Porch lupdate"
+  state_topic: "house/acurite-6045M/last_update"
+  device_class: timestamp
+- platform: mqtt
+  name: "Lightning Strike Count"
+  state_topic: "house/acurite-6045M/lightning_count"
+- platform: mqtt
+  name: "Lightning Storm Distance"
+  state_topic: "house/acurite-6045M/storm_dist"
+
+- platform: mqtt
+  name: "Lightning total count"
+  state_topic: "house/lightning/total_count"
+
+
 
 - platform: statistics
   name: Inside Humidity Stats