How to trigger Automation if another automations actions ran to many times in a given time frame?

I have an automation that fires every 15 seconds as my daughter’s alarm. There is a condition that checks to see if her Echo is already playing a song. However, at times the Alexa plug in doesn’t work correctly and keeps reporting Idle, so every 15 seconds it starts a new song.

Only way to fix this is to reboot HA. I would like to see if the alarm automation actions ran 3 times in last minute, and if so reboot HA. I know how to reboot HA, but not sure how to set up the trigger/conditions.

Any help would be appreciated.

Edit: As requested the automation

- id: '1586455192037'
  alias: School - Robin Wake Music
  description: ''
  trigger:
  - platform: time_pattern
    seconds: /15
  condition:
  - condition: state
    entity_id: calendar.noschool
    state: 'off'
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: time
    after: 05:55:00
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    before: 06:30:00
  - condition: or
    conditions:
    - condition: state
      entity_id: media_player.robin_echo
      state: paused
    - condition: state
      entity_id: media_player.robin_echo
      state: idle
    - condition: state
      entity_id: media_player.robin_echo
      state: standby
  action:
  - service: switch.turn_on
    target:
      entity_id:
      - switch.echo_spare_room
      - switch.robin_tree_1
  - service: light.turn_on
    data: {}
    target:
      entity_id:
      - light.robin
      - light.robin_closet
      - light.robin_chandelier
  - service: notify.alexa_media
    data:
      data:
        type: announce
      message: It is {{ now().strftime('%H:%M')}}, time To Wake up
      target:
      - media_player.robin_echo
  - service: media_player.play_media
    data:
      media_content_id: "{{  [ \n  \"Cover me in Sunshine\", \n  \"See it, say it,\
        \ sign it by Jack Hartman\",\n  \"overweight hedgehog\",\n  \"where are you\
        \ Christmas\",\n  \"90 days\",\n  \"hurt by johnny cash\",\n  \"Fishy on Me\"\
        ,\n  \"someone you loved\",\n  \"frantic by Metallica\",\n  \"Enter sandman\"\
        ,\n  \"We built this city\",\n  \"Welcome to the Jungle\"] | random }}"
      media_content_type: AMAZON_MUSIC
    entity_id: media_player.robin_echo
  mode: single

Post the automation.

I have something similar.
I use a history stats sensor on a boolean that I turn on/off at the end of the automation.
In your case set the timeframe of the history stats sensor to one minute and it should give you the count.

Then as a condition set history stats sensor < 3

Added it to the post

What’s the reasoning behind triggering the automation every 15 seconds all day long instead of triggering it once at a scheduled time?

I tend to turn it off once the kids are up. I need it to play music for more then one time, and depending on the random song it plays I won’t know how long before the next trigger point.

Doesn’t the media_player have media_duration or something like that.
Seems like you should be able to use the duration to set a date time with the next activation time

No clue how to set the next activation time, but that does make me realize that I could set it to trigger when the media player stops, and at a start time to start the whole process off in the mornings.

Thought that wouldn’t tell me when I need to reboot, actually would make it so that the alarm stops playing all together when it doesn’t receive the change state.

In my opinion the reboot and alarm is two different things. They just share some points.

If you have an automation like this:

alias: New Automation
description: ''
mode: single
trigger:
  - platform: time
    at: '05:55'
condition:
  - condition: state
    entity_id: calendar.noschool
    state: 'off'
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: time
    after: '05:55'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    before: '06:30'
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - condition: state
    entity_id: media_player.robin_echo
    state: idle
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - service: hassio.host_reboot

Then it will trigger at the same time as the alarm (I suggest this is saved in a date time instead to update both automations at the same time).
Then if the state of the media player is not changed from idle (or off?) then it reboots.
I assume this is not an issue that happens 06:15 when it has been running for a while?

So (notwithstanding the reliability problem with the plug-in) you want the automation to start playing music continuously, stepping through a random selection of tunes, starting at 05:55 and stopping at 06:30?

Or did you mean something different by “more than one time”?

Maybe let me explain what the automation itself is trying to do (not my initial question)

At 5:55
Tell the child the current time
Play a random Song
when random song is done, repeat first two steps
Do this till it becomes 6:30

So my solution for this is what you see above, check every 15 seconds to see if a song is being played, it’s a week day and a school day,

