Personalize Telegram message with entity_id name

#1

Hi,

I'm using Home Assistant in conjunction with NodeRed. I have the following automation in place that sends a Telegram alert every time opens a blind (I have 4 blinds in the living room). The

The problem is don't know how to personalize the generic message I have right now ("Opening living room blinds") by including the name of the blind ("Opening living room blind 1/2/3/4").

image

How can I pass the name (entity_id) of the blind to the Telegram node and include it in the message?

Thanks a lot.

0 Likes

#2

The way I do it is to use a function or change node to change the payload, adding the data you want to the text string to be sent. You can also use simplistic HTML or Markdown as well but you can only do simple things like bold and italics.

Here are some example messages from one of my bots:

The bold titles are actually part of the payload.

1 Like

#3

Thanks, that function node seems like the way to go. Would you mind sharing some code examples for that node?

0 Likes

#4

I'll see what I can do, it is a collection of nodes and I make the telegram part a common target node by using lots of link nodes. But I'll see what I can extract.

0 Likes

#5

The problem I have is that all the information (I get this with a debug node) that I can see in the output of "Living room blind 1 - OPEN" is the following, but friendly name is not listed there:

object

payload: object

domain: "cover"

service: "open_cover"

data: object

entity_id: "switch.sonoff_ch4_1"

_msgid: "f910e57.eb50718"

I tried getting the friendly name at the output of this node with "data.attributes.friendly_name" but is not working.

0 Likes

#6

In the debug node you should be able to click on a small arrow to “open” the ones that say object to see the nested value.

(see the page on working with messages in the documentation)

0 Likes

#7

That was exactly mi issue, that info was not included in the output (even if I open/display the whole output).

I solved it with:

And this the content of the function node:

msg.payload = {}
var my_friendly_name = msg.data.attributes.friendly_name;
msg.payload.chatId = MY_ID
msg.payload.type = "message"
msg.payload.content = "Opening: " + my_friendly_name
return msg;

Note that I needed to use the "Telegram Sender" node. As the one I was using (Notify) was not capable of injecting a custom message. It needed to be defined as a text/string in the node itself.

0 Likes

#8

Here is an example function node:

/**
 * Get all latest humidities and save to global var for replay.
 * WARNING: Requires Node.JS v8+
 **/

let currentHumidities = global.get('currentHumidities') || {}

let payload = ''
let locns = Object.keys(currentHumidities).sort()

locns.forEach(function(locn) {
    let h = Math.round(currentHumidities[locn]).toString().padStart(3)
    payload += `\`${locn.padEnd(15)}: ${h}\`
` // Note the newline, also note text is wrapped in `` as output is markdown - this makes the text fixed width
});

msg.payload.content = "*Current Humidities are*:\n" + payload

msg.payload.options = { "parse_mode": "Markdown" }

msg.topic = 'TELEGRAM/myBOT/Current_House_Humidities'

return msg;

// EOF

Note the requirement for at least Node.js v8 due to back-ticks. Also note the embeded markdown.

0 Likes