NodeRed "Tell me joke" : read a local JSON file

Hey

I use HAOS in a local mode : disconnected from internet.
I would like to read some jokes picked up from a local file on my Rpi.

What i have done :

So i downloaded a JSON file with 10 jokes and stored it on my Raspberry.

See Json content
{
    "error": false,
    "amount": 10,
    "jokes": [
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Comment reconnaître un ministre qui ment?",
            "delivery": "Ses lèvres bougent",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 237,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Pourquoi est-ce que les anges sont sourds?",
            "delivery": "Parce que Jésus-Christ.",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 203,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Que disait Eve pour faire avancer le bâteau?",
            "delivery": "Ramadan",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 315,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Tu sais ce que c'est un suppositoire?",
            "delivery": "C'est une invention qui restera dans les annales.",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 319,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Quand 2 poissons s'énervent..",
            "delivery": "Est-ce qu'on peut dire que le thon monte?",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 298,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Pourquoi les bots ne parlent pas avec les humains?",
            "delivery": "Par peur de leur ressembler.",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 272,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Qu'est-ce qu'un cadeau qui s'en va?",
            "delivery": "Une surprise party.",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 153,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Que font 2 brosses à dents le 14 Juillet?",
            "delivery": "Un feu dentifrice!",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 262,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Chéri, je me sens grosse et laide... S'il te plait, fais-moi un compliment...",
            "delivery": "Tu as une bonne vue!",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 248,
            "lang": "fr"
        },
        {
            "category": "Misc",
            "type": "twopart",
            "setup": "Pourquoi il n'y a pas de ballon à question pour un champion?",
            "delivery": "Parce que Julien Lepers",
            "flags": {
                "nsfw": false,
                "religious": false,
                "political": false,
                "racist": false,
                "sexist": false,
                "explicit": false
            },
            "safe": true,
            "id": 15,
            "lang": "fr"
        }
    ]
}

I manage to read this file and find back those jokes : 2 lines (question =setup & answer =delivery)

NodeRed nodes
[
    {
        "id": "15c33a53cd75773b",
        "type": "file in",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "filename": "/config/Jokes/blagues.json",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "utf8",
        "allProps": false,
        "x": 320,
        "y": 640,
        "wires": [
            [
                "5cc6e99793bc4d7d"
            ]
        ]
    },
    {
        "id": "d39b1e46968ef8db",
        "type": "debug",
        "z": "20d5c36c60b1a5e6",
        "name": "DEbug content Json file",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1150,
        "y": 620,
        "wires": []
    },
    {
        "id": "81890602d5baede6",
        "type": "inject",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 80,
        "y": 600,
        "wires": [
            [
                "15c33a53cd75773b"
            ]
        ]
    },
    {
        "id": "5cc6e99793bc4d7d",
        "type": "json",
        "z": "20d5c36c60b1a5e6",
        "name": "Json String to Json Objet",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 610,
        "y": 640,
        "wires": [
            [
                "d39b1e46968ef8db",
                "5d3a22fa8158d720"
            ]
        ]
    },
    {
        "id": "5d3a22fa8158d720",
        "type": "function",
        "z": "20d5c36c60b1a5e6",
        "name": "GetJoke",
        "func": "\nvar tts = payload.jokes[0].setup\n\nmsg.payload= tts\nreturn msg;",
        "outputs": 1,
        "noerr": 1,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 740,
        "wires": [
            [
                "35606b2bf84f5634"
            ]
        ]
    },
    {
        "id": "35606b2bf84f5634",
        "type": "http request",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://homeassistant.local:12101/api/text-to-speech",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "credentials": {
            "user": "",
            "password": ""
        },
        "x": 1030,
        "y": 740,
        "wires": [
            []
        ]
    }
]

My problem :
For the moment, i cannot grab those data to send it to Rhasspy

I want to share it with you if anyone is interested and could help me :slight_smile:

thanks a lot

Fab

Change:

http://homeassistant.local:12101/api/text-to-speech

to the actual IP address

