# Generate values and publish to MQTT topic

**URL:** <https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519>\
**Category:** General\
**Created:** [27 November 2024 13:55 UTC](https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519 "2024-11-27T13:55:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![atux\_null](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@atux\_null](https://discourse.nodered.org/u/atux_null)\
**Post date:** [27 November 2024 13:55 UTC](https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519/1 "2024-11-27T13:55:12Z")

</div>

setup consists of a MQTT broker running in a PC.  
Node red runs in a Raspberry PI.  
i would like to generate fake values every 5 secs repeatedly of WATTS in the range 1-5 with 2 decimal points. Then send those values to MQTT broker to a certain topic in the MQTT of the PC. eg topic /bedroom/lights/master  
i have created a sample to check how it will look in debug, with a chart, a gauge and a debug every 5 secs. It seems that it outputs the values, but with 1 decimal point, not 2.  
Also how do i send this information to my MQTT broker (192.168.178.251/24), to the topic /bedroom/lights/master ?  
Here is my

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/798fa361eee9b4092401924f9d7764d7d18c9973.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/7686d2f93cd1990376a46daf77a209e4f5b7b55f.png)  
and the json.

```auto
[
    {
        "id": "02971057881eb4a0",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "7b3ceefc5f6f4dc1",
        "type": "inject",
        "z": "02971057881eb4a0",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 100,
        "wires": [
            [
                "42f9d6d97e491ddd"
            ]
        ]
    },
    {
        "id": "42f9d6d97e491ddd",
        "type": "function",
        "z": "02971057881eb4a0",
        "name": "Random_generator",
        "func": "msg.payload = Math.round(Math.random()*10);\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 140,
        "wires": [
            [
                "eafdc1be9aaec763",
                "1a5206170bab8791",
                "eeda97170d857a95",
                "94d95500410527dd",
                "3ae60c20ad27b74a"
            ]
        ]
    },
    {
        "id": "eafdc1be9aaec763",
        "type": "debug",
        "z": "02971057881eb4a0",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 580,
        "y": 80,
        "wires": []
    },
    {
        "id": "d226331329827763",
        "type": "comment",
        "z": "02971057881eb4a0",
        "name": "Random_generate_every_5s_a_number_0_5",
        "info": "",
        "x": 350,
        "y": 20,
        "wires": []
    },
    {
        "id": "1a5206170bab8791",
        "type": "ui_text",
        "z": "02971057881eb4a0",
        "group": "ef108d3cefe2d6ee",
        "order": 0,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "text",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "style": false,
        "font": "",
        "fontSize": 16,
        "color": "#000000",
        "x": 590,
        "y": 180,
        "wires": []
    },
    {
        "id": "eeda97170d857a95",
        "type": "ui_gauge",
        "z": "02971057881eb4a0",
        "name": "",
        "group": "ef108d3cefe2d6ee",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "gauge",
        "label": "units",
        "format": "{{value}}",
        "min": 0,
        "max": "5",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 590,
        "y": 240,
        "wires": []
    },
    {
        "id": "94d95500410527dd",
        "type": "ui_slider",
        "z": "02971057881eb4a0",
        "name": "",
        "label": "slider",
        "tooltip": "",
        "group": "ef108d3cefe2d6ee",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "all",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "5",
        "step": "0.1",
        "className": "",
        "x": 590,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "3ae60c20ad27b74a",
        "type": "ui_chart",
        "z": "02971057881eb4a0",
        "name": "",
        "group": "ef108d3cefe2d6ee",
        "order": 3,
        "width": 0,
        "height": 0,
        "label": "chart",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm:ss",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 590,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "ef108d3cefe2d6ee",
        "type": "ui_group",
        "name": "Lab",
        "tab": "8af0828ba460aacc",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "8af0828ba460aacc",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 November 2024 14:13 UTC](https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519/2 "2024-11-27T14:13:15Z")

</div>

> [@atux\_null](#):
>
> `Math.round(Math.random()*10)`

That results in no DP's. What you need is `Math.round(Math.random()*10000)/100`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 November 2024 14:30 UTC](https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519/3 "2024-11-27T14:30:21Z")

</div>

> [@atux\_null](#):
>
> Also how do i send this information to my MQTT broker (192.168.178.251/24), to the topic /bedroom/lights/master ?

You need an MQTT-out node. It's settings should specify the IP address of the PC (which should therefore have a static IP) and any login credentials.

And you can specify the topic in the MQTT-out.

It's a little unusual I think to have an MQTT broker on a PC if you have a Pi running 24/7, because that makes a perfect host for an MQTT broker.

---

<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:** [25 February 2025 14:31 UTC](https://discourse.nodered.org/t/generate-values-and-publish-to-mqtt-topic/93519/4 "2025-02-25T14:31:13Z")

</div>

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