Mini-tutorial: Add suncalc API values to the dashboard via Node-RED and MQTT

via this mini-tutorial you will be able to add the values from the suncalc API into the dashboard via an HTTPS call to the API https://api.sunrise-sunset.org/ which returns a JSON in this format

for this you must import the flow from the following message into Node-RED (it exceeds 32000 characters)

You should normally have this flow

each node 'function" sends the desired value in the payload at 00:00 but also after x minutes after midnight which allows generating an MQTT message at the time of the desired event (for example the value « Sunrise » is sent here at 00:01 and at 5:42) to use it in a scene but in that case you must condition it to after 00:01

you’ll just need to adapt the parameters of the « Mqtt » node by clicking on the node then on the pen of the 'Server section

and adjust your GPS coordinates in the ‹ api.sunrise-sunset.org › node

Then in Gladys, in the « MQTT » integration create a device as follows

and for each desired feature (sunrise, sunset etc) add a feature following the « sunrise » template

then in the dashboard add the desired values

There you go, you now have the Suncalc elements in the dashboard!

Now by creating a scene here « Sunrise » and filtering the message sent at 00:00 you can perform an action at sunrise (which already exists in Gladys) but additionally you can adjust the time in the « function » node of the « Sunrise »

mode normal

// calculate the delay after midnight in minutes to send the message
var delai = hours *60 + minutes + 1;
msg.delay = delai;

mode 20 mn before

// calculate the delay after midnight in minutes to send the message
var delai = hours *60 + minutes + 1 - 20;
msg.delay = delai;

mode 20 mn after

// calculate the delay after midnight in minutes to send the message
var delai = hours *60 + minutes + 1 + 20;
msg.delay = delai;

The simplicity of Gladys in terms of use and Dashboard coupled with the Swiss-army-knife Node-RED = a product adaptable to the specific needs of each user! :wink:

I modified the code because the ‹ delay › nodes were in milliseconds instead of minutes…oops, my bad :upside_down_face: :face_with_hand_over_mouth:! And I added a ‹ send time › function to track the time at which the « sunrise » message is sent for the second time (the first at 00:01), just duplicate to track the others.

[details=« Flow code »]