http://IP-OF-HOMEASSISTANT:12101/api/text-to-speech

as for your actual function - instead of what you have which always returns the first entry - you should have:

var i = Math.floor(Math.random() * payload.jokes.length -1);
var joke = payload.jokes[i];
var tts = joke.setup;
1 Like

Thanks a lot @mobile.andrew.jones for your help

the random calculation is cool
i used the random node instead

and i introduce a delay because there the question and then the answer 4s later :slight_smile:

it is perfectly read by rhasspy :slight_smile:

Here is the flow :slight_smile:

Summary
[
    {
        "id": "15c33a53cd75773b",
        "type": "file in",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "filename": "/config/Jokes/blagues.json",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "utf8",
        "allProps": false,
        "x": 400,
        "y": 600,
        "wires": [
            [
                "5cc6e99793bc4d7d"
            ]
        ]
    },
    {
        "id": "81890602d5baede6",
        "type": "inject",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 600,
        "wires": [
            [
                "15c33a53cd75773b"
            ]
        ]
    },
    {
        "id": "5cc6e99793bc4d7d",
        "type": "json",
        "z": "20d5c36c60b1a5e6",
        "name": "Json String to Json Objet",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 690,
        "y": 600,
        "wires": [
            [
                "18888040a580fb7d"
            ]
        ]
    },
    {
        "id": "5d3a22fa8158d720",
        "type": "function",
        "z": "20d5c36c60b1a5e6",
        "name": "GetJokeQuestion",
        "func": "var randomJoke = msg.random\nvar tts1 = msg.payload.jokes[msg.random].setup\nvar tts = tts1\nmsg.payload= tts\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1010,
        "y": 700,
        "wires": [
            [
                "35606b2bf84f5634"
            ]
        ]
    },
    {
        "id": "35606b2bf84f5634",
        "type": "http request",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://homeassistant.local:12101/api/text-to-speech",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1230,
        "y": 700,
        "wires": [
            []
        ]
    },
    {
        "id": "18888040a580fb7d",
        "type": "random",
        "z": "20d5c36c60b1a5e6",
        "name": "Random",
        "low": 1,
        "high": "09",
        "inte": "true",
        "property": "random",
        "x": 620,
        "y": 700,
        "wires": [
            [
                "5d3a22fa8158d720",
                "86638e9d35563164"
            ]
        ]
    },
    {
        "id": "e9a030d9901b8801",
        "type": "function",
        "z": "20d5c36c60b1a5e6",
        "name": "GetJokeAnswer",
        "func": "var randomJoke = msg.random\nvar tts2 = msg.payload.jokes[msg.random].delivery\nvar tts = tts2\nmsg.payload= tts\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1000,
        "y": 780,
        "wires": [
            [
                "e71ba3d0583bac94"
            ]
        ]
    },
    {
        "id": "e71ba3d0583bac94",
        "type": "http request",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://homeassistant.local:12101/api/text-to-speech",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1230,
        "y": 780,
        "wires": [
            []
        ]
    },
    {
        "id": "86638e9d35563164",
        "type": "delay",
        "z": "20d5c36c60b1a5e6",
        "name": "",
        "pauseType": "delay",
        "timeout": "4",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 840,
        "y": 780,
        "wires": [
            [
                "e9a030d9901b8801"
            ]
        ]
    }
]

Next step : how can i put it on HA frontend (lovelace) :stuck_out_tongue_winking_eye:

The benefit of using Math.floor(Math.random() * msg.payload.jokes.length - 1) is that you don’t need to keep editing the random node’s upper limit - if the number of jokes in the JSON file changes, you wouldn’t need to edit the flow at all to account for that.

As for how to put it on Lovelace there are a few ways - the simplest way is to have a button that runs a script, the script doesn’t need to do anything at all really. In NodeRed you listen for that script being triggered to kick off the random jokes flow.

1 Like

thanks again @mobile.andrew.jones , that helps me a lot !

i am going to see what i can do with that and will share it here :wink: