Do something when Alexa Timer has expired

OK, I think that was easier than I thought.

the key isn’t to use the state of the next timer but the time remaining value in the attribute of each timer.

try this automation to see if it works for you if you want to:

- alias: flash lights when echo timer is done
  trigger:
    - platform: state
      entity_id: sensor.garage_dot_next_timer
  condition:
    - condition: not
      conditions:
        - condition: state
          state: unavailable
          entity_id: sensor.garage_dot_next_timer
  action:
    - delay: >
        {% set index = (((state_attr('sensor.garage_dot_next_timer', 'sorted_active') | from_json)) | list | length) - 1 %}
        {{ ((state_attr('sensor.garage_dot_next_timer', 'sorted_active') | from_json)[index][1].remainingTime/ 1000)| round(0) }}
    - condition: not
      conditions:
        - condition: state
          entity_id: sensor.garage_dot_next_timer
          state: Unavailable
    - service: switch.toggle
      entity_id: switch.garage_lights_switch
    - delay:
        seconds: 1
    - service: switch.toggle
      entity_id: switch.garage_lights_switch
  mode: parallel
  max: 10

I tested it with two timers (the first was a one minute timer and the second was a two minute timer) and the lights flashed as expected without stopping either timer.

I have to correct us. Looks to me like it’s not correct after all. For me, only the variables declared under “variables:” work in the templates, while the variables defined under “input:” do not work in the templates. Can you check if this is the same for you?

Yes, this works. Great!

I have released a new version of this blueprint as a combination of our work.

1 Like

I’m sorry I don’t use blueprints…yet…I haven’t updated to the latest version. (TBH, I don’t know if it’s something I’ll ever use…)

It seems strange that only variables would work in templates in blueprints. I don’t know why they would be treated any differently than any other entity.

Have you found that referenced anywhere in the docs? Maybe it’s a bug?

Oh, and thanks for checking that out and adding it to your work.

Glad I could help after all of the wrong I was… :wink:

In the first condition, it refers to the sensor’s state value as lower case unavailable

        state: unavailable

In the second condition it refers to the same value as proper case Unavailable

        state: Unavailable

It can’t be both so which is the correct one for the Alexa Device? For most entities, it’s lower case.


If you’re interested, this bit of code:

  - choose:
    - conditions: '{{ True }}'      
      sequence: !input 'target_action'

can be reduced to this:

  - choose:
    default: !input target_action

Ideally it would be something simple like this:

  - sequence: !input target_action

Unfortunately, a standalone sequence key is not currently supported in this manner. The workaround is to use a bastardized choose.

1 Like

Good catch.

It actually is lower case.

I didn’t even actually use that in my test automation so I didn’t really look to closely at it. I didn’t see the need for the second condition check since it was already confirmed in the first one. Then I uncommented it back in when I posted since I realized it would be needed for times when you started a timer but then cancelled it before it had expired and there was no longer a reason to have the lights flash.

As for the rest of your post referencing the choose code…

Where are you seeing that used in this blueprint? it’s not in the OP or mine.

In this topic’s first post. The action contains a delay, two conditions, and a choose.

Thank you. I have optimized this in the blueprint.

Ah, I must not have refreshed my browser for this topic.

I kept looking and looking and never saw it. Until I just looed again after I came back and now it’s there.

Either that or I’m just finally starting to loose my mind…it honestly could go either way at this point. :smiley:

After blueprint import a new automation (action>call service>ligh.toggle>light) is created via GUI, but…
got this error:
Do something when Alexa Timer has expired: Error rendering Do something when Alexa Timer has expired delay template: UndefinedError: list object has no element -1

automation do not work at all. what am I missing here?

Please post a screenshot of your automation, then we can check.

you will see that if there is no timer set for the echo that is referenced in the blueprint and then try to run the associated automation manually.

try setting a timer and trying the blueprint again.

Tho I’m surprised that the blueprint config wouldn’t work and gave errors even tho the automation that used it wasn’t executed yet.

If the blueprint config actually checks the results of every template created within it before it is actually executed then I can see that being a big problem since there are a lot of templates that will throw an error because something might not exist before the automation is run.

And the condition that checks that the timer isn’t “unavailable” before evaluating the delay template eliminates that error.

Maybe I’ll revisit the template for the delay and put some error checking in it to eliminate the issue. But it still doesn’t seem right to me.

It implies the length of the sorted_active attribute was zero (or no such attribute).

{% set index = (((state_attr(timestamp_sensor, 'sorted_active') | from_json)) | list | length) - 1 %}

0 - 1 = -1

Which is the case when there is no next alarm.

That’s what the post above was about.

@AndrejDelany

try this new template for the delay:

it should always give an integer (default 0) even if there is no timer set:

{% if states('sensor.garage_dot_next_timer') != 'unavailable' %}
  {% set index = (((state_attr('sensor.garage_dot_next_timer', 'sorted_active') | from_json)) | list | length) - 1 %}
  {{ ((state_attr('sensor.garage_dot_next_timer', 'sorted_active') | from_json)[index][1].remainingTime/ 1000)| round(0) }}
{% else %}
  0
{% endif %}

oh, silly me. Tnx! I apologize.
Now works great!

I still think I need some clarification on this…

did the error occur upon creation of the automation based on the blueprint when you tried to save it?

Or did the error occur after you tried to run the automation (by setting new timer) the first time?

error: “list object has no element -1” appeared after I created automation with sensor.next_alarm and not sensor.next_timer
Now with correct sensor.next_timer used, error is gone.
It works for timer incl seconds too. it’s precise enough.
TnxUall

Then I still have to say that is potentially going to be a huge problem for more complex blueprints for use in automations. Sometimes you really need to rely on the fact that the template only gets rendered when other conditions are satisfied.

Automations have never worked like that. I wonder why they changed that