Ring MQTT -> Base64 -> Telegram

Hey Guys,

Question:

for the past 3 day's i'm trying to figure out how to get a mqtt base64 img to a telegram channel so i get a picture if someone rights the bell

i think i'm pretty far although i also think it can be more efficiënt.

this is my flow as of now:

The MQTT message gets encoded by the base64 then gets written on disk by "write"

it gets pickup when someone rings the doorbell by the "load image" and send picture contains:

msg.payload.chatId=-100170xx49x220
msg.payload.type= 'photo';
msg.payload.caption= "Caption"
msg.payload.content= msg.payload
return msg;

i copied that from another user here on the forum and edited my chatid

this is the flow:

[
    {
        "id": "52910ba9d857f29e",
        "type": "base64",
        "z": "09d4dd0351a92c23",
        "name": "",
        "action": "",
        "property": "payload",
        "x": 340,
        "y": 780,
        "wires": [
            [
                "9291282e83a46ed7",
                "c1340dd4b1952e22"
            ]
        ]
    },
    {
        "id": "c1340dd4b1952e22",
        "type": "image viewer",
        "z": "09d4dd0351a92c23",
        "name": "",
        "width": 160,
        "data": "payload",
        "dataType": "msg",
        "active": true,
        "x": 750,
        "y": 620,
        "wires": [
            []
        ]
    },
    {
        "id": "9291282e83a46ed7",
        "type": "jimp-image",
        "z": "09d4dd0351a92c23",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "ret": "img",
        "parameter1": "/data/node-red-256.png",
        "parameter1Type": "str",
        "parameter2": "",
        "parameter2Type": "msg",
        "parameter3": "",
        "parameter3Type": "msg",
        "parameter4": "",
        "parameter4Type": "msg",
        "parameter5": "",
        "parameter5Type": "msg",
        "parameter6": "",
        "parameter6Type": "msg",
        "parameter7": "",
        "parameter7Type": "msg",
        "parameter8": "",
        "parameter8Type": "msg",
        "sendProperty": "payload",
        "sendPropertyType": "msg",
        "parameterCount": 1,
        "jimpFunction": "write",
        "selectedJimpFunction": {
            "name": "write",
            "fn": "write",
            "description": "Write to file. NOTE: You can specify an alternative file extension type to change the type. Currently support types are jpg, png, bmp.",
            "parameters": [
                {
                    "name": "filename",
                    "type": "str",
                    "required": true,
                    "hint": "Name of the file",
                    "defaultType": "str"
                }
            ]
        },
        "x": 610,
        "y": 820,
        "wires": [
            []
        ]
    },
    {
        "id": "12902d544a65b520",
        "type": "switch",
        "z": "09d4dd0351a92c23",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 250,
        "y": 960,
        "wires": [
            [
                "110674a7fb87820a",
                "0303f5f02dc57c56",
                "82ef1ec6644c5519",
                "a0158dde9de70598"
            ],
            []
        ]
    },
    {
        "id": "a41a7223779bc470",
        "type": "telegram sender",
        "z": "09d4dd0351a92c23",
        "name": "",
        "bot": "9199505571c7bbde",
        "haserroroutput": false,
        "outputs": 1,
        "x": 840,
        "y": 860,
        "wires": [
            [
                "692390b113e4e004"
            ]
        ]
    },
    {
        "id": "a0158dde9de70598",
        "type": "file in",
        "z": "09d4dd0351a92c23",
        "name": "load image",
        "filename": "/data/node-red-256.png",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 470,
        "y": 960,
        "wires": [
            [
                "bf924d9bde8ac67c"
            ]
        ]
    },
    {
        "id": "3fd182b208be2475",
        "type": "sonos-universal",
        "z": "09d4dd0351a92c23",
        "confignode": "ad4f73545a13e1ce",
        "command": "group.play.notification",
        "state": "http://patatje.net/dingdong.mp3",
        "stateType": "str",
        "avoidCheckPlayerAvailability": false,
        "name": "Overloop Ding Dong",
        "x": 380,
        "y": 860,
        "wires": [
            []
        ]
    },
    {
        "id": "0303f5f02dc57c56",
        "type": "sonos-universal",
        "z": "09d4dd0351a92c23",
        "confignode": "3bbcc44335742592",
        "command": "group.play.notification",
        "state": "http://patatje.net/dingdong.mp3",
        "stateType": "str",
        "avoidCheckPlayerAvailability": false,
        "name": "Keuken Ding Dong",
        "x": 630,
        "y": 880,
        "wires": [
            []
        ]
    },
    {
        "id": "82ef1ec6644c5519",
        "type": "api-current-state",
        "z": "09d4dd0351a92c23",
        "name": "AppleTV Woonkamer is playing",
        "server": "51041573183f2685",
        "version": 3,
        "outputs": 2,
        "halt_if": "Playing",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "media_player.woonkamer_tv",
        "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": 230,
        "y": 1080,
        "wires": [
            [
                "886c43c6cbf703ee"
            ],
            []
        ]
    },
    {
        "id": "886c43c6cbf703ee",
        "type": "api-call-service",
        "z": "09d4dd0351a92c23",
        "name": "Pause AppleTV",
        "server": "51041573183f2685",
        "version": 5,
        "debugenabled": false,
        "domain": "media_player",
        "service": "media_play_pause",
        "areaId": [
            "woonkamer"
        ],
        "deviceId": [
            "d25c0aee46e67579ff3bb3cbbc5c3f6f"
        ],
        "entityId": [
            "media_player.woonkamer_tv"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 320,
        "y": 1120,
        "wires": [
            []
        ]
    },
    {
        "id": "692390b113e4e004",
        "type": "debug",
        "z": "09d4dd0351a92c23",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 780,
        "wires": []
    },
    {
        "id": "bf924d9bde8ac67c",
        "type": "function",
        "z": "09d4dd0351a92c23",
        "name": "send picture",
        "func": "msg.payload.chatId=-00000\nmsg.payload.type= 'photo';\nmsg.payload.caption= \"Caption\"\nmsg.payload.content= msg.payload\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 670,
        "y": 960,
        "wires": [
            [
                "a41a7223779bc470"
            ]
        ]
    },
    {
        "id": "3899e466c719ea88",
        "type": "comment",
        "z": "09d4dd0351a92c23",
        "name": "Ring creates a new snapshot every 3 seconds",
        "info": "",
        "x": 230,
        "y": 700,
        "wires": []
    },
    {
        "id": "ae69b7afd5e1b59f",
        "type": "comment",
        "z": "09d4dd0351a92c23",
        "name": "Write image to file",
        "info": "Write image to file",
        "x": 590,
        "y": 760,
        "wires": []
    },
    {
        "id": "ea0e5854176486bf",
        "type": "comment",
        "z": "09d4dd0351a92c23",
        "name": "Pickup writed file",
        "info": "",
        "x": 480,
        "y": 1020,
        "wires": []
    },
    {
        "id": "9199505571c7bbde",
        "type": "telegram bot",
        "botname": "@Pic0Bot",
        "usernames": "xxx",
        "chatids": "-xx",
        "baseapiurl": "",
        "updatemode": "polling",
        "pollinterval": "300",
        "usesocks": false,
        "sockshost": "",
        "socksport": "6667",
        "socksusername": "anonymous",
        "sockspassword": "",
        "bothost": "",
        "botpath": "",
        "localbotport": "8443",
        "publicbotport": "8443",
        "privatekey": "",
        "certificate": "",
        "useselfsignedcertificate": false,
        "sslterminated": false,
        "verboselogging": false
    },
    {
        "id": "ad4f73545a13e1ce",
        "type": "sonos-config",
        "name": "Overloop Sonos",
        "serialnum": "",
        "ipaddress": "10.0.1.10"
    },
    {
        "id": "3bbcc44335742592",
        "type": "sonos-config",
        "name": "Keuken",
        "serialnum": "",
        "ipaddress": "10.0.1.8"
    },
    {
        "id": "51041573183f2685",
        "type": "server",
        "name": "Home Assistant",
        "version": 2,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30"
    }
]

i wonder what i'm doing wrong but i also wonder why i cant directly send the base64 to telegram without having to save it first.

i hope anyone can help!

You could save the image to flow context (in memory) then recall it from flow context upon DingDong

Wait.. how? i forgot to mention this is my first flow ever created lol.. i'm learning allot by reading your answers on different forum post but i keep bumping against unknowns like when i edit the "write" node and edit it to say "save to buffer" it doesnt work and i dont know why because in my mind thats correct right?

It you attach a debug to the image node when set as "to buffer" and "none" for the operation, then you will see the output is a buffer.

therefore, if you use a change node AFTER the image node you can store that buffer in flow.door_image then you can use another change node to "set msg.payload to the value of flow.door_image" instead of the file read node.

Like this...

Nodes I changed here...

[{"id":"b9abc24ee4d2c689","type":"mqtt in","z":"d8bcc25e815f0bc3","name":"mqtt set to return buffer","topic":"door_image","qos":"2","datatype":"buffer","broker":"1dd4851e.412ce3","nl":false,"rap":true,"rh":0,"inputs":0,"x":1160,"y":1400,"wires":[["9291282e83a46ed7"]]},{"id":"9291282e83a46ed7","type":"jimp-image","z":"d8bcc25e815f0bc3","name":"","data":"payload","dataType":"msg","ret":"buf","parameter1":"/data/node-red-256.png","parameter1Type":"str","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","sendProperty":"payload","sendPropertyType":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":1350,"y":1400,"wires":[["fa48ec9f64be014e"]]},{"id":"fa48ec9f64be014e","type":"change","z":"d8bcc25e815f0bc3","name":"store image data in flow.door_image","rules":[{"t":"set","p":"door_image","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1590,"y":1400,"wires":[["c1340dd4b1952e22"]]},{"id":"c1340dd4b1952e22","type":"image viewer","z":"d8bcc25e815f0bc3","name":"","width":160,"data":"payload","dataType":"msg","active":true,"x":1850,"y":1400,"wires":[[]]},{"id":"293e3c3885d06ebb","type":"change","z":"d8bcc25e815f0bc3","name":"get flow.door_image","rules":[{"t":"set","p":"payload","pt":"msg","to":"door_image","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1370,"y":1580,"wires":[["bf924d9bde8ac67c"]]},{"id":"bf924d9bde8ac67c","type":"function","z":"d8bcc25e815f0bc3","name":"prepare telegram","func":"\nmsg.payload = {\n    chatId: -0,\n    type: 'photo',\n    caption: \"Caption\",\n    content: msg.payload,\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1590,"y":1580,"wires":[["a41a7223779bc470"]]},{"id":"1dd4851e.412ce3","type":"mqtt-broker","name":"test.mosquitto.org","broker":"test.mosquitto.org","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

important note...

When using images, try not to branch off as this will create clones of the image - images are HEAVY work for nodejs. See how i have single wires only where the image travels :point_up_2:

Awesome! it works!

So we converted the base64 to image using the image node and then converted the image to a buffer named flow.door_image and then grabbed the buffer converted it to msg.payload and send it to telegram

that means we can create buffers (temp. variables) using the change node? that would be a new insight in creating other flows.

thank you for this lesson :slight_smile:

In simplest form - yes, that is correct

Just dont get carried away. :slight_smile: - some gotchas...

  • context is NOT a database (is held in memory)
  • if 2 messages arrive in quick succession, you can get odd effects (context being overwriten by 2nd msg while first message is using it)

The best solutions are almost always event based and store necessary data for processing in the msg (and not necessarily just in msg.payload - you can store data in msg.here, msg.there and msg.any_where_your_heart_desires) for use in nodes further downstream.

1 Like

haha noted! thank you for the insight

just my 2c and probably haven't understood totally... - in the top flow - you are taking the image and converting it to base64 - so now it looks like you are going mqtt (buffer) -> base64 -> image -> buffer... are you sure that the original isn't just a buffer you can use to start with ?

1 Like

Quite possibly, but I didnt raise it since I was unsure of incoming format (it was working for the OP) so just stay shtum :shushing_face:

@joeblack2k as Dave rightly points out, you can likely just do mqtt (output buffer) --> change node (store in context) and skip the other stuff (though it does depend on the msg format and image format - it may or may not work - as the image node does do some magic on odd images with missing type info and missing mime types)

YMMV

1 Like

Nice, i could indeed remove the image node and just go directly

even cleaner setup! thanks!

1 Like

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