Home Smart Home Home Assistant Lesson 14 – Special scripts and structures

Home Assistant Lesson 14 – Special scripts and structures

289 views

Structures in the Script

DELAY

Delay is useful if you want to temporarily stop the executing script and wait for the operation to resume after a while. This will be important in situations where you want to wait for media_player.play_media

You see the sample code. For example, if I want to make a script in the order of Google Home, issue the sentence “ABCDEF …” and then finish the light.

i_am_home:
  sequence:
    - service: media_player.play_media
      data:
        abcdefgh...#This specific place I will write in another tutorial, the purpose of this article is just an example
    - service: switch.turn_on
      entity_id: switch.main_light

If you do not do delay (as above, there is no delay), right after the first voice is played, but the sentence is not finished, the lamp is already open. Because the order is called the service is  media_player.play_media complete, it calls the service  switch.turn_on, not wait for Google Home to finish saying the sentence ‘ABCDEF’. For that reason, we will see how Google Home says ABCDEF for how long it takes for the delay to fit.

For example, Google Home says ABCDEF takes 3 seconds, we declare delay according to 1 in the following structures:

- delay: '00:00:03'

or

- delay:
    seconds: 3

We will be:

i_am_home:
  sequence:
    - service: media_player.play_media
      data:
        abcdefgh...#This specific place I will write in another tutorial, the purpose of this article is just an example
    - delay: '00:00:03'
    - service: switch.turn_on
      entity_id: switch.main_light

So after the call is  media_player.play_media completed, it will wait 3 seconds (corresponding to the time of the sentence) and then call again switch.turn_on

Delay used in such standby situations. However, for media, we have a different or better structure.

Through the next structure:

WAIT

Also the above example, but how do we use wait now?

The idea is that when we wait for the status of the media_player from playing to stop or idle (ie play is finished), we will open the light.

Code as follows:

i_am_home:
  sequence:
    - service: media_player.play_media
      data:
        abcdefgh...#This specific place I will write in another tutorial, the purpose of this article is just an example    
   - wait_template: "{{ is_state('media_player.speaker', 'stop') }}"    
   - service: switch.turn_on
     entity_id: switch.main_light

The paragraph   is meant to wait for media_player.speaker to go into the stop state to continue- wait_template: "{{ is_state('media_player.speaker','stop') }}"

Rate this post

Related Tips And Tricks

Leave a Comment

Tips and Tricks

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept