# Get current.state of an HA entity and send it to Telegram

**URL:** <https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [27 December 2024 21:41 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211 "2024-12-27T21:41:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bemo47](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bemo47/32/97561_2.png) [@bemo47](https://discourse.nodered.org/u/bemo47)\
**Post date:** [27 December 2024 21:41 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/1 "2024-12-27T21:41:37Z")

</div>

Hi,

Now, i am able to define a command for my bot and then when chosing an option, temperature for now, in telegram i would like to get the current.state of my sensor temperature.  
I can see that the value is correct in debug 12, but function 3 doesn't execute to format the answer to be sent to the telegram sender.

The tuto i was following was using a request to influxdb\_in instead of current.state to get the last value of my entity, but i wasn't able to make it working so i supposed that i can get the last value also with current.state....

If any advices....

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/101d9d5f02db4522c8f2a782cbf348d269356ad8.png)

```auto
27/12/2024 22:35:43noeud: debug 12
msg : Object
object
payload: "20.72"
originalMessage: object
id: "6031484955416982446"
from: object
id: 5699281820
is_bot: false
first_name: "Bernard"
language_code: "fr"
message: object
message_id: 50
from: object
id: 7451665920
is_bot: true
first_name: "Alerte"
username: "Alerte47bot"
chat: object
id: 5699281820
first_name: "Bernard"
type: "private"
date: 1735322010
text: "Selection?"
reply_markup: object
inline_keyboard: array[1]
0: array[2]
0: object
text: "Temperature"
callback_data: "1"
1: object
text: "Humidity"
callback_data: "2"
chat_instance: "6729422385743329509"
data: "1"
_msgid: "c627bcf6d38bbe28"
callback: object
chatId: 5699281820
messageId: 50
inlineMessageId: undefined
type: "callback_query"
content: "1"
callbackQueryId: "6031484955416982446"
from: object
id: 5699281820
is_bot: false
first_name: "Bernard"
language_code: "fr"
data: object
entity_id: "sensor.sonoff_salon_temperature"
state: "20.72"
attributes: object
state_class: "measurement"
unit_of_measurement: "°C"
device_class: "temperature"
friendly_name: "Sonoff Salon Température"
last_changed: "2024-12-27T21:31:09.287Z"
last_reported: "2024-12-27T21:31:09.287628+00:00"
last_updated: "2024-12-27T21:31:09.287Z"
context: object
id: "01JG50FHK7K1HH0RVPRZX4PSJB"
parent_id: null
user_id: null
timeSinceChangedMs: 273790
27/12/2024 22:35:43noeud: function 3
function : (error)
"TypeError: Cannot read properties of undefined (reading '0')"

```

