Read value_template on speaker in automation

Goal : I am trying to have my Google Nest Mini read the number of minutes till the next bus in an automation :bus:

Here is what I have successfully done so far :slight_smile:
I get the bus schedule from an open REST API with the following piece of code in my configuration.yaml :

sensor:
  - platform: rest
    resource: https://api-ratp.pierre-grimaud.fr/v4/schedules/buses/120/bry-sur-marne/R
    name: RATP Bry
    value_template: "{{ value_json.result.schedules[0].message[:-3] }}"

If you open the resource url above, you’ll see a json like this :

{
    "result": {
        "schedules": [
            {
                "message": "11 mn",
                "destination": "Nogent-Sur-Marne RER"
            },
            {
                "message": "31 mn",
                "destination": "Nogent-Sur-Marne RER"
            }
        ]
    },
    "_metadata": {
        "call": "GET /schedules/buses/120/bry-sur-marne/R",
        "date": "2021-08-14T19:40:55+02:00",
        "version": 4
    }
}

My value_template (trimmed with [:-3]) would then return 11 in that case (the number of minutes till the next bus) :tada:

In my automation, I expect to trigger it with a physical switch eventually but in the mean time, I’ve been testing with a dummy trigger that does work :

platform: state
entity_id: sensor.ratp_bry
from: '12'
to: '11'

Yes, it’s very clumsy but it proves to me that I can fetch the value in minutes from my value_template :blush:

In the Actions of my automation, I would like to have something like this (following code NOT working) :

service: tts.google_say
data:
  message: Bus coming in {{ value_template }} minutes
  entity_id: media_player.salon

(note : if I remove {{ value_template }} from the message above, my speaker does talk to me)

:arrow_forward: What would be the correct syntax to call the number 11 in that message ? :pray:

  message: 'Bus coming in {{ trigger.to_state.state }} minutes'
1 Like

Thank you very much for your quick answer : it works like a charm ! :+1:

I improved my routine to trigger it on the click of an Aqara switch.
Note the message that now contains {{ states('sensor.ratp_bry') }}

alias: 'Bus : announce next bus on button click'
description: ''
trigger:
  - device_id: 2ff14c779a5287c4e9079af97ce9ba5d
    domain: deconz
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - service: tts.google_say
    data:
      message: Bus coming in {{ states('sensor.ratp_bry') }} minutes
      entity_id: media_player.salon
mode: single