How to create a flexible Timer for Assist (Home Assistant)
Use custom sentences with Assist to start a timer and react on it after finishing
This is an unusual blog article about Home Assistant. If you have subscribed to this channel due to the Flutter articles: Don’t worry, I will continue to publish articles on this topic as well
English article
This is a short blog post for anyone who uses Home Assistant and is looking for an easy way to set a timer using voice control.
What to expect?
In this article, I will show you step by step how you can create a minute acurate extendable timer in Home Assistant that is started by voice control using Assist and sends a message to your smartphone (or performs any other action) after the time has elapsed.
What not to expect?
This article is aimed at Home Assistant users who have already had their first experience with Assist and voice control in Home Assistant.
The article does not cover setting up a voice assistant in Home Assistant. There are plenty of articles and videos on this topic on the web.
It only briefly explains the configuration of custom sentences. If there are any difficulties with the setup, I recommend the following entry on Home Assistant webpage: https://www.home-assistant.io/voice_control/custom_sentences/#setting-up-sentences-in-the-config-directory
Create a timer helper
If this information is new: There is already a timer as a helper in Home Assistant. Unfortunately, it is not easy to access it out of the box via Assist.
However, we need one of these helpers for our flexible timer.
The timer can be created either in the configuration.yaml file or under “Settings -> Devices & services -> Helpers -> Create helper -> Timer”. The name and duration are nearly irrelevant. Only the EntityID derived from the name is needed for later.
Preparing the Timer sentences (intents)
Go to your YAML in the file editor to define new custom sentences (custom_sentences/en/your_file.yaml). Create a new Intent SetTimer indented under intents. One or more sentences can then be defined under this.
Under a new item lists, we define all the attributes that we want to access in the timer, as well as their minimum and maximum values. For a simple timer, I have only created a Timer_Minute attribute.
An example YAML file looks like this:
Create the response
One way to react to custom sentences is via the intent_script entry. This is introduced with the intent_script: entry in configuration.yaml and can define the response sentences either directly there or in a file embedded via !include in the same directory. The intent_script for the flexible timer looks as follows:
With SetTimer we refer to our custom intent. Below this, we execute the actions action and speech. Action defines the action for our timer. The command to Home Assistant in human readable language is:
Start the timer (timer.start) myTimer (timer.myTimer) with a duration of x minutes (timer_minute).
The response sentence is formulated under speech using the text attribute, whereby all variables defined under lists can also be accessed here (timer_minute).
Mid-term result
After reloading the YAML configuration, our timer is already ready for use. When Assist hears a sentence defined under sentences, it responds and starts the timer. All that is missing now is a reaction as soon as the timer has expired. The reaction can be as arbitrary as you want it to be. In my case, for example, a Telegram message is sent as soon as the timer has expired. But you can also receive a push notification on your mobile phone if you have installed the Home Assistant app. Or have the lights flicker briefly, etc.
React once the timer’s finished
Using automation, we can now react as soon as the timer is finished. The trigger is activated via an event with the name timer.finished. We again specify our timer myTimer as the entity.
Further Development
With this timer as a foundation, you should now be able to control timers in every conceivable way using Assist. Possible extensions of this result are:
By creating several timer helper objects, for example, several timers can be started simultaneously.
Or: By adding seconds and hours to the lists attributes and adjusting the duration accordingly, timers can be controlled to the second (duration: “{{timer_hours}}:{{timer_minutes}}:{{timer_seconds}}”).
Have you saved time by reading this post? If so, I would be delighted to receive a small gift as a thoughtful gesture :)
Blog-Post auf deutsch
Dieser Post ist ein kurzer Beitrag für alle, die Home Assistant nutzen und einen einfachen Weg suchen per Sprachsteuerung einen Timer zu stellen.
Was behandelt dieser Post?
In diesem Beitrag zeige ich euch Schritt für Schritt, wie ihr einen Minutengenauen erweiterbaren Timer in Home Assistant erstellen könnt, der per Sprachsteuerung über Assist gestartet wird und euch nach Ablauf der Zeit eine Nachricht ans Smartphone schickt (oder jede beliebige weitere Aktion ausführt)
Was behandelt er nicht?
Dieser Beitrag richtet sich an Nutzer von Home Assistant, die bereits erste Erfahrungen mit Assist und der Sprachsteuerung in Home Assistant gemacht haben.
Der Beitrag umfasst nicht die Einrichtung eines Sprachassistenten in Home Assistant. Zu diesem Thema findet man genügend Beiträge und Videos im Web.
Es wird nur kurz die Konfiguration von Custom Sentences erläutert. Sollte es bei der Einrichtung Schwierigkeiten gebe empfehle ich den folgenden Eintrag in Home Assistant: https://www.home-assistant.io/voice_control/custom_sentences/#setting-up-sentences-in-the-config-directory
Timer Helfer erstellen
Falls diese Info neu ist: Es gibt bereits einen Timer als Helfer in Home Assistant. Leider lässt sich dieser nicht out of the box über Assist nicht so einfach ansprechen.
Für unseren flexiblen Timer benötigen wir jedoch einen dieser Helfer. Der Timer kann entweder in der configuration.yaml Datei oder unter “Einstellungen -> Geräte & Dienste -> Helfer -> Helfer erstellen -> Timer” erstellt werden. Dabei sind Name und Dauer nahezu irrelevant. Lediglich die aus dem Namen abgeleitete EntitätID braucht man für später.
Eingabesatz für den Timer vorbereiten (intents)
Gehe im File Editor in dein YAML, um neue Custom Sentences zu definieren (custom_sentences/de/your_file.yaml). Erstelle eingerückt unter intents ein neues Intent SetTimer. Unter diesem können anschließend eine oder mehrere Sätze definiert werden.
Unter einem neuen Punkt lists definieren wir alle Attribute, auf die wir im Timer zugreifen möchten, sowie ihre Minimal- und Maximalwerte. Für einen einfachen Timer habe ich nur ein Timer_Minute Attribut erstellt.
Eine YAML-Beispieldatei sieht wie folgt aus:
Assists Antwort erstellen
Eine Möglichkeit auf Custom Sentences zu reagieren ist über den intent_script Eintrag. Dieser wird eingeführt mit dem Eintrag intent_script: in der configuration.yaml und kann entweder direkt dort oder in einer über !include eingebetteten Datei im selben Verzeichnis die Antwortsätze definieren. Das Intent_script für den flexiblen Timer sieht wie folgt aus:
Mit SetTimer verweisen wir auf unser custom intent. Darunter führen wir die Aktionen action und speech aus. Action sefiniert die Aktion für unseren Timer. Der Befehl an Home Assistant lautet:
Starte den Timer (timer.start) myTimer (timer.myTimer) mit einer Dauer von x Minuten (timer_minute).
Unter speech wird mittels des Attributs text der Antwortsatz formuliert, wobei auch hier auf alle unter lists definierten Variablen zugegriffen werden kann (timer_minute).
Zwischenergebnis
Nach dem neu laden der YAML Konfiguration ist unser Timer bereits einsatzbereit. Wenn Assist einen unter sentences definierten Satz hört antwortet er und startet den Timer. Jetzt fehlt lediglich eine Reaktion, sobald der Timer abgelaufen ist. Die Reaktion kann dabei so beliebig wie sein, wie man es sich vorstellt. Bei mir wird Beispielsweise eine Telegram Nachricht geschickt, sobald der Timer abgelaufen ist. Man kann aber auch eine Push Benachrichtigung auf sein Handy erhalten, wenn man die Home Assistant App installiert hat. Oder die Lichter kurz flackern lassen etc.
Nach Ablauf des Timers reagieren
Mithilfe einer Automation können wir nun reagieren, sobald der Timer fertig ist. Der Trigger wird dabei über ein event mit dem namen timer.finished ausgelöst. Als Entität geben wir dabei wieder unseren Timer myTimer an.
Weiterentwicklungsmöglichkeiten
Mit diesem Timer als Grundstein sollte es dir nun möglich sein Timer über jede erdenkliche Art mittels Assist steuern zu können. Mögliche Erweiterungen dieses Ergebnisses sind:
Durch erstellen mehrerer Timer Helfer Objekte können beispielsweise mehrere Timer gleichzeitig gestartet werden.
Oder: Durch Erweiterung der lists Attribute um Sekunden und Stunden und der entsprechenden Anpassung der Dauer können Timer Sekundengenau gesteuert werden (duration: “{{timer_hours}}:{{timer_minutes}}:{{timer_seconds}}”).
Hast du durch diesen Post durch ausprobieren gespart? Dann freue ich mich über eine kleine Aufmerksamkeit als nette Geste :)