Announce Bus timing on Alexa via Script using Sensor State on HA

Hi guys,

i am trying to do a automation whereby I request Alexa to announce Bus Arrival timing at a particular Bus Stop. I have done the integration and setup as a sensor.

There are total of 3 Sensors, indicating the 1st, 2nd, and 3rd Bus arrival times in minutes

1st Bus
sensor.lta_04168_67_1

2nd Bus
sensor.lta_04168_67_2

3rd Bus
sensor.lta_04168_67_3

Each Sensor can report in Mins in Integer value e.g. 1-20 etc. The Sensor will indicate ‘ARR’ if the Bus has arrived at the bus stop.

Im trying to trigger a Notification announcement on Alexa through this Script that i have created in HA.
So when triggered, if will check the 1st Bus timing if it shows ARR, then it will announce the 2nd bus timing instead. If the 1st Bus timing is a number, then it will announce the msg in minutes.

I’m trying to figure out the right If Else syntax to get this to work.

service: notify.alexa_media
data:
  message: >-
      {% if (states('sensor.lta_04168_67_1') == 'ARR'  %}
 Next 67 Bus to City Hall MRT Station has arrived at Bustop. The next 67 Bus will be arriving at  {{states('sensor.lta_04168_67_2') }} minutes
   {% elif (states('sensor.lta_04168_67_2') == 'ARR' %}
 Next 67 Bus to City Hall MRT Station has arrived at Bustop. The next 67 Bus will be arriving in   {{states('sensor.lta_04168_67_2') }} minutes
     {% else %}
 Next 67 Bus to City Hall MRT Station arriving in {{states('sensor.sensor.lta_04168_67_1’) }} minutes
     {% endif %}
  data:
    type: announce
  target: media_player.bedroom_echo_dot

You’re not using the correct entity_id’s. You said its …1, …2, …3. Your if statements check …2, then …2, then spit out …1. Lastly, you have the wrong quote at the end of your …1 entity_id.

Usually, to help with things like this, you make variables so that you don’t have to get the syntax correct in 10 spots.

{% set bus1 = states('sensor.lta_04168_67_1') %}
{% set bus2 = states('sensor.lta_04168_67_2') %}
{% set bus3 = states('sensor.lta_04168_67_3') %}
{% if bus1 != 'ARR' %}
  {% set next_bus = bus1 %}
{% elif bus2 != 'ARR' %}
  {% set next_bus = bus2 %}
{% elif bus3 != 'ARR' %}
  {% set next_bus = bus3 %}
{% else %}
  {% set next_bus = none %}
{% endif %}
{% if next_bus %}
  Next 67 Bus to City Hall MRT Station has arrived at Bustop. The next 67 Bus will be arriving at  {{ next_bus }} minutes
{% else %}
  You missed all the busses 
{% endif %}

Now that’s a huge template, but it’s easy to understand. It can be reduced quite a bit but it will be harder to follow:

{% set next_bus = expand('sensor.lta_04168_67_1', 'sensor.lta_04168_67_2', 'sensor.lta_04168_67_3') 
                  | rejectattr('state','eq', 'ARR') | list | first | default(none) %}
{% if next_bus %}
  Next 67 Bus to City Hall MRT Station has arrived at Bustop. The next 67 Bus will be arriving at  {{ next_bus.state }} minutes
{% else %}
  You missed all the busses 
{% endif %}

Thanks for sharing the code, I find the 1st code much easier to understand and adapt.

service: notify.alexa_media
data:
  message: >-
{% set bus1 = states('sensor.lta_04168_67_1') %}
{% set bus2 = states('sensor.lta_04168_67_2') %}
{% set bus3 = states('sensor.lta_04168_67_3') %}
{% if bus1 != 'ARR' %}
  {% set next_bus = bus1 %}
{% elif bus2 != 'ARR' %}
  {% set next_bus = bus2 %}
{% elif bus3 != 'ARR' %}
  {% set next_bus = bus3 %}
{% else %}
  {% set next_bus = none %}
{% endif %}
{% if next_bus %}
  Next 67 Bus to City Hall MRT Station has arrived at Bustop. The next 67 Bus will be arriving at  {{ next_bus }} minutes
{% else %}
  You missed all the busses 
{% endif %}  
  data:
    type: announce
  target: media_player.bedroom_echo_dot

I get this following error when i put this code in my script.

you have to indent the template under message

also, target has an incorrect indentation

EDIT: Sorry, this info is wrong. Wrong target. Yours is good. Sorry!