Beginner Question - DHT11 triggering Relay

Hi Everyone,

First off want to apologize. I am new to Node Red and in a way a beginner at programming. Although I am enjoying using the Node Red interface, I'm trying to wrap my head around triggering, specifically a relay based on the DHT11 sensor reaching a certain temperature.

I created my flow and can read temperature and humidity, even created a gauge. To trigger a relay, I have read through a lot of the forum, followed examples online and watched several videos, but in all cases, some other elements are involved, like MQTT, or HomeAssistant, or Alexa, etc. In these cases I am having a hard time pulling out the pieces that I would need.

I don't want to be given the answer, but does anyone have good instructions about implementing triggers?

Thanks in advance,

Robert

1 Like

Welcome to the forum @bugsy65.

Is it a relay connected directly to the pins of a pi, for example, or is it a relay in an external device of some sort?

Thanks for responding so quickly. It’s a 4 channel relay connected directly to a Pi zero with a dht11 on pin 4 of the zero.

Assuming that the relays are connected to the GPIO pins on the pi then to control a relay you can use a GPIO node to drive the output low (usually this energises the relay) or high (to de-energise). You just have to send the gpio node 0 or 1 in msg.payload.

To drive it based on a temperature threshold I suggest you look at node-red-contrib-ramp-thermostat which will give you a thermostat effect. It also has a ramp feature but you can just ignore that.

Hi Colin,

I've installed the ramp-thermostat, but haven't found any useful tutorials to implement it. I've just updated this message by adding the 2 flows that I'm working on.

Thanks,

Robert

node-red-contrib-ramp-thermostat (node) - Node-RED shows how to use it. Ignore the stuff about profiles if you don't need that feature. If you don't understand the basic concepts then I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Thanks Colin,

I've watched the videos you suggested in the past and hundreds of others on youtube. There is nothing that points me to implementing what I am trying to do right out of the Node Red box. Using Ramp-Thermostat sounds like something to use, but with no examples of how it is implemented is useless to me. As well as the hundreds of videos that show examples that are dependent on using home assistant, or homekit or other tools. you would think there is a simple example of using a sensor to trigger a relay. Sorry, just really frustrated right now.

Robert

Hello,
Here is a most basic example of a flow that based on the input sends 0 if the input is below 20 and 1 when above. It uses a change node with a simple jsonata conditional for this and an rbe node after that to only send something if it changed from 1 to 0 or otherwise:

[{"id":"d7891e82.9dbe9","type":"inject","z":"6fb808b7.b81b6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"str","x":210,"y":100,"wires":[["930aa71c.bc49b8"]]},{"id":"8e2a9b6f.e3c77","type":"inject","z":"6fb808b7.b81b6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"str","x":210,"y":180,"wires":[["930aa71c.bc49b8"]]},{"id":"75500510.a0269c","type":"debug","z":"6fb808b7.b81b6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":140,"wires":[]},{"id":"930aa71c.bc49b8","type":"change","z":"6fb808b7.b81b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($number(msg.payload) < 20) ? 0 : 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":140,"wires":[["add1e361.5f294"]]},{"id":"add1e361.5f294","type":"rbe","z":"6fb808b7.b81b6","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":570,"y":140,"wires":[["75500510.a0269c"]]}]

The videos don’t handle such concrete examples as they are more about teaching the methods that are used to solve problems with nodered.
@Colin recommended the ramp thermostat node as you can run into problems when switching based on temperature as when the temperature is around the switch point it can bounce below and above it in short succession and lead to unwanted oscillation in the switching. The ramp thermostat prevents this by implementing a hysteresis feature.
Hope this helps,
Johannes

1 Like

Hi Colin and Johannes,

Please see the example flow I created. I am able to control the relay based on temperature range. Basically when it hits below 21 then the relay and when it is equal to or greater than 22 it turns off.

One thing that I don't know is where to place the ramp thermostat and what parameters to set. There is little in the way of explanation or examples that are useful.

If you have any thoughts I would appreciate it.

Robert

