######################################################################################################
### @Tesla Powerwall - https://www.vcloudinfo.com/2018/01/going-green-to-save-some-green-in-2018.html
# # @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# Read more about this on https://www.vcloudinfo.com
# Tesla Powerwall added via UI Integration
# Tesla Powerwall Monitoring #272
######################################################################################################


#-------------------------------------------
# Located in the Grafana package.  Tweets out Solar Production Daily!
#
automation:
  - alias: Notify if Grid is down
    id: 56a32121-5725-4510-a1fa-10f69a5c82ef
    trigger:
      - platform: state
        entity_id: binary_sensor.grid_status
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: "{{ states('binary_sensor.grid_status') == 'on' }}"
        - condition: template
          value_template: "{{ states('binary_sensor.grid_status') == 'off' }}"
    action:
      - service: script.notify_engine
        data:
          title: "Electrical Grid Status {{ (trigger.to_state.state)|replace('on', 'up')|replace('off', 'down') }}."
          value1: "Please verify with the Tesla App."
          who: 'parents'
          apns_id: 'information'

# https://github.com/CCOSTAN/Home-AssistantConfig/issues/779
  - alias: Shut Things down if running on PowerWall
    mode: restart
    id: 37288a96-5f49-46c2-85c8-7e8794d23612
    trigger:
      - platform: state
        entity_id: binary_sensor.grid_status
        from: 'True'
        to: 'False'

    action:
      - service: homeassistant.turn_off
        entity_id:
         - group.interior_lights
         - group.exterior_lights
      - service: script.notify_engine
        data:
         title: "Electrical Grid Status {{ (trigger.to_state.state)|replace('True', 'up')|replace('False', 'down') }}."
         value1: "Taking actions to turning off the House Lights to preserve Battery Power."
         who: 'family'
         apns_id: 'information'

      - delay:
         minutes: 1
      - service: homeassistant.turn_off
        entity_id:
         - group.interior_lights
         - group.exterior_lights

      - service: script.speech_engine
        data:
          value1: "Because of the Power Outage, the Lights will be recycled for 3 minutes.  Lights may turn on and off during this time."

      - delay:
          minutes: 3
      - service: homeassistant.turn_off
        entity_id:
          - group.interior_lights
          - group.exterior_lights

      - service: script.speech_engine
        data:
          value1: "Automatic light recycling has been completed.  Any abnormalities will have to be addressed in the Hue App most likely. "

# https://github.com/CCOSTAN/Home-AssistantConfig/issues/547
  - alias: Turn off the Nest Thermostats during a Power Outage
    id: 022bd9da-ab7c-4c1b-9e95-2ff4ffe5f9cc

    trigger:
      - platform: state
        entity_id: binary_sensor.grid_status
        from: 'True'
        to: 'False'
        for:
          minutes: 6

    condition:
      - condition: template
        value_template: "{{ states('binary_sensor.grid_status') == 'off' }}"

    action:
      - service: climate.turn_off
        data:
         entity_id:
           - climate.downstairs
           - climate.upstairs

      - service: script.notify_engine
        data:
          title: "Electrical Grid Status {{ (trigger.to_state.state)|replace('True', 'up')|replace('False', 'down') }}."
          value1: "Taking actions to turning off the AC units to preserve Battery Power."
          who: 'family'
          apns_id: 'information'

  - alias: Turn back on the Nest Thermostats after a Power Outage
    id: 14faf84b-cb24-4391-a97f-b83479a67fc7

    trigger:
      - platform: state
        entity_id: binary_sensor.grid_status
        from: 'False'
        to: 'True'
        for:
          minutes: 5

    condition:
      - condition: template
        value_template: "{{ states('binary_sensor.grid_status') == 'on' }}"
      - condition: or
        conditions:
          - condition: state
            entity_id: climate.downstairs
            state: 'off'
          - condition: state
            entity_id: climate.upstairs
            state: 'off'

    action:
      - service: climate.set_hvac_mode
        data:
          entity_id: climate.downstairs
          hvac_mode: auto
      - service: climate.set_hvac_mode
        data:
          entity_id: climate.upstairs
          hvac_mode: cool
      - service: climate.set_preset_mode
        data:
          entity_id: climate.upstairs
          preset_mode: eco

      - service: script.notify_engine
        data:
          title: "Electrical Grid Status {{ (trigger.to_state.state)|replace('True', 'up')|replace('False', 'down') }}."
          value1: "All AC systems are set back to normal operations."
          who: 'family'
          apns_id: 'information'