[
    {
        "id": "7ff8481b0e530218",
        "type": "tab",
        "label": "Suncalc API",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "467565bd915a2a8d",
        "type": "group",
        "z": "7ff8481b0e530218",
        "name": "Suncalc API",
        "style": {
            "stroke": "#000000",
            "fill": "#e3f3d3",
            "label": true
        },
        "nodes": [
            "eae0976220b585b3",
            "5dda4437af0bff5e",
            "9ed546b6886bbafa",
            "f6ea59633d3bb98b",
            "e5be0b9d45a8b6c2",
            "38e643d99efa7a95",
            "846f914fd081c13f",
            "152afaf0fdc6be38",
            "59307f7f63590e5d",
            "838df80a9f965a6d",
            "a4397d1a98150a4d",
            "5d1de6bc5497c087",
            "c2b15889e9096be1",
            "b8a6dca76c04f480",
            "3a99ad9a2f752241",
            "3ea4979d2eaf146b",
            "901f952626786b11",
            "1d040814b1befb3a",
            "8f7e50836226d1ee",
            "8c49858b8bbfe11c",
            "9e462a3d9f69bee0",
            "048c1578dc7d13d5",
            "bee1fb0b9897f334",
            "3dd556977ebeb003",
            "17d36d7e51f3c5aa",
            "89002c75327e8e5f",
            "2e8b434a55627e7a",
            "21097eb9222e6af0",
            "e389303bb28be230"
        ],
        "x": 54,
        "y": 79,
        "w": 1312,
        "h": 722
    },
    {
        "id": "eae0976220b585b3",
        "type": "inject",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 00 * * *",
        "once": true,
        "onceDelay": "1",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 400,
        "wires": [
            [
                "5dda4437af0bff5e"
            ]
        ]
    },
    {
        "id": "5dda4437af0bff5e",
        "type": "http request",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "api.sunrise-sunset.org",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.sunrise-sunset.org/json?lat={42.555}&lng={2.833}&date=today",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 220,
        "y": 460,
        "wires": [
            [
                "9ed546b6886bbafa",
                "38e643d99efa7a95",
                "846f914fd081c13f",
                "152afaf0fdc6be38",
                "59307f7f63590e5d",
                "838df80a9f965a6d",
                "a4397d1a98150a4d",
                "5d1de6bc5497c087",
                "c2b15889e9096be1",
                "b8a6dca76c04f480",
                "3a99ad9a2f752241"
            ]
        ]
    },
    {
        "id": "9ed546b6886bbafa",
        "type": "debug",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 470,
        "y": 460,
        "wires": []
    },
    {
        "id": "f6ea59633d3bb98b",
        "type": "debug",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "msg",
        "x": 700,
        "y": 120,
        "wires": []
    },
    {
        "id": "e5be0b9d45a8b6c2",
        "type": "mqtt out",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "Publish to MQTT broker IP Gladys:1883",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "f4ab211ed11dc81c",
        "x": 1190,
        "y": 460,
        "wires": []
    },
    {
        "id": "38e643d99efa7a95",
        "type": "function",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "sunrise",
        "func": "var topic = msg.topic;\nvar msg = msg.payload;\n\nmsg.topic = \"gladys/master/device/mqtt:suncalc/feature/mqtt:sunrise/text\";\n\n// time string in AM/PM format\nvar timeString = msg.results.sunrise;\n\n// Convert the string to a Date object\nvar date = new Date(\"2000-01-01 \" + timeString);\n\n// Format the time in 24-hour format\nvar hours = date.getHours();\nvar minutes = date.getMinutes();\n\n// add a zero in front of the digits if necessary\nhours = hours < 10 ? \"0\" + hours : hours;\nminutes = minutes < 10 ? \"0\" + minutes : minutes;\n\n// Display the time in 24-hour format\nvar formattedTime = hours + \":\" + minutes;\n\n// send in 24h format\nmsg.payload = formattedTime;\n\n// calculate the delay after midnight in minutes to send the message\nvar delai = (hours * 60) + minutes + 1;\nmsg.delay = delai;\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 160,
        "wires": [
            [
                "e5be0b9d45a8b6c2",
                "f6ea59633d3bb98b",
                "3ea4979d2eaf146b",
                "2e8b434a55627e7a"
            ]
        ]
    },
    {
        "id": "846f914fd081c13f",
        "type": "function",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "sunset",
        "func": "var topic = msg.topic;\nvar msg = msg.payload;\n\nmsg.topic = \"gladys/master/device/mqtt:suncalc/feature/mqtt:sunset/text\";\n\n\n// time string in AM/PM format\nvar timeString =msg.results.sunset;\n\n// Convert the string to a Date object\nvar date = new Date(\"2000-01-01 \" + timeString);\n\n// Format the time in 24-hour format\nvar hours = date.getHours();\nvar minutes = date.getMinutes();\n\n// add a zero in front of the digits if necessary\nhours = hours < 10 ? \"0\" + hours : hours;\nminutes = minutes < 10 ? \"0\" + minutes : minutes;\n\n// Display the time in 24-hour format\nvar formattedTime = hours + \":\" + minutes;\n\n// send in 24h format\nmsg.payload = formattedTime;\n\n// calculate the delay after midnight in minutes to send the message\nvar delai = hours *60 + minutes + 1;\nmsg.delay = delai;\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 450,
        "y": 220,
        "wires": [
            [
                "e5be0b9d45a8b6c2",
                "901f952626786b11"
            ]
        ]
    },
    {
        "id": "152afaf0fdc6be38",
        "type": "function",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "solar_noon",
        "func": "var topic = msg.topic;\nvar msg = msg.payload;\n\nmsg.topic = \"gladys/master/device/mqtt:suncalc/feature/mqtt:solar_noon/text\";\n\n\n// time string in AM/PM format\nvar timeString =msg.results.solar_noon;\n\n// Convert the string to a Date object\nvar date = new Date(\"2000-01-01 \" + timeString);\n\n// Format the time in 24-hour format\nvar hours = date.getHours();\nvar minutes = date.getMinutes();\n\n// add a zero in front of the digits if necessary\nhours = hours < 10 ? \"0\" + hours : hours;\nminutes = minutes < 10 ",? "0" + minutes : minutes;

// Display time in 24-hour format
var formattedTime = hours + ":" + minutes;

// Send in 24h format
msg.payload = formattedTime;

// Calculate the delay after midnight in minutes to send the message
var delai = hours *60 + minutes + 1;
msg.delay = delai;

return msg;"},? \"0\" + hours : hours;\nminutes = minutes < 10 ? \"0\" + minutes : minutes;\n\n// Display time in 24-hour format\nvar formattedTime = hours + \":\" + minutes;\n\n// send in 24h format\nmsg.payload = formattedTime;\n\n// calculate the delay after midnight in minutes to send the message\nvar delai = hours *60 + minutes + 1;\nmsg.delay = delai;\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 700,
        "wires": [
            [
                "e5be0b9d45a8b6c2",
                "048c1578dc7d13d5"
            ]
        ]
    },
    {
        "id": "3a99ad9a2f752241",
        "type": "function",
        "z": "7ff8481b0e530218",
        "g": "467565bd915a2a8d",
        "name": "astronomical_twilight_end",
        "func": "var topic = msg.topic;\nvar msg = msg.payload;\n\nmsg.topic = \"gladys/master/device/mqtt:suncalc/feature/mqtt:astronomical_twilight_end/text\";\n\n// time string in AM/PM format\nvar timeString =msg.results.astronomical_twilight_end;\n\n// Convert the string to a Date object\nvar date = new Date(\"2000-01-01 \" + timeString);\n\n// Format the time in 24-hour format\nvar hours = date.getHours();\nvar minutes = date.getMinutes();\n\n// add a leading zero if necessary\nhours = hours < 10 ? \"0\" + hours : hours;\nminutes = minutes < 10