Run Once Only Timer for Air Conditioning

Background
My air conditioning units are split unit, each of them is controlled by remote / by ESPHome. On the remote, they have “run once only timer”. Here is the example:


At the bottom of the remote, there is TIMER group. If I press, it will cycle through 1h, 2h, 3h timers. If 1h is chosen, the AC will be turned off in 1 hour.

Goal
I want to replicate that functionality but for several air conditioning units. I imagine to be able to choose the AC units from my lovelace dashboard, then type/slide a timer to determine how long should the AC be turned off.

Any suggestion?

Thank you very much for your assistance.

So sorry wrong category. Edited & moved it to the right category.

I think you can achieve that by using the following-

  1. Input select containing the your list of air conditioning units
    Input Select - Home Assistant
  2. Input datetime for storing the timer value (configurable from the dashboard)
    Input Datetime - Home Assistant

For your reference, you could take a look at tom_l’s setup for his pizza delivery-

1 Like

If the room is divided by a wall then this is possible, however, if your device is in close proximity and under the same brand you will have a nightmare to setup

1 Like

It can be blocked by covering the side(s) of the IR transmitter using aluminum foil.

1 Like

Fortunately, only 1 air conditioning per room.

Great example @tom_l thanks! Using this, what happen if before the counter reaches 0, my HA crashes? Can the timer know after it starts up?

Yes it can recover from a restart.

1 Like

I have created an input_boolean as the example made by tom. But it seems the last_changed by input_boolean is not being saved after a restart. The last_changed value is replaced by when home assistant started which would make the timer longer.

Any idea?

Oh sorry, no it won’t recover from a restart. last_changed is set on restart (the state changes from unavailable to something).

123 wrote a post about restoring timers on restart.

1 Like

Alternative method for you-

  1. Create an input_datetime to be used to input your desired timer (Helpers)
input_datetime:
  ac_timer:
    name: AC Timer
    has_date: false
    has_time: true
  1. Create an input_select containing your list of air conditioner (Helpers). If your climate entity_id is climate.living_room_ac, then you write Living Room AC in the option.
input_select:
  ac_list:
    name: AC List
    options:
      - Living Room AC
      - Pantry AC
  1. Create an input_boolean to determine the status of your timer (Helpers)
input_boolean:
  ac_timer_status:
    name: AC Timer Status
  1. Create a “dummy” automation to turn on the air conditioning, retrieve last_triggered, and to turn on input_boolean. Make sure to turn off the dummy automation. You will only use automation.trigger service to call the action of this automation.
alias: 'Timer: Air Conditioning ON'

trigger:
  - platform: tag
    tag_id: ''

condition: []

action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: cool
    target:
      entity_id: >-
        climate.{{ states('input_select.ac_list')
          | regex_replace(find=' ', replace='_', ignorecase=False)
          | lower }}
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.ac_timer_status
  1. Create a template sensor to be used as a countdown timer
template:
  - sensor:
      - name: Remaining AC Timer
        icon: "{{ 'mdi:timer-off' if is_state('input_boolean.ac_timer_status','off') else 'mdi:timer' }}"
        state: >-
          {% set t = (as_timestamp(state_attr('automation.timer_air_conditioning_on', 'last_triggered')) 
            + state_attr('input_datetime.ac_timer', 'timestamp') - as_timestamp(now())) | int %}
          {% set r = ([0, t ]|max) if is_state('input_boolean.ac_timer_status', 'on') else 0 %}
          {{ '{:02d}:{:02d}'.format(r // 3600, (r % 3600) // 60) }}
  1. Create an automation to turn off the air conditioning based on the states of the template sensor
alias: 'Timer: Air Conditioning OFF'

trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.remaining_ac_timer') == '00:00' }}
  - platform: homeassistant
    event: start

condition:
  - condition: template
    value_template: >-
      {{ states('sensor.remaining_ac_timer') == '00:00' }}
  - condition: state
    entity_id: input_boolean.ac_timer_status
    state: 'on'

action:
  - service: climate.turn_off
    target:
      entity_id: >-
        climate.{{ states('input_select.ac_list')
          | regex_replace(find=' ', replace='_', ignorecase=False)
          | lower }}
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.ac_timer_status
  1. Create an automation to prevent changing the AC List while the timer is running
trigger:
  - platform: state
    entity_id: input_select.ac_list

condition:
  - condition: state
    entity_id: input_boolean.ac_timer_status
    state: 'on'
  - condition: template
    value_template: >-
      {{ (as_timestamp(now())-as_timestamp(state_attr('automation.timer_air_conditioning_failsafe', 'last_triggered') | default(0)) | int > 3) }}

action:
  - service: input_select.select_option
    target:
      entity_id: input_select.ac_list
    data:
      option: "{{ trigger.from_state.state }}"
  - service: notify.mobile_app_your_name_here
    data:
      message: Timer for {{ trigger.from_state.state }} is currently running.
  1. Create a grid card to control your timer.
    This will be displayed in the card:
  • AC List
  • Input Datetime to control your timer duration
  • Remaining Timer for your AC
  • Button to Start/Stop the Automation Timer
type: grid
cards:
  - type: entities
    entities:
      - entity: input_select.ac_list
      - entity: input_datetime.ac_timer
      - entity: sensor.remaining_ac_timer
    show_header_toggle: false
  - type: button
    tap_action:
      action: call-service
      service: automation.trigger
      service_data: {}
      target:
        entity_id: automation.timer_air_conditioning_off
    hold_action:
      action: call-service
      service: automation.trigger
      service_data: {}
      target:
        entity_id: automation.timer_air_conditioning_on
    icon: mdi:air-conditioner
    name: Run AC Timer
columns: 2
square: false

Let me know if it works for you.

3 Likes

It worked!!! Thanks for the help @ardysusilo also @tom_l.

I ended up using automation example from ardysusilo as I can implement it directly without changing much from it.

In the future if I need a timer that can be restored after a restart, I’ll take a look at this topic again (thanks @tom_l).