hasl-sensor / integration

Swedish Public Transport Sensor (HASL). Formerly named HomeAssistant SL Sensor
https://hasl.sorlov.com
Apache License 2.0
36 stars 8 forks source link

Voice assistant support #53

Closed BoKKeR closed 9 months ago

BoKKeR commented 1 year ago

This is outside of the scope of this project, but I was wondering if anyone managed to get Alexa or other voice assistant to return the data about the upcoming busses using this sensor. I have the sensor and voice assistant exposed to each other. I am just missing the phrase that would activate it

DSorlov commented 1 year ago

Any suggestion on how and what is needed? I have no insights into if a sensor value could be used? Anyone

BoKKeR commented 9 months ago

I found a way to make it work! This code only works with one bus, in my case I will set it to 999, you can change the bus number in the script

service: tts.speak
target:
  entity_id: tts.google_en_com
data:
  message: >
    {% set departures = state_attr('sensor.sl_departure_sensor',
    'departures') | selectattr('line', 'equalto', '999') | list %} {% if
    departures %}
      {% set next_departure = departures[0] %}
      {% set departure_time = next_departure.expected %}
      {% set departure_datetime = departure_time %}
      {% set now = now().replace(tzinfo=None) %}
      {% set minutes_until_departure = int(((departure_datetime - now).total_seconds() // 60)) %}
      leaves in {{ minutes_until_departure }} minutes to {{ next_departure.destination }} number 999.
    {% else %}
      No line 999 bus departures found.
    {% endif %}
  media_player_entity_id: media_player.dining_room
  cache: true
  language: en