```auto
[
    {
        "id": "5a5a89b553a3d32a",
        "type": "tab",
        "label": "Flux 5",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c23f3dd1f48cdd9b",
        "type": "telegram command",
        "z": "5a5a89b553a3d32a",
        "name": "",
        "command": "/data",
        "description": "",
        "registercommand": false,
        "language": "",
        "scope": "default",
        "bot": "f9e88713e8567cb0",
        "strict": false,
        "hasresponse": true,
        "useregex": false,
        "removeregexcommand": false,
        "outputs": 2,
        "x": 90,
        "y": 100,
        "wires": [
            [
                "92285107c7108b2d",
                "a75b5ef5793b9b88"
            ],
            [
                "a75b5ef5793b9b88"
            ]
        ]
    },
    {
        "id": "92285107c7108b2d",
        "type": "function",
        "z": "5a5a89b553a3d32a",
        "name": "function 1",
        "func": "var opts = {\n reply_markup: JSON.stringify({\n \"inline_keyboard\": [[\n {\n \"text\": \"Temperature\",\n \"callback_data\": \"1\"\n },\n {\n \"text\": \"Humidity\",\n \"callback_data\": \"2\" \n }]\n ]\n })\n};\n\nmsg.error = false;\n\nmsg.payload.content = 'Selection?';\nmsg.payload.options = opts;\n\nmsg.payload.chatId = 5699281820;\nmsg.payload.type = \"message\";\n\nreturn [msg];",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 100,
        "wires": [
            [
                "9f51b54ca5a55a95",
                "dadfb1f107a8a154"
            ]
        ]
    },
    {
        "id": "9f51b54ca5a55a95",
        "type": "telegram sender",
        "z": "5a5a89b553a3d32a",
        "name": "",
        "bot": "f9e88713e8567cb0",
        "haserroroutput": false,
        "outputs": 1,
        "x": 550,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "dadfb1f107a8a154",
        "type": "debug",
        "z": "5a5a89b553a3d32a",
        "name": "debug 9",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 470,
        "y": 200,
        "wires": []
    },
    {
        "id": "1fa16ed011242e3b",
        "type": "telegram event",
        "z": "5a5a89b553a3d32a",
        "name": "",
        "bot": "f9e88713e8567cb0",
        "event": "callback_query",
        "autoanswer": false,
        "x": 100,
        "y": 320,
        "wires": [
            [
                "09c95e6087d856db"
            ]
        ]
    },
    {
        "id": "09c95e6087d856db",
        "type": "function",
        "z": "5a5a89b553a3d32a",
        "name": "function 2",
        "func": "msg.callback = msg.payload;\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 330,
        "y": 340,
        "wires": [
            [
                "3a3f73ec1d478c96"
            ]
        ]
    },
    {
        "id": "c46b25fbd6946d20",
        "type": "function",
        "z": "5a5a89b553a3d32a",
        "name": "function 3",
        "func": "if(msg.callback.content == 1){\n msg.payload.content = msg.payload.result[0].series[0].values[0][4] + \" °C\";\n}\nif(msg.callback.content == 2){\n msg.payload.content = msg.payload.result[0].series[0].values[0][4] + \" %\";\n}\n\nmsg.payload.type = \"message\";\nmsg.payload.chatId = 5699281820;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 820,
        "y": 420,
        "wires": [
            [
                "f8ad4dda89077e04",
                "b985f2a46240be1b"
            ]
        ]
    },
    {
        "id": "f8ad4dda89077e04",
        "type": "telegram sender",
        "z": "5a5a89b553a3d32a",
        "name": "",
        "bot": "f9e88713e8567cb0",
        "haserroroutput": false,
        "outputs": 1,
        "x": 870,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "a75b5ef5793b9b88",
        "type": "debug",
        "z": "5a5a89b553a3d32a",
        "name": "debug 11",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 190,
        "y": 200,
        "wires": []
    },
    {
        "id": "3a3f73ec1d478c96",
        "type": "api-current-state",
        "z": "5a5a89b553a3d32a",
        "name": "",
        "server": "59781b09.703574",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.sonoff_salon_temperature",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 660,
        "y": 300,
        "wires": [
            [
                "c773343a607756f7",
                "c46b25fbd6946d20"
            ]
        ]
    },
    {
        "id": "c773343a607756f7",
        "type": "debug",
        "z": "5a5a89b553a3d32a",
        "name": "debug 12",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 200,
        "wires": []
    },
    {
        "id": "b985f2a46240be1b",
        "type": "debug",
        "z": "5a5a89b553a3d32a",
        "name": "debug 13",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 670,
        "y": 480,
        "wires": []
    },
    {
        "id": "f9e88713e8567cb0",
        "type": "telegram bot",
        "botname": "Alerte",
        "usernames": "Alerte47bot",
        "chatids": "56xxxxxx20",
        "baseapiurl": "",
        "testenvironment": false,
        "updatemode": "polling",
        "addressfamily": "",
        "pollinterval": "300",
        "usesocks": false,
        "sockshost": "",
        "socksprotocol": "socks5",
        "socksport": "6667",
        "socksusername": "anonymous",
        "sockspassword": "",
        "bothost": "",
        "botpath": "",
        "localbotport": "8443",
        "publicbotport": "8443",
        "privatekey": "",
        "certificate": "",
        "useselfsignedcertificate": false,
        "sslterminated": false,
        "verboselogging": false
    },
    {
        "id": "59781b09.703574",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 December 2024 21:59 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/2 "2024-12-27T21:59:20Z")

</div>

The error is in function 3 and suggest that msg.payload.result[0].series[0].values[0][4] is not the right path to the value.

With out a debug showing the full message going into the function it is hard to help.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

This is the way to copy the full message in a format that is helpful and has no ambiguity.

---

<div class="post-metadata">

**Author:** ![bemo47](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bemo47/32/97561_2.png) [@bemo47](https://discourse.nodered.org/u/bemo47)\
**Post date:** [27 December 2024 23:05 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/3 "2024-12-27T23:05:47Z")

</div>

hmm, i understand two things :

1 - i can get the path or the value by hovering above the small icons, ok  
2 - you need a better format of the full message shown in the debug 12 which is the entry to the function, but here i don't really understand how to copy the whole content of the debug in a formatted way.... This is what i tried above but all indents are lots...

If also i try to get the path of my value, here 20.87, the temperature, i get only "payload"...

But i agree that i start from far away and i am far from understanding all that...

Or maybe like below ? Is that a better format for this debug 12 ?

```auto
{"payload":"20.87","originalMessage":{"id":"6031484957904880899","from":{"id":5699281820,"is_bot":false,"first_name":"Bernard","language_code":"fr"},"message":{"message_id":50,"from":{"id":7451665920,"is_bot":true,"first_name":"Alerte","username":"Alerte47bot"},"chat":{"id":5699281820,"first_name":"Bernard","type":"private"},"date":1735322010,"text":"Selection?","reply_markup":{"inline_keyboard":[[{"text":"Temperature","callback_data":"1"},{"text":"Humidity","callback_data":"2"}]]}},"chat_instance":"6729422385743329509","data":"2"},"_msgid":"2c84af4958ec2699","callback":{"chatId":5699281820,"messageId":50,"type":"callback_query","content":"2","callbackQueryId":"6031484957904880899","from":{"id":5699281820,"is_bot":false,"first_name":"Bernard","language_code":"fr"}},"data":{"entity_id":"sensor.sonoff_salon_temperature","state":"20.87","attributes":{"state_class":"measurement","unit_of_measurement":"°C","device_class":"temperature","friendly_name":"Sonoff Salon Température"},"last_changed":"2024-12-27T22:56:10.551Z","last_reported":"2024-12-27T22:56:10.551527+00:00","last_updated":"2024-12-27T22:56:10.551Z","context":{"id":"01JG55B79QV9SVWCY9EF90E889","parent_id":null,"user_id":null},"timeSinceChangedMs":95024}}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 December 2024 00:03 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/4 "2024-12-28T00:03:15Z")

