Including a message payload in a Telegram body

How do I include a variable from a sensor in node red to put it into a Telegram message body?
I presume this should apply to any Call Service node.

If I fill the data field for a Call Service node with this:
{“title”:“HA Alert!”,“target”:“555555555”,“message”:“Fridge was opened. Temperature=”}
then it works fine but I want to add the temperature which is the msg.payload which is the input to the node and is a string containing a temperature from a sensor.

I have tried

{“title”:“HA Alert!”,“target”:“555555555”,“message”:“Fridge was opened” + [msg.payload]}
and
{“title”:“HA Alert!”,“target”:“555555555”,“message”:“Fridge was opened” + msg.payload}
but neither of them work.
I also tried building the whole Telegram Data field in a function as an input to the Call Service node & it looks good as a debug output:

6/15/2018, 7:19:59 PMnode: 3: Fridge Temperature
sensor.fridge_temp : msg.payload : Object
{ title: “HA Alert!”, target: “555555555”, message: “Fridge open. T = 3.18 °C” }

but I can’t see how to put that function output into the Call Service node Data field.

Are you using the telegram connection from your HA instance?

I prefer to let Node-Red handle the connection to telegram.

or

2 Likes

Ah, thank you - that was a bit of a learning curve but I did find a different Telegram reference in the palette and used that and it does work. Now I can just use a function like this:

var mypayload = msg.payload;
msg.topic = “HA Alert!”
msg.payload = “Fridge open! Temperature=” + mypayload + " °C"
return msg;

hi. i am having the same problem.
i am trying to send a message to my telegram notification but am not able to do so. can you please guide me for the same. i am trying to use the Telegram_bot node in node-red for the same. attaching the snapshots.
@Kermit can you plz guide on this?
i am trying to retrieve the switches which are on currently and pass the message to my Telegram notification.
request your help.!

Any guides on getting this to work? I have the basic telegram notify setup but I have to type in the specific message for each notification. I installed chatbot but can’t seem to get the dynamic notifications working. I want to pass on my msg.text based on which sensor was activated.

What problem are you having with this?

I’ve been doing this for quite some time

I don’t know how to get the dynamic message to send on either the chatbot nor telegrambot. I prefer telegram bot as it’s pretty straight forward. I enter a message and it sends, but I want it to change the message based on the sensor that has been triggered like I do for my android notifications. For example:

