Add easier solution for loops in automations/scripts

You don’t need a python script for this. Take a look at the alert integration, it should do exactly what you want.

The thing is, most of the time when someone asked for a loop here in the forum it was either solvable through the alert integration or no loop was needed for achieving their end goal. And people that really needed a loop and the alert integration was not enough, were capable of writing python scripts.

That’s an aspirational goal. Much work remains to make the Automation Editor support all existing automation abilities, let alone adding a looping ability. Currently, Home Assistant’s installation process alone puts it out of reach of ‘common folks’ (i.e. the average consumer).

As I’ve stated in previous posts, looping is an edge-case requirement. Many of the use-cases cited, to justify its need, can already be achieved through other means.

Feel free to vote for this Feature Request but know that thousands of people are using this 6+ years-old software project without an explicit ‘automation looping’ ability.

though agreeing with you here @123, I do wish we had a media_player.loop_media service… that would make life so much easier, the ways we have to go to make an automation play a file, calulate its length and restart that file again…

just curious - why do you need to do so many things?

I remember that conversation. Yes, a specific service for media_player, that understands the media’s duration, would be useful.

According to this post it seemed that TTS.google_say was not a valid notifier, and they went with node-red instead. But I will look into it! Thanks for your feedback.

Sure, I understand that it maybe isn’t highest priority. Still I think it’s a valid request.

Of course it is possible to do without a built in loop feature. But so is everything:)

Personally I would gladly learn python to be able to contribute, but not right there yet. But I will maybe try the code you supplied above as a start :slight_smile:

1 Like

Ou okay didn’t know that, thought you wanted a notification on the phone.

No, the message should be broadcasted on all my google home minis around the house. So it does not seem the solution works for this case. So either this pull request are more valid, or support for tts.google_say could/should maybe be added to the alert integration instead.

If I would need to do something like this with native HA automations (I do this with AppDaemon and there it is “easy” to do something like this), I would create or look for a song and then play that song on repeat as long as ther is an intruder and stop it when the alarm system has been disarmed instead of sending ao many TTS.

1 Like

now remember this thread. I do what you want, I think, using 3 scripts:

  sound_bite:
    alias: Sound bite
    sequence:
      - service: media_player.play_media
        data_template:
          entity_id: >
            {{states('sensor.sound_bite_player')}}
          media_content_id: >
            {{states('sensor.sound_bite')}}
          media_content_type: 'music'

  play_sound_bite:
    alias: Play sound bite
    sequence:
      - service: script.sound_bite
      - delay:
          seconds: >
           {{state_attr(states('sensor.sound_bite_player'),'media_duration')|int}}
      - condition: state
        entity_id: input_boolean.loop_sound_bite
        state: 'on'
      - service: script.play_sound_bite_loop

  play_sound_bite_loop:
    alias: Play sound bite loop
    sequence:
      - service: script.sound_bite
      - delay:
          seconds: >
           {{state_attr(states('sensor.sound_bite_player'),'media_duration')|int}}
      - condition: state
        entity_id: input_boolean.loop_sound_bite
        state: 'on'
      - service: script.play_sound_bite

broadcast is easy, just create a group with all you media_players, and select that group in the top script.
I do that setting an input_select:

      sound_bite_player:
        friendly_name: Sound bite player
        value_template: >
          {% set state = states('input_select.sound_bite_player') %}
          {% if state in ['Hub','Woonkamer','Hobbykamer','Hall','Master bedroom','Office','Dorm 1','Dorm 2'] %}
            media_player.googlehome_{{(state)|lower|replace(' ','_')}}
          {% elif state == 'Gym' %} media_player.chromecastaudio_gym
          {% elif state == 'TheBoss' %} media_player.theboss
          {% else %} group.broadcast
          {% endif %}
        icon_template: >
           {% set mapper =
             {'Hub':'tablet-dashboard',
              'Woonkamer':'sofa',
              'Hobbykamer':'xbox',
              'Hall':'transit-transfer',
              'Master bedroom':'human-male-female',
              'Gym':'dumbbell',
              'Office':'desktop-mac',
              'Dorm 1':'human-child',
              'Dorm 2':'human-child',
              'TheBoss':'cellphone-iphone'} %}
           {% set state = states('input_select.sound_bite_player') %}
           {% set prefix = 'mdi:' %}
           {{[prefix,mapper[state]]|join if state in mapper else [prefix,'radio-tower']|join}}

