# Wine Fridge Package

# -----------------------------------------------------------------------------
# Wine Fridge Helpers
input_number:
  wine_fridge_setpoint:
    name: Wine Fridge Set Point
    initial: 65
    min: 45
    max: 70
    step: 1
    unit_of_measurement: °F

  wine_fridge_delta_t:
    name: Wine Fridge Delta T
    initial: 5
    min: 2
    max: 15
    unit_of_measurement: °F

template:
  binary_sensor:
    # Start wine fridge when dT is high enough to make the compressor happy
    - name: Wine Fridge Should Start
      state: >
        {{
          states('sensor.wine_fridge_sensor_temperature')|float > states('input_number.wine_fridge_setpoint')|float + 1
          and states('sensor.garage_sensor_air_temperature')|float > states('sensor.wine_fridge_sensor_temperature')|float + states('input_number.wine_fridge_delta_t')|float
          and states('switch.garage_wine_fridge_outlet') == 'off'
        }}

    # Stop wine fridge when temperature falls and the compressor is stopped
    - name: Wine Fridge Should Stop
      state: >
        {{
          states('sensor.garage_sensor_air_temperature')|float < states('sensor.wine_fridge_sensor_temperature')|float + states('input_number.wine_fridge_delta_t')|float
          and states('sensor.garage_wine_fridge_outlet_electric_consumption_a')|float < 0.5
          and states('switch.garage_wine_fridge_outlet') == 'on'
        }}

# -----------------------------------------------------------------------------
# Wine Fridge Automations
automation:
  - alias: Turn On Wine Fridge
    mode: single
    trigger:
      - platform: state
        entity_id: binary_sensor.wine_fridge_should_start
        to: 'on'
        for:
          minutes: 5
    action:
      - service: switch.turn_on
        target:
          entity_id: switch.garage_wine_fridge_outlet

  - alias: Turn Off Wine Fridge
    mode: single
    trigger:
      - platform: state
        entity_id: binary_sensor.wine_fridge_should_stop
        to: 'on'
        for:
          minutes: 5
    action:
      - service: switch.turn_off
        target:
          entity_id: switch.garage_wine_fridge_outlet