</div>

AS mentioned function 3 is trying to set payload.content to msg.payload.result[0].series[0].values[0][4].  
But msg.payload.result[0].series[0].values[0][4] does not exist.  
Payload is a float 20.87

The api current state is only returning the temperature in msg.payload (This is HA and I am not a user) and msg.data an object.

just to get function 3 working try.

```auto
msg.payload = {content: msg.payload};
msg.payload.type = "message";
msg.payload.chatId = 5699281820;
return msg;

```

You need to work out how to return a temp or humidity from you api current state call. This is a HA forum issue. You may find someone on this forum but most don't use HA. You could try directing to different api current state nodes, using a switch node to route if msg.callback.content is equal to "1" or "2" (strings).

---

<div class="post-metadata">

**Author:** ![bemo47](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bemo47/32/97561_2.png) [@bemo47](https://discourse.nodered.org/u/bemo47)\
**Post date:** [28 December 2024 00:18 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/5 "2024-12-28T00:18:05Z")

</div>

Ok, thanks again

Gemini has suggested to use a node "change" to build the msg to be sent to the sender, but i think the issue will be the same, concatenating a text with a float, or number....

I will explore on HA forum side.

edit :  
with your suggestion the number is returned !

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d965e34b257e4937a5fa8ac1e8e8e37bd46861e2.png)

Great !

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 December 2024 00:44 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/6 "2024-12-28T00:44:35Z")

</div>

> [@bemo47](#):
>
> sender, but i think the issue will be the same, concatenating a text with a float, or number....

You can concatenate with a  
function node [3 Ways to Concatenate Strings in JavaScript - Mastering JS](https://masteringjs.io/tutorials/fundamentals/string-concat)  
[Template literals (Template strings) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

Or in a change node using expression :J (JSONata)

> **[Other Operators · JSONata](https://docs.jsonata.org/other-operators#-concatenation)**
>
> \## \`&\` (Concatenation)

Or in a template node using Mustache  
[https://mustache.github.io/mustache.5.html](https://mustache.github.io/mustache.5.html)

---

<div class="post-metadata">

**Author:** ![bemo47](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bemo47/32/97561_2.png) [@bemo47](https://discourse.nodered.org/u/bemo47)\
**Post date:** [28 December 2024 00:55 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/7 "2024-12-28T00:55:04Z")

</div>

okkkkk

writing this :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d2549010c60e7271c82fceb858b2a075a0f52b06.png)

gives :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76f1352a8c27ce3ace5f42874ac3199e84eeb9f9.png)

which is a very good result for me

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [3 January 2025 18:12 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/8 "2025-01-03T18:12:53Z")

</div>

I see that you have already solved this however in the future, home assistant states can be accessed directly from a function node. First in your home assistant server node look to see if globals are enabled, if not check the box and restart.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed726d9002b9a2517432e2cf078a5ff20973244e.png)

Once that is done use the following to pull state/attribute of entity into a func node.

```auto
const phoneState = global.get('homeassistant.homeAssistant.states["sensor.pixel_7_phone_state"].state');

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 April 2025 18:13 UTC](https://discourse.nodered.org/t/get-current-state-of-an-ha-entity-and-send-it-to-telegram/94211/9 "2025-04-03T18:13:31Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
