Timer Automation with Toggle

Hi All,

I have created a timer automation that will toggle a switch and will send a notification when finished.
As the entity I use is a Sonoff Plug, the timer will not start unless the switch is powered on and available.

The timer card looks like this:
timer card

You need 3 helpers:

  1. Toggle (in the code input_boolean.timer_1_toggle)
  2. Number ( in the code input_number.timer_1_slider)
  3. Timer (in the code timer.timer_1_timer)

The automation is I made is the one below:

alias: Timer - Sonoff Plug 1
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.timer_1_toggle
    id: Start
    to: 'on'
  - platform: state
    entity_id: input_boolean.timer_1_toggle
    id: Cancel
    to: 'off'
  - platform: event
    event_type: timer.finished
    id: Finish
    event_data:
      entity_id: timer.timer_1_timer
  - platform: event
    event_type: timer.cancelled
    id: Cancelled
    event_data:
      entity_id: timer.timer_1_timer
  - platform: event
    event_type: timer.started
    id: Started
    event_data:
      entity_id: timer.timer_1_timer
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Start
          - condition: not
            conditions:
              - condition: state
                entity_id: switch.sonoffplug1
                state: unavailable
        sequence:
          - service: timer.start
            data:
              duration: '{{ states(''input_number.timer_1_slider'') | int * 60 }}'
            target:
              entity_id: timer.timer_1_timer
      - conditions:
          - condition: trigger
            id: Cancel
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.timer_1_timer
      - conditions:
          - condition: trigger
            id: Finish
        sequence:
          - service: switch.toggle
            target:
              entity_id: switch.sonoffplug1
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.timer_1_toggle
          - service: notify.all_devices
            data:
              message: Timer for Sonoff Plug 1 is finished
              title: Plug 1 timer
      - conditions:
          - condition: trigger
            id: Cancelled
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.timer_1_toggle
      - conditions:
          - condition: trigger
            id: Started
          - condition: not
            conditions:
              - condition: state
                entity_id: switch.sonoffplug1
                state: unavailable
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.timer_1_toggle
    default:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.timer_1_toggle
      - service: timer.cancel
        target:
          entity_id: timer.timer_1_timer
mode: single

The card code is the one below:

type: entities
entities:
  - entity: switch.sonoffplug1
  - entity: input_boolean.timer_1_toggle
  - entity: input_number.timer_1_slider
  - entity: timer.timer_1_timer
title: Plug 1 Timer
show_header_toggle: false
state_color: true

I hope you find it useful!

4 Likes

If you’re interested, here’s a streamlined version of the automation:

alias: Timer - Sonoff Plug 1
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.timer_1_toggle
  - platform: event
    event_type: ['timer.started', 'timer.finished', 'timer.cancelled']
    event_data:
      entity_id: timer.timer_1_timer
condition: "{{ states('switch.sonoffplug1') != 'unavailable' }}"
action:
  - choose:
      - conditions: "{{ trigger.platform == 'state' }}"
        sequence:
          - choose:
              - conditions: "{{ trigger.to_state.state == 'on' }}"
                sequence:
                  - service: timer.start
                    target:
                      entity_id: timer.timer_1_timer
                    data:
                      duration: "{{ states('input_number.timer_1_slider') | int * 60 }}"
            default:
              - service: timer.cancel
                target:
                  entity_id: timer.timer_1_timer
    default:
      - service: "input_boolean.turn_{{ 'on' if trigger.event.event_type == 'timer.started' else 'off' }}"
        target:
          entity_id: input_boolean.timer_1_toggle
      - condition: template
        value_template: "{{ trigger.event.event_type == 'timer.finished' }}"
      - service: switch.toggle
        target:
          entity_id: switch.sonoffplug1
      - service: notify.all_devices
        data:
          message: Timer for Sonoff Plug 1 is finished
          title: Plug 1 timer
mode: single
max_exceeded: silent

If there was no need to display the remaining time (i.e. the timer’s countdown) the automation can be reduced even further (by replacing the timer with a for option in a State Trigger).

1 Like

Looks really nice but it will not cancel the timer when the plug is not available.

I want to make sure that the automation checks that the plug is powered on before the countdown starts.

It won’t start the timer if the plug is unavailable. If it doesn’t start it, there’s no need to cancel it.

Sure, but the time on/off switch remains on. Also the timer strarts the countdown when you hit the Start button from the timer entity.

With my code these 2 don’t happen.

It’s not a competition of course. Both work just fine, just my version works the way I need to.

I appreciate the input of course!

Which is irrelevant because the UI will clearly show the timer has failed to start, indicating there’s a problem (namely the switch is offline).

I fail to see the justification for all of these entities and the elaborate automation to control the timer if you still allow for manual control of the timer.

You’re welcome.

Sorry, bei mir tut sich mit dieser Automatisierung ĂĽberhaupt nichts :frowning: Es startet kein Timer und und die Zeit vom Slider wird auch nicht in den Timer ĂĽbernommen.

@nefelodamon , Will this allow the timer to be stopped and started without resetting the timer? For instance: can I start the timer, pause it after a certain amount if time, then restart it, and have it continue where it left off?