[{"id":"ce4b87bd.a6f3a8","type":"server-state-changed","z":"dbe7bd4a.b13108","name":"Front Door","server":"e8627faa.c1d02","version":1,"entityidfilter":"binary_sensor.front_door","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":160,"y":920,"wires":[["deb25907.26d13"],[]]},{"id":"813daa7d.b915e","type":"server-state-changed","z":"dbe7bd4a.b13108","name":"Back Door","server":"e8627faa.c1d02","version":1,"entityidfilter":"binary_sensor.back_door","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":160,"y":960,"wires":[["eee23138.dd68b8"],[]]},{"id":"8eb58cce.d620a","type":"server-state-changed","z":"dbe7bd4a.b13108","name":"Garage Door","server":"e8627faa.c1d02","version":1,"entityidfilter":"binary_sensor.garage_door","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":170,"y":1000,"wires":[["8c12ed07.404388"],[]]},{"id":"34e7304b.14d748","type":"function","z":"dbe7bd4a.b13108","name":"Data","func":"msg.payload = \n{\n  \"data\": \n  {\n    \"message\": msg.text,\n  }\n}\nreturn msg;","outputs":1,"noerr":0,"x":990,"y":940,"wires":[["2a93e4f5.aec034"]]},{"id":"3824190e.bbab4e","type":"change","z":"dbe7bd4a.b13108","name":"Front Door","rules":[{"t":"set","p":"text","pt":"msg","to":"Alarm Triggered Front Door","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":900,"wires":[["34e7304b.14d748"]]},{"id":"ac32ff53.2fe328","type":"change","z":"dbe7bd4a.b13108","name":"Garage Door","rules":[{"t":"set","p":"text","pt":"msg","to":"Alarm Triggered Garage Door","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":980,"wires":[["34e7304b.14d748"]]},{"id":"2cea3c68.68726c","type":"change","z":"dbe7bd4a.b13108","name":"Back Door","rules":[{"t":"set","p":"text","pt":"msg","to":"Alarm Triggered Back Door","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":940,"wires":[["34e7304b.14d748"]]},{"id":"70420ed8.2d705","type":"api-current-state","z":"dbe7bd4a.b13108","name":"triggered?","server":"e8627faa.c1d02","version":1,"outputs":2,"halt_if":"triggered","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"alarm_control_panel.home_alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":920,"wires":[["3824190e.bbab4e"],[]]},{"id":"445ba041.4c79f8","type":"api-current-state","z":"dbe7bd4a.b13108","name":"triggered?","server":"e8627faa.c1d02","version":1,"outputs":2,"halt_if":"triggered","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"alarm_control_panel.home_alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":960,"wires":[["2cea3c68.68726c"],[]]},{"id":"40530942.36ddd","type":"api-current-state","z":"dbe7bd4a.b13108","name":"triggered?","server":"e8627faa.c1d02","version":1,"outputs":2,"halt_if":"triggered","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"alarm_control_panel.home_alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":1000,"wires":[["ac32ff53.2fe328"],[]]},{"id":"dd44d282.bf3608","type":"server-state-changed","z":"dbe7bd4a.b13108","name":"Great Room Motion","server":"e8627faa.c1d02","version":1,"entityidfilter":"binary_sensor.great_room_motion","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":190,"y":1040,"wires":[["5262c499.89ca5c"],[]]},{"id":"f475c9ae.6ab15","type":"api-current-state","z":"dbe7bd4a.b13108","name":"triggered?","server":"e8627faa.c1d02","version":1,"outputs":2,"halt_if":"triggered","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"alarm_control_panel.home_alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":570,"y":1040,"wires":[["6e86f511.69990c"],[]]},{"id":"6e86f511.69990c","type":"change","z":"dbe7bd4a.b13108","name":"Great Room Motion","rules":[{"t":"set","p":"text","pt":"msg","to":"Alarm Triggered Great Room Motion","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":1020,"wires":[["34e7304b.14d748"]]},{"id":"5262c499.89ca5c","type":"delay","z":"dbe7bd4a.b13108","name":"3s","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":370,"y":1040,"wires":[["f475c9ae.6ab15"]]},{"id":"8c12ed07.404388","type":"delay","z":"dbe7bd4a.b13108","name":"4","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":350,"y":1000,"wires":[["40530942.36ddd"]]},{"id":"eee23138.dd68b8","type":"delay","z":"dbe7bd4a.b13108","name":"3s","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":350,"y":960,"wires":[["445ba041.4c79f8"]]},{"id":"deb25907.26d13","type":"delay","z":"dbe7bd4a.b13108","name":"3s","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":350,"y":920,"wires":[["70420ed8.2d705"]]},{"id":"2a93e4f5.aec034","type":"api-call-service","z":"dbe7bd4a.b13108","name":"Notify Android","server":"e8627faa.c1d02","version":1,"service_domain":"notify","service":"mobile_app_ryan_s_pixel_2","entityId":"","data":"{}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1200,"y":940,"wires":[[]]},{"id":"e8627faa.c1d02","type":"server","z":"","name":"HA Fahrer","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

1 Like

You’ll need to create some flow to create the message.

I use a subflow and it uses topics to decide unit of measure, or verbiage, or even to modify the state.

What do you mean by “dynamic” message? Like the state of the object?

In my Android notification the message will change based on the sensor that is triggered. So if the front door opens it’ll say “alarm triggered front door”. If it’s the motion sensor it would say “alarm triggered great room motion” ect…

here is a portion of my setup.

inside Prep Chat subflow:

the link to the telegram sender:
image

examples of notifications to Telegram:
image

1 Like

Thanks could you send a copy of your actual flows so I can try to modify it for my setup?

I can only share my prep chat subflow. Everything else is so customized to my setup, including some personal information, that I can’t really share it.

[
    {
        "id": "caaac6d8.279038",
        "type": "subflow",
        "name": "Prep Chat",
        "info": "",
        "in": [
            {
                "x": 20,
                "y": 140,
                "wires": [
                    {
                        "id": "8d53840d.bbbfa8"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1460,
                "y": 120,
                "wires": [
                    {
                        "id": "fed78da3.bca8",
                        "port": 0
                    }
                ]
            }
        ]
    },
    {
        "id": "8d53840d.bbbfa8",
        "type": "function",
        "z": "caaac6d8.279038",
        "name": "friendly name",
        "func": "if(msg.data.attributes){\n    msg.entity = msg.data.attributes.friendly_name;\n} else if (msg.data.new_state) {\n    msg.entity = msg.data.new_state.attributes.friendly_name;\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 140,
        "y": 140,
        "wires": [
            [
                "1fe45b17.2d3b85"
            ]
        ]
    },
    {
        "id": "59081597.6ee05c",
        "type": "debug",
        "z": "caaac6d8.279038",
        "name": "Prep Chat",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 1420,
        "y": 180,
        "wires": []
    },
    {
        "id": "c471888.d318a78",
        "type": "change",
        "z": "caaac6d8.279038",
        "name": "find set temp",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "data.new_state.attributes.temperature",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 570,
        "y": 140,
        "wires": [
            [
                "1d733de4.1a6ce2"
            ]
        ]
    },
    {
        "id": "1fe45b17.2d3b85",
        "type": "function",
        "z": "caaac6d8.279038",
        "name": "what device",
        "func": "if(msg.topic.includes(\"climate.downstairs_thermostat_\")||msg.topic.includes(\"climate.upstairs_thermostat_\")){\n    msg.type = \"thermostat\";\n    return [null, msg, null, null];\n}\nelse if(msg.topic.includes(\"_temp\")){\n    msg.type = \"temperature\";\n    return [null, null, msg, null];\n}\nelse if(msg.topic.includes(\"_phone_battery\")||msg.topic.includes(\"iphone_battery_level\")||msg.topic.includes(\"_battery\")){\n    msg.type = \"battery\";\n    return [null, null, null, msg];\n}\nelse if(msg.topic.includes(\"person.\")){\n    msg.type = \"person\";\n    return [msg, null, null, null];\n}\nelse { return [msg, null, null, null]; }",
        "outputs": "4",
        "noerr": 0,
        "x": 310,
        "y": 140,
        "wires": [
            [
                "abbbd0b7.04188"
            ],
            [
                "c471888.d318a78"
            ],
            [
                "1d733de4.1a6ce2"
            ],
            [
                "eceeb643.a8c838"
            ]
        ],
        "outputLabels": [
            "state",
            "thermostat",
            "temp reading",
            "phone battery"
        ]
    },
    {
        "id": "abbbd0b7.04188",
        "type": "function",
        "z": "caaac6d8.279038",
        "name": "what verb?",
        "func": "if(msg.topic.endsWith(\"_home_status\")){\n    if(msg.payload === 'on'){\n        msg.payload = 'home';\n    } else if(msg.payload === 'off'){\n        msg.payload = 'not home'\n    }\n    msg.entity = msg.entity.split(\" \")[0];\n    msg.verb = 'is';\n}\nif(msg.topic.includes(\"person.\")){\n    if(msg.payload === 'not_home'){\n        msg.payload = 'not home';\n        msg.verb = 'is';\n    }\n    else if(msg.payload !== 'home'){\n        msg.verb = 'is at'\n    }\n    else if(msg.payload === 'home'){\n        msg.verb = 'is'\n    }\n    msg.entity = msg.entity.split(\" \")[0];\n}\nelse if(msg.topic.endsWith(\"alarm_control_panel.house_alarm\")){\n    if(msg.payload === 'armed_home'){\n        msg.payload = 'armed home'\n    }\n    else if(msg.payload === 'armed_away'){\n        msg.payload = 'armed away'\n    }\n    else if(msg.payload === 'pending'){\n        msg.payload = 'arming in 60 seconds'\n    }\n    msg.verb = 'is';\n}\nelse if(msg.entity.endsWith(\"s\")){\n    msg.verb = \"are\";\n}\nelse if(msg.payload == 'Breached'){\n    msg.verb = \"has been\";\n}\nelse if(msg.type == \"thermostat\"){\n    msg.verb = \"set temp is\";\n}\nelse if(msg.type == \"battery\"){\n    msg.verb = \"is at\";\n}\nelse { msg.verb = \"is\"; }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 970,
        "y": 120,
        "wires": [
            [
                "4c3b13a0.ddb33c"
            ]
        ]
    },
    {
        "id": "1d733de4.1a6ce2",
        "type": "function",
        "z": "caaac6d8.279038",
        "name": "add UOM to payload",
        "func": "if(msg.data.attributes){\n    var val = msg.payload;\n    msg.payload = val + msg.data.attributes.unit_of_measurement;\n} else if (msg.data.new_state) {\n    if(msg.data.new_state.attributes.temperature && !msg.topic.includes(\"_battery_sensor\")){\n        var val = msg.data.new_state.attributes.temperature.toString();\n        msg.payload = val + \"°F\";\n    } else { \n        var val =  msg.data.new_state.state; \n        msg.payload = val + msg.data.new_state.attributes.unit_of_measurement;\n    }\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 780,
        "y": 180,
        "wires": [
            [
                "abbbd0b7.04188"
            ]
        ]
    },
    {
        "id": "eceeb643.a8c838",
        "type": "function",
        "z": "caaac6d8.279038",
        "name": "batt_hits_25",
        "func": "if (msg.data.old_state.state == \"26.0\" && msg.data.new_state.state == \"25.0\"){\n    return msg;\n} else { return null;}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 570,
        "y": 220,
        "wires": [
            [
                "1d733de4.1a6ce2"
            ]
        ]
    },
    {
        "id": "4c3b13a0.ddb33c",
        "type": "change",
        "z": "caaac6d8.279038",
        "name": "set message",
        "rules": [
            {
                "t": "set",
                "p": "message",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1130,
        "y": 120,
        "wires": [
            [
                "fed78da3.bca8"
            ]
        ]
    },
    {
        "id": "fed78da3.bca8",
        "type": "template",
        "z": "caaac6d8.279038",
        "name": "Entity is/are status",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{{entity}}} {{verb}} {{message}}",
        "output": "str",
        "x": 1310,
        "y": 120,
        "wires": [
            [
                "59081597.6ee05c"
            ]
        ]
    }
]
1 Like

Thats understandable. When I go to import from clipboard it fails to show up. Normally I choose current flow, but I tried new flow and current flow and it still won’t show.

I don’t know man. I just exported the current flow and pasted it in the code blocks

I found it when I searched, but for some reason it doesn’t show up automatically. Thanks for posting. Looks a little more involved than I was hoping, but Ill take a look and try to break it down. I am pretty new to all of this.

It might be more involved at first,but I think I created this subflow more than a year ago and only have tweaked a couple of things since then. It definitely just works for me.

Thanks for all the help! I got it working. Im still trying to learn all the formatting, but it’s up and running and I’ve been able to make a few changes to fit my needs.