Using Data_Template to pass in Friendly Name to TTS

So I am trying to do the following:

Have 3 binary_door sensors monitored and when a door is opened or closed send a TTS to Google Say to say “The <friendly_name> was opened/closed”.

I am trying to use a data_template node to create the payload as follows:

{ “data_template”: {
“message”:“The {{trigger.to_state.attributes.friendly_name}} was opened” }
}

When I debug the template output, the {{trigger.to…}} either isn’t being evaluated or is returning a blank value.

I know I can do it more brute force method, but this seems a lot cleaner if I can get it working.

I basically use the exact same flow as you for the same purpose but use a function node instead of a template node.

Here’s my function code

const friendlyName = msg.data.new_state.attributes.friendly_name;
node.status({text: friendlyName});
return {
        "payload": {
           "data": {
               "message": friendlyName + " opened"
           } 
    }
};

Not saying you have to use a function node but try the same path in your template node

data.new_state.attributes.friendly_name

Edit:

That location is based off my node-red fork. If you’re using the original try payload.data.new_state.attributes.friendly_name

2 Likes

Thanks - This worked perfectly. Now my 2 year old has no chance of sneaking out without us knowing during the day :smile:

Here is the final Function node-red code for those interested:

const friendlyName = msg.data.new_state.attributes.friendly_name;
node.status({text: friendlyName});
return {
        "payload": {
           "data": {
               "message": "The " + friendlyName + " was opened"
           } 
    }
};
5 Likes

How is the “TTS Google Play” set up? I would like to adapt for Alexa

Not sure what you mean how is it setup??

Google is enabled via HA Cloud. I have 3 different Google Home Mini’s in the house.

I just have it using the Call Service node to the TTS domain, and then the google_say service.

hey,

Whats the syntax you used for google tts?

Here is how I do this with a (node-red not HA) template node, for a notification if something goes offline:

🏠⚠ {{data.new_state.attributes.friendly_name}} was turned {{payload}}

In a template you refer to the values without the msg. on the front

I just set the payload since I have a function that processes notifications to some other criteria, but you can change which property in the node options. ie msg.payload.data.message

I use node red too.
Do you mind posting your flow and json please?

I’m trying to announce the weather, what tvshows or movies has been downloaded etc.

I just use the following:

google_say

And payload is the text.

@MwC-Trexx thanks this helped me tonight!

Thanks a lot! Searched for exactly this notification, thanks mate :wink:
I’m using it to monitor different lights in the house, added the state, to which the light switched.

const friendlyName = msg.data.new_state.attributes.friendly_name;
const state = msg.payload
node.status({text: friendlyName});
return {
        "payload": {
           "data": {
               "message": friendlyName + " switched " + state
           } 
    }
};
1 Like

Glad it was useful

Thank you both, I was planning to do exactly this but hadn’t had time to look into setting it up.
With this is only took me a few seconds to do.

Thanks again

@MwC-Trexx can you share your flow please?