Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fire "alexa_media_last_called_event" after update #2522

Merged
merged 2 commits into from
Sep 14, 2024

Conversation

danielbrunt57
Copy link
Collaborator

This addition will allow scripts/automations which reference the notify.alexa_media_last_called service or the templated sensor.last_alexa sensor to wait for this event to fire before proceeding with their actions. Previously, everyone was using a hard coded delay to ensure the update last_called had finished. I had tried watching timestamps but this was only reliable when the timestamp actually changed which sometimes did not happen. With this feature the reaction time to actions will be a variable delay yet reliable. I've implemented this with a timeout on the wait for event and if it times out, I bypass all the notify/TTS actions.

@alandtse alandtse changed the title feat: fire event "alexa_media_last_called_event" when last_called has finished updating feat: fire "alexa_media_last_called_event" after update Sep 14, 2024
@alandtse alandtse merged commit 95f1b16 into alandtse:dev Sep 14, 2024
3 checks passed
@sumoneelse
Copy link

sumoneelse commented Sep 16, 2024

This is awesome. No more hard-coded delays! Here is a script I'm using now to update sensor.last_alexa:

alias: Echo Device Last Called Update
sequence:
  - parallel:
      - action: alexa_media.update_last_called
        data: {}
      - alias: Wait for alexa_media_last_called_event to fire
        wait_for_trigger:
          - platform: event
            event_type: alexa_media_last_called_event
        continue_on_timeout: true
mode: single

Dropping this here in case it helps anyone and if anyone has any suggestions for improvement.

@garysargentpersonal
Copy link

garysargentpersonal commented Sep 22, 2024

@danielbrunt57 @sumoneelse I've been using this for a few days now and while it is reliable, I have noticed that waiting for the event to fire is significantly a longer delay then the old method of waiting for the timestamp to change. With the event it's around 4.5 seconds from finishing speaking to Alexa to a lamp turning on. With waiting for the timestamp change it is around 2 to 2.5 seconds.

As a result I've modified my script to now detect EITHER the timestamp changing or the event firing, which should be the best of both worlds.

I don't know if there is a way to make the event firing quicker? Also why do we need to do this at all, can't the call to update alexa's block instead of us having to wait like this?

alias: Update Last Called Alexa and wait for result
sequence:
  - parallel:
      - data: {}
        action: alexa_media.update_last_called
      - wait_for_trigger:
          - platform: event
            event_type: alexa_media_last_called_event
          - platform: state
            entity_id:
              - sensor.last_alexa
            attribute: last_called_timestamp
        continue_on_timeout: true
        timeout:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
mode: single

Where sensor.last_alexa is:

template:
  - sensor:
    - name: Last Alexa
        unique_id: last_alexa
        icon: phu:amazon-logo
        state: >
          {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first | default(none) }}
        availability: >
          {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_timestamp') | first is defined }}
        attributes:
          last_called_timestamp: >
            {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_timestamp') | first | default(none) }}
          actual_friendly_name: >
            {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.friendly_name') | first | default(none) }}

@sumoneelse
Copy link

Thanks for the suggestion for improvement! I implemented the timestamp trigger in parallel with the last_called_event and watched in the UI to see which one was caught while I issued several commands.

Screenshot 2024-09-22 104217

Without exception it was the last_called_event for me. I even disabled the last_called_event trigger to verify there was nothing wrong and observed the timestamp trigger was working but slower.

I am going to leave timestamp there as belt-and-suspenders never hurts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants