Hello,
I would like to share my little script. It took me some time to finish it. I found it difficult to find the right information. Unfortunately, many relevant questions in this forum were answered with solutions in which the original approach was no longer needed.
My goal:
I wanted a simple way to specify a time period after which an automation would be triggered. This period should be flexible, changeable and cancelable.
In my specific case, I have two valves that I sometimes forget to turn off. Since I plan to purchase a lot more, the script should be universally applicable. Unfortunately, I couldn’t get it to work completely independently of helper entities.
Here is an example of my Lovelace Card:
What do you need:
You need a timer for each entity you want to trigger an automation for.
Here is the script:
alias: X Sekunden zu Timer
sequence:
- service: timer.pause
target:
entity_id: "{{ entity }}"
- variables:
newDuration: >-
{% set r = (state_attr(entity,'remaining')|default("00:00:00",true)) %}
{% set parts = r.split(":") %} {% set result = parts[0]|int * 3600 +
parts[1]|int * 60 + parts[2]|int %} {% set result = result + change %}
{{ result|int }}
- if:
- condition: template
value_template: "{{ newDuration < 5 }}"
then:
- service: timer.cancel
target:
entity_id: "{{ entity }}"
else:
- service: timer.start
metadata: {}
data:
duration: "{{ newDuration }}"
target:
entity_id: "{{ entity }}"
- condition: template
value_template: "{{ wasActive == false }}"
- service: timer.pause
data: {}
target:
entity_id: "{{ entity }}"
description: >-
Füge x Minuten zum übergebenen Timer hinzu, diese dürfen auch negativ sein.
Fällt der Timer dabei unter 5 Sekunden, wird er abgebrochen. War der Timer
nicht aktiv, wird er nach dem Hinzufügen pausiert.
fields:
timerEntity:
name: Timer
description: Der Timer, welcher verändert werden soll
example: timer.timer_x
required: true
changeValue:
selector:
number:
min: -86400
max: 86400
name: Change
required: true
default: 300
description: Der Wert in Sekunden um wieviel der Timer verändert werden soll
variables:
entity: "{{ timerEntity }}"
change: "{{ changeValue }}"
wasActive: "{{ is_state(entity, 'active') }}"
mode: queued
icon: mdi:timer-edit-outline
max: 12
Explanation:
The script has 2 fields that must be set when called.
The entity_id of our timer helper entity and the seconds by which we want to change the timer.
First we pause the timer so that the remaining attribute is updated.
If the new total time is less than 5 seconds, we cancel the timer. No finished event is thrown.
Alternatively, we set the new total time.
At the end it is checked whether the timer was originally active. If this is not the case, it is paused again.
Here is my card config:
I use values of 300 to increase/decrease the timer by 5 minutes
type: entities
entities:
- entity: switch.hochbeet_ventil_1
name: Hochbeet Ventil 1
toggle: true
type: custom:multiple-entity-row
entities:
- icon: mdi:minus
tap_action:
action: call-service
service: script.x_sekunden_zu_timer
service_data:
timerEntity: timer.abschalt_timer_hochbeet_ventil_1
changeValue: -300
- entity: timer.abschalt_timer_hochbeet_ventil_1
attribute: remaining
name: false
- icon: mdi:plus
tap_action:
action: call-service
service: script.x_sekunden_zu_timer
service_data:
timerEntity: timer.abschalt_timer_hochbeet_ventil_1
changeValue: 300
- entity: switch.hochbeet_ventil_2
name: Hochbeet Ventil 2
toggle: true
type: custom:multiple-entity-row
entities:
- icon: mdi:minus
tap_action:
action: call-service
service: script.x_sekunden_zu_timer
service_data:
timerEntity: timer.abschalt_timer_hochbeet_ventil_2
changeValue: -300
- entity: timer.abschalt_timer_hochbeet_ventil_2
attribute: remaining
name: false
- icon: mdi:plus
tap_action:
action: call-service
service: script.x_sekunden_zu_timer
service_data:
timerEntity: timer.abschalt_timer_hochbeet_ventil_2
changeValue: 300
- entity: sensor.hochbeet_temperatur
- entity: switch.hochbeet_steckdosen
footer:
type: graph
entity: sensor.hochbeet_temperatur
hours_to_show: 24
detail: 1
A note about triggering automations:
I created automations using the UI. Here timer.finish was offered to me as an event. But that is the name of the service call. When the timer expires, the event is timer.finished
I hope this is helpful to others. If you have any suggestions or questions, please let me know.