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

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....

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')"

[
    {
        "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
    }
]

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) 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

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

1 Like

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 ?

{"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}}

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.

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).

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 !

Great !

You can concatenate with a
function node 3 Ways to Concatenate Strings in JavaScript - Mastering JS
Template literals (Template strings) - JavaScript | MDN

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

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

okkkkk

writing this :
image

gives :

which is a very good result for me

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.

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

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