[
    {
        "id": "d87acc50.862d1",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "fee1635e.5b4f5",
        "type": "mqtt in",
        "z": "d87acc50.862d1",
        "name": "Temperature",
        "topic": "home/livingroom/temperature",
        "qos": "2",
        "datatype": "auto",
        "broker": "8225a193.5398a",
        "x": 90,
        "y": 240,
        "wires": [
            [
                "73a2686d.a78b38",
                "cac59bd.c146e68",
                "4e713a7.4bf4fc4",
                "8047225e.861da"
            ]
        ]
    },
    {
        "id": "317513be.fa125c",
        "type": "mqtt in",
        "z": "d87acc50.862d1",
        "name": "Humidity",
        "topic": "home/livingroom/humidity",
        "qos": "2",
        "datatype": "auto",
        "broker": "8225a193.5398a",
        "x": 80,
        "y": 300,
        "wires": [
            [
                "6c72ce5.015d23",
                "73a2686d.a78b38"
            ]
        ]
    },
    {
        "id": "6c72ce5.015d23",
        "type": "ui_gauge",
        "z": "d87acc50.862d1",
        "name": "Humidity",
        "group": "bc7f15c3.5ac668",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Humidity",
        "label": "%",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 400,
        "y": 340,
        "wires": []
    },
    {
        "id": "73a2686d.a78b38",
        "type": "debug",
        "z": "d87acc50.862d1",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 410,
        "y": 120,
        "wires": []
    },
    {
        "id": "cac59bd.c146e68",
        "type": "ui_gauge",
        "z": "d87acc50.862d1",
        "name": "Temperature",
        "group": "bc7f15c3.5ac668",
        "order": 2,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Temperature",
        "label": "C",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "x": 410,
        "y": 220,
        "wires": []
    },
    {
        "id": "4e713a7.4bf4fc4",
        "type": "switch",
        "z": "d87acc50.862d1",
        "name": "Check Temperature Level",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "lt",
                "v": "21",
                "vt": "str"
            },
            {
                "t": "gte",
                "v": "22",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 430,
        "y": 540,
        "wires": [
            [
                "254d0bdc.c7cb54"
            ],
            [
                "c3824d67.55e6b"
            ]
        ]
    },
    {
        "id": "254d0bdc.c7cb54",
        "type": "change",
        "z": "d87acc50.862d1",
        "name": "Turn Relay On",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "0",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 680,
        "y": 480,
        "wires": [
            [
                "32cbf966.4b3ac6",
                "6fb27ee.404958",
                "f0979053.c67f5"
            ]
        ]
    },
    {
        "id": "c3824d67.55e6b",
        "type": "change",
        "z": "d87acc50.862d1",
        "name": "Turn Real Off",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 670,
        "y": 560,
        "wires": [
            [
                "32cbf966.4b3ac6",
                "f0979053.c67f5"
            ]
        ]
    },
    {
        "id": "32cbf966.4b3ac6",
        "type": "mqtt out",
        "z": "d87acc50.862d1",
        "d": true,
        "name": "Send Command to GPIO 37",
        "topic": "/relayboard/GPIO/37",
        "qos": "",
        "retain": "",
        "broker": "8225a193.5398a",
        "x": 1110,
        "y": 440,
        "wires": []
    },
    {
        "id": "6fb27ee.404958",
        "type": "rpi-gpio out",
        "z": "d87acc50.862d1",
        "name": "Relay On/Off",
        "pin": "37",
        "set": true,
        "level": "0",
        "freq": "",
        "out": "out",
        "x": 990,
        "y": 580,
        "wires": []
    },
    {
        "id": "8047225e.861da",
        "type": "ui_chart",
        "z": "d87acc50.862d1",
        "name": "",
        "group": "bc7f15c3.5ac668",
        "order": 2,
        "width": 0,
        "height": 0,
        "label": "chart",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "0",
        "ymax": "100",
        "removeOlder": "10",
        "removeOlderPoints": "",
        "removeOlderUnit": "60",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "x": 400,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "f0979053.c67f5",
        "type": "ui_switch",
        "z": "d87acc50.862d1",
        "name": "",
        "label": "Relay 1",
        "tooltip": "",
        "group": "b91687b1.1218f8",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "",
        "style": "",
        "onvalue": "0",
        "onvalueType": "str",
        "onicon": "",
        "oncolor": "",
        "offvalue": "1",
        "offvalueType": "str",
        "officon": "",
        "offcolor": "",
        "x": 840,
        "y": 580,
        "wires": [
            [
                "6fb27ee.404958"
            ]
        ]
    },
    {
        "id": "c0ebb121.9d1eb",
        "type": "ramp-thermostat",
        "z": "d87acc50.862d1",
        "name": "",
        "profile": "9ca4b3cb.a8589",
        "hysteresisplus": "1",
        "hysteresisminus": "1",
        "x": 300,
        "y": 660,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "8225a193.5398a",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT-Raspberrt Pi4",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "bc7f15c3.5ac668",
        "type": "ui_group",
        "z": "",
        "name": "Sensor",
        "tab": "215e58b8.b33b68",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "b91687b1.1218f8",
        "type": "ui_group",
        "z": "",
        "name": "GPIO",
        "tab": "215e58b8.b33b68",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "9ca4b3cb.a8589",
        "type": "profile",
        "z": "",
        "name": "Test Thermostat",
        "time1": "00:00",
        "temp1": "21",
        "time2": "23:59",
        "temp2": "22",
        "time3": "",
        "temp3": "",
        "time4": "",
        "temp4": "",
        "time5": "",
        "temp5": "",
        "time6": "",
        "temp6": "",
        "time7": "",
        "temp7": "",
        "time8": "",
        "temp8": "",
        "time9": "",
        "temp9": "",
        "time10": "",
        "temp10": ""
    },
    {
        "id": "215e58b8.b33b68",
        "type": "ui_tab",
        "z": "",
        "name": "Room",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

The ramp thermostat node replaces your three switch nodes. Your method will work fine if you want fixed target temperature and fixed hysteresis, but it gets complex if you want to be able to adjust these, like a real thermostat.

Here is an example of the node configured with a target set to 21, but with the addition of an inject node that sets the target to 20. The target input could come from a slider on the dashboard for example.

image

Feed your temperature in the front alongside the inject node. Output 1 of the node goes to the GPIO.

[{"id":"8e12689f.e371","type":"inject","z":"6d810db7.640bf4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"setTarget","payload":"20","payloadType":"num","x":140,"y":720,"wires":[["89b2fc12.618a98"]]},{"id":"89b2fc12.618a98","type":"ramp-thermostat","z":"6d810db7.640bf4","name":"","profile":"d2f590cc.52e34","hysteresisplus":"0.1","hysteresisminus":"0.1","x":320,"y":620,"wires":[[],[],[]]},{"id":"d2f590cc.52e34","type":"profile","name":"Fixed 21","time1":"00:00","temp1":"21","time2":"23:59","temp2":"21","time3":"","temp3":"","time4":"","temp4":"","time5":"","temp5":"","time6":"","temp6":"","time7":"","temp7":"","time8":"","temp8":"","time9":"","temp9":"","time10":"","temp10":""}]

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