Problem

Smart plugs make it easy to automate devices in your home. Heavy duty ones can control higher load appliances like air conditioners and refrigerators. But... you might want to take extra precaution when connecting those to the internet!

AC units, for example, often warn you to wait a few minutes before restarting them. Why? Restarting too soon forces the compressor to start under high pressure - which can damage it or trip the circuit breaker.

We can avoid this situation by creating a "cool down" switch - something that prevents a switch from turning on if it was recently turned off.

Design

In Home Assistant we can create such a switch using a Template Switch, a Timer and an Automation.

Block diagram for "cool down" switch

For example, let's say we have an entity called switch.ac_unsafe.

We hide this switch from the UI and encode the danger into its name ("unsafe") to discourage future programmatic use.

We replace it with a Template Switch (switch.ac) that mirrors the real switch's state (using value_template) and forward all its input to the real switch.

We create an Automation (automation.start_cooldown) triggered by the real switch turning off which, in turn, starts a Timer (timer.cooldown) during the lockdown period. This is preferable to coupling switch.ac so the cool down still occurs even from external events (ex. physically triggering the smart plug).

We tie it all together by connecting the availability_template of switch.ac to the state of the timer. If the timer is ticking, the switch control is disabled.

"Cool down" switch in action

Implementation

Here's what the concept looks like in code.

In configuration.yaml:

# Create a fake switch that mirrors the real switch
switch:
  - platform: template 
    switches:
      ac_safe:
        availability_template: "{{ not is_state('timer.cooldown', 'active') }}"
        value_template: "{{ is_state('switch.ac_unsafe', 'on') }}"
        friendly_name: AC
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.ac_unsafe
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.ac_unsafe

# Hide the real switch from the UI
homeassistant:
  customize:
    switch.ac_unsafe:
      hidden: true

# Timer to track cool down period
timer:
  cooldown:
    duration: '00:05:00'

In automations.yaml:

- alias: Start Cool Down
  trigger:
  - device_id: <YOUR_ACTUAL_SWITCH_DEVICE_ID>
    domain: switch
    entity_id: switch.ac_unsafe
    platform: device
    type: turned_off
  condition: []
  action:
  - data: {}
    entity_id: timer.cooldown
    service: timer.start

Voice Commands

When connecting to Google Assistant or Alexa, be careful to exclude the actual switch (using include_entities or exclude_entities) and only expose the cool down switch. This extends the protection to voice commands:

cloud:
  google_actions:
    filter:
      include_entities:
        - switch.ac

An example conversation with Google Assistant:

Hey Google, turn on the AC.
Sure, turning AC on.
Hey Google, turn off the AC.
Got it, turning AC off.
Hey Google, turn on the AC.
Sorry, it looks like AC isn't available right now.

The Alexa integration doesn't handle it as gracefully, but the switch is still protected. When I tried, it verbally confirmed the AC turned on, even though it did not.