as long as input_boolean.loop_sound_bite is ‘on’ this loops…

I also found this thread for other reasons than flashing a light. @Erlend is correct many other reasons…

And like Erland you have also overlooked to describe these “many other reasons”.

Nevertheless, in 0.113 it will contain the means to loop in a script so now it will be easier to flash a light … or do all those other things that have been alluded to but not actually described.

Perhaps its due to the type of users of HA. With an IT background we prefer to simplify problems instead of frivolous details. But, since you seem so concerned with the unimportant. I needed to loop a service call to set the volume on my Roku TV via 100x volume down calls and 30 volume up calls as Roku API has no volume set function. See Volume_Down Multiple times in one service call

Glad to see development has listened regardless and looking forward to cleaning up my code, but ideally my hack would be implemented in the Roku automatic setup for setting volume.

For someone who claims to have an IT background, it’s surprising that you said this in your other post:

but I don’t really understand yaml and how this coding language works TBH.

You have just described your own use of looping as “unimportant”.

Looping was always possible using python_script and I provided a simple example earlier in this thread consisting of four lines of python.

Here is a simple example of how to use the new repeat feature in a script (introduced in 0.113). It repeats (loops) a sequence 5 times. The sequence simply increments counter.test then waits 2 seconds.

#script:
  increment_by_5:
    sequence:
      repeat:
        count: 5
        sequence:
          - service: counter.increment
            entity_id: counter.test
          - delay: '00:00:02'

In an automation, you can call the script like this:

  action:
    service: script.increment_by_5

For more information, refer to the Script Syntax documentation.


Here is the same thing as a python_script:

increment_by_5.py

for i in range(5):
  hass.services.call('counter', 'increment', {'entity_id':'counter.test'}, False)
  time.sleep(2)

In an automation, you can call the python_script like this:

  action:
    service: python_script.increment_by_5

@123 You seem to want to argue (says the guy responding :stuck_out_tongue: , perhaps another IT trait?) with every person who has shown thier desire for this feature, isn’t that the point of a feature request thread & poll? Just because somebody can’t list three edge cases doesn’t mean there isn’t enough to justify it’s integration into HA. Is my use case not a perfect example?

@123 For someone who claims to have an IT background, it’s surprising that you said this in your other post:

but I don’t really understand yaml and how this coding language works TBH.

Ask me to script deployment of a High Availablility Galara SQL Cluster, sure, no problem, write a bash script to monitor it? Yeah ok. Deploy a Windows Domain, OK! Do I need to know everything to have an IT background?

I struggle with YAML formatting (read the forum and you will see I’m not alone). I generally use the UI to generate configs, then edit them via CLI, then use an online formatter to make it work. I suspect many users “limp” along fine in a similar fashion. I have no desire/time to perfect my understanding of YAML just for HA.

@123 You have just described your own use of looping as “unimportant”.

What I described was the “unimportant” details of WHY I desired the feature. My comment was simply mean’t to show my vote/desire to see it implemented, I didn’t feel it was necessary to go into details as to why.

@123 Looping was always possible using python_script and I provided a simple example earlier in this thread consisting of four lines of python.

I’d rather not introduce python scripts into a setup that doesn’t utilize them currently, while my solution of pasting code repeatedly was ugly, since I was able to split configs, it was bearable. But, I’m pulling the latest docker HA image to hopefully test the new “repeat/loop” config. Thank you for your example configuration, it will save me time “limping” along with my clean up of my code <3

@123 The new feature works Amazing! Could you explain how I could make the “Count” an argument that could be passed when calling the script for the “VolumeUp” portion of my setup? I currently utilize individual scripts for each volume setting, but that is less than ideal. If not, I’ll try and figure it out and post back!

I got it, not to hard.

w/ Count as Argument:

  volup:
    sequence:
      repeat:
        count: '{{ count }}'
        sequence:
          - service: media_player.volume_up
            entity_id: media_player.freesoftwareservers

I want to make it clear that looping was always available in Home Assistant, using either python_script or automations based on timer events. In addition, the number one, most frequently requested application of looping is … flashing a light. That’s why I asked that someone provide a few other real-world home automation applications for repeating the same thing over and over. I thank you for offering at least one other application … and it’s a good example.

python_script is just another integration. Everything in Home Assistant is an integration: Template Sensors, scripts, REST, etc.

Glad to hear it. Sounds like you’re more comfortable with YAML than you indicated.

1 Like