I wasn’t trying to put it in the same automation, but a new one that would fire when the alarm executed to many times.

You are correct that it happens from the beginning, so I could in theory just check at 05:56 if the echo is idle and the other conditions, and if so reboot right then.

thanks for the different point of view! With this thread I should be able to get rid of the every 15 seconds and trigger off the the following with same conditions:

  1. Run at 5:55
  2. When player goes back to any state other then idle
  3. on start up (time range condition will catch this)

I would assume changing to the 3 triggers would take some CPU pressure of of HA since it’s not going to be checking every 15 mins.

I suggest this:

- id: '1586455192037'
  alias: School - Robin Wake Music
  description: ''
  trigger:
  - platform: time
    at: '05:55:00'
  condition:
  - "{{ is_state('calendar.noschool', 'off') }}"
  - "{{ is_state('binary_sensor.workday_sensor', 'on') }}"
  - "{{ 1 <= now().isoweekday() <= 5 }}"
  action:
  - variables:
      entities:
        - switch.echo_spare_room
        - switch.robin_tree_1
        - light.robin
        - light.robin_closet
        - light.robin_chandelier
      songs:
        - 'Cover me in Sunshine'
        - 'See it, say it, sign it by Jack Hartman'
        - 'overweight hedgehog'
        - 'where are you Christmas'
        - '90 days'
        - 'hurt by johnny cash'
        - 'Fishy on Me'
        - 'someone you loved'
        - 'frantic by Metallica'
        - 'Enter sandman'
        - 'We built this city'
        - 'Welcome to the Jungle'
  - service: homeassistant.turn_on
    target:
      entity_id: '{{ entities }}'
  - repeat:
      until: "{{ now().hour == 6 and now().minute == 30 }}"
      sequence:
      - service: notify.alexa_media
        data:
          data:
            type: announce
          message: "It is {{ now().strftime('%H:%M')}}, time To Wake up"
          target:
          - media_player.robin_echo
      - service: media_player.play_media
        target:
          entity_id: media_player.robin_echo
        data:
          media_content_type: AMAZON_MUSIC
          media_content_id: '{{ songs | random }}'
      - wait_for_trigger:
        - platform: state
          entity_id: media_player.robin_echo
          from: 'playing'
  mode: single

The automation might need some adjustment in case the initial announcement is clipped by the song.

It employs a repeat - until to perform the announcement and play a random song continuously until 06:30. The song is allowed to play until the media_player’s state changes from playing to anything else (meaning it just finished playing the song and is now idle or possibly paused).


EDIT

A more robust version of the repeat can employ while with a template that checks if the current time is between 05:55 and 06:30. I would also add an input_boolean, to the condition and to the until/while, to serve as a master control (normally it would be on but if you set it to off it would prevent the automation from executing or instantly terminate the repeat while in progress).

Honestly don’t care if it does, it will just say it over the song. I just want the 6 year old to get her ass out of bed!

I do see one possible issue, and bites me in the ass to this day, if I reboot in the time frame I don’t believe it will pick up as the trigger has already happened. I have another Automation that performs an action waits 10 mins, and does next action. If I reboot in that 10 min time span the next action doesn’t fire.

I think for this one, triggering at start up and at 5:55 will fix it for me though

thanks, I learned a bunch of new stuff here with the while/repeat. This will make the next alarm at 6:30 simpler as well. Currently it’s every 2 seconds, and just yells over Alexa “Get Down Stairs”, I’ll swap that to a repeat!

Any automation whose action is in progress, be it waiting for a delay or wait_template statement or performing a repeat or etc, will be cancelled by a Reload Automations or restart.

You don’t even have to explicitly execute Reload Automations for this to happen. If you use the Automation Editor, every time you create/modify then save an automation, it automatically executes Reload Automations (thereby cancelling any automations in progress).

For the example I posted, you would have to restart/reload moments before 05:55 for it to miss its scheduled Time Trigger (so that’s a very narrow window of opportunity for failure). However, if you reload/restart while it’s executing its action, between 05:55 and 06:30, it will suffer the fate of all other automations and will be cancelled.

If you’re interested, there are ways to mitigate this as well.

That’s been an issue forever and I’ve tried to offer suggestions to fix it but it seems no developer thinks it is a problem. :man_shrugging: