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:
You need 3 helpers:
- Toggle (in the code input_boolean.timer_1_toggle)
- Number ( in the code input_number.timer_1_slider)
- 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!