Home Assistant Setting Up Voice Timer

 We've been familiar with the idea ease and convenience of voice activated timer either on Alexa, Google Assistant or Apple Home/Siri.

However, to get it working on Home Assistant (HA), isnt achieved without a bit of nerd-ish satisfaction to exercise and see the designing and realization of the design come to life.

A bit of a background

The Talking to Assist feature in home assist, "set a timer for 5 minutes" does start a timer, however you need some device connected to HA to let you know that the timer has completed. That was not what i was not very excited about, one more device to make it work, hmmm , not my stuff. To set the scope, the requirement needs to be defined clearly, hence the designing process was triggered.

Requirement(s): 

1, Start a timer with voice command for specified time ( hours, minutes and seconds )

2. Play a ringing sound once the timer is done.

3. Stop the ringing sound on stop voice command.

The Design : Part Starting Timer (Automation 1)

The design has to be thought out, within the HA framework, so first step was to setup the pre - requisites that would help realize step 1.

Starting Timer Setup - 1

Which means, setting up a helper of Timer type, lets call it variable_timer, the remaining of our design would look at how it can be activated for a specific duration, then at the end of the activation it should trigger our ringing sound.

 Starting Timer Automation Design 

The automation would be triggered by sentences in a way that can be triggered for hour, minutes and seconds. As we want to trigger a specific timer, not the one used by HA by default we would need to customize the intent message. Hence as follows

book a timer for {minute} minute[s]
book a timer for {second} second[s]
book a timer for {hour} hours[s]
book a timer for {hour} hours[s] and {minute} minute[s]
book a timer for {minute} minute[s] and {second} second[s]

The variable minute, second, and hour would go into the HA namespace trigger.slots and can be retrieved as trigger.slots.minute ( for example )

Then use that to set the duration for the timer, with the following YAML/Jinja script, so as to set default values hour, minute, and seconds if it was not part of the command.
{% set t_hour = trigger.slots.hour | default(0) | int %}
{% set t_sec = trigger.slots.second | default(0) | int %}
{% set t_min = trigger.slots.minute | default(0) | int %}
{{t_hour}}:{{t_min}}:{{t_sec}}

Code 1 : The source here

The Design : Part Ringing Sound

On to the next part of playing the sound, here i considered two choices either to play media on music assistant using one of the streaming platform as a source, or to get an open source mp3 that plays the standards ringing sound. Obviously went for the latter.

Ringing Sound Setup

Nothing major here, but we need to upload the media file to the right spot, for which we create a sample automation, add a play media action, and select what to play, and when you select the local option, at the top right of the popup it gives an option to upload the media file use it to upload the file so that can be selected when you setup the play part of the automation. You can discard this automation, we just needed to upload the file to the right place

 Timer End Automation Design  (Automation 2)

 Upon ending the timer, it triggers a timer.finished event, this is what we shall use, so we create new automation, use the event trigger , to trigger the automation. On the event data use our time name.

triggers:
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.variable_timer

Add an action to play the media file which we setup before. Test your setup at this point to start the timer and make sure that it plays back the ring once timer completes.

we will use a loop setup to make it play until we issue a stop command

The Design : Part Stopping Sound

 This requires two parts, one setup an input boolean as a helper, that we will use to exit the loop. 

This will have two implications

Implication 1 : Toggle Boolean to On

We would need to toggle the boolean variable to on, when we start the timer, and you can see that in the code : Code 1.

Implication 2 : Toggle Boolean to Off

We would need to to have another automation that would turn the Boolean to Off ( action ) based on a sentence trigger such as "turn off alarm"

 Timer End Automation Design  

Lets assume the implications 1 & 2 are taken care of, now we update the Ringing sound Automation( Automation 2) to a while loop within which the ring is played, as long as the toggle boolean value is true.

The code, Code 2 , is here 

Conclusion

There would be better approaches to the same problem, am yet to find the best way to tap into the default timer commands, hopefully soon, inshallah. This approach of building the timer and mixing and matching it with options fine tuning how the timer behaves is challenging yet interesting.

This was the last part of moving all of my alexa based automation to HA, and grateful to achieve this milestone going local.

Comments

Popular posts from this blog

Meaning of Summa Ameen

The Chicken Story- Shawai, Tanduri, BBQ or What?

Halaal Food E Codes