# Control a Sonoff Switch with Temperature and Date

**URL:** <https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977>\
**Category:** General\
**Created:** [11 March 2020 14:10 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977 "2020-03-11T14:10:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mimk97](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mimk97](https://discourse.nodered.org/u/mimk97)\
**Post date:** [11 March 2020 14:10 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/1 "2020-03-11T14:10:03Z")

</div>

Hi,

I´m new to Node Red.  
Have installed it on Home Assistant.  
Can someone please explain how I control a Sonoff switch depending on date and temperature?  
For Example:  
Monday - Friday 06:00 - 18:00 and Temperatur (an Aqara Sensor) lower than 20.5 = Switch On  
Else Switch off

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 March 2020 14:51 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/2 "2020-03-11T14:51:38Z")

</div>

Hi, @mimk97, and welcome to the forum.

If you already can control the Sonoff from Node-RED, you can get the timed on/off commands you need from a scheduling node such as [bigtimer](https://flows.nodered.org/node/node-red-contrib-bigtimer), [schedex](https://flows.nodered.org/node/node-red-contrib-schedex) or [cronplus](https://flows.nodered.org/node/node-red-contrib-cron-plus). (A search of the flow library for 'timer' will give you more options.)

The most efficient way to handle the temperature condition is probably to read the sensor only when an "On" message arrives from the timer, and either pass or ignore the message depending on the temperature.

If you start developing a flow and run into difficulties, you can post the flow here and people will be happy to give more specific advice.

---

<div class="post-metadata">

**Author:** ![mimk97](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mimk97](https://discourse.nodered.org/u/mimk97)\
**Post date:** [11 March 2020 15:11 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/3 "2020-03-11T15:11:11Z")

</div>

Thanks.  
but which node can I use to passthrough the message depending on the temperature?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 March 2020 15:30 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/4 "2020-03-11T15:30:15Z")

</div>

> [@mimk97](#):
>
> which node can I use to passthrough the message

You have several options here. The `switch`, `function`, and `gate` ( node-red-contrib-simple-gate) nodes can all be used for this. A lot depends on whether the temperature is available at all times (for example, in a flow or global variable) or whether it is read on demand. In the second case, you might need to use the `join` node to combine the temperature and timer messages into a single message. You should look at the [Flow Control](https://cookbook.nodered.org/#flow-control) section of the NR cookbook for ideas.

---

<div class="post-metadata">

**Author:** ![mimk97](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mimk97](https://discourse.nodered.org/u/mimk97)\
**Post date:** [11 March 2020 15:38 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/5 "2020-03-11T15:38:05Z")

</div>

Thanks in Advance.  
Will try 🙂

---

<div class="post-metadata">

**Author:** ![mimk97](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mimk97](https://discourse.nodered.org/u/mimk97)\
**Post date:** [12 March 2020 10:58 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/6 "2020-03-12T10:58:36Z")

</div>

I got it work.  
But the Switch turn on every few seconds (if switch is on) and relay also make a noise  
what´s wrong?

```auto
[{"id":"287145b1.ace60a","type":"tab","label":"Heizung","disabled":false,"info":""},{"id":"3c7fac8a.06c4a4","type":"BooleanLogicUltimate","z":"287145b1.ace60a","name":"","filtertrue":"both","persist":true,"sInitializeWith":"WaitForPayload","triggertopic":"trigger","outputtriggeredby":"all","inputCount":2,"topic":"result","x":440,"y":120,"wires":[["c54e5bbb.8e18c8"],[],[]]},{"id":"faa6bbf2.dc0bc8","type":"server-state-changed","z":"287145b1.ace60a","name":"Temp Küche","server":"f1b76942.37d308","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.aqara_sensor_kueche_3","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":70,"y":120,"wires":[["7de7ffcc.23ebc"]]},{"id":"892eb83f.50bda8","type":"bigtimer","z":"287145b1.ace60a","outtopic":"","outpayload1":"true","outpayload2":"false","name":"Wochentag und Zeit","comment":"","lat":"0","lon":"0","starttime":"360","endtime":"1080","starttime2":0,"endtime2":0,"startoff":0,"endoff":0,"startoff2":0,"endoff2":0,"offs":0,"outtext1":"","outtext2":"","timeout":1440,"sun":false,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":0,"month6":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":0,"w6":0,"xday1":0,"xmonth1":0,"xday2":0,"xmonth2":0,"xday3":0,"xmonth3":0,"xday4":0,"xmonth4":0,"xday5":0,"xmonth5":0,"xday6":0,"xmonth6":0,"xd1":0,"xw1":0,"xd2":0,"xw2":0,"xd3":0,"xw3":0,"xd4":0,"xw4":0,"xd5":0,"xw5":0,"xd6":0,"xw6":0,"suspend":false,"random":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":100,"y":260,"wires":[["3c7fac8a.06c4a4","f8a72dd0.d2348"],[],[]]},{"id":"7de7ffcc.23ebc","type":"combine-bangbang","z":"287145b1.ace60a","upper":"20.5","lower":"20.4","invert":true,"name":"Soll Temp Küche","x":250,"y":120,"wires":[["3c7fac8a.06c4a4"]]},{"id":"c54e5bbb.8e18c8","type":"switch","z":"287145b1.ace60a","name":"Steuerung Küche","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"false","repair":false,"outputs":2,"x":630,"y":140,"wires":[["f6fe72f7.b656c","98e403aa.8e3b2"],["5deb1988.605738","7fe96a81.461834"]]},{"id":"5deb1988.605738","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Küche AN","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.fbh_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":160,"wires":[[]]},{"id":"f6fe72f7.b656c","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Küche AUS","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.fbh_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":100,"wires":[[]]},{"id":"7fe96a81.461834","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Pumpe AN","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.fbh_switch_3","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":240,"wires":[[]]},{"id":"98e403aa.8e3b2","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Pumpe AUS","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.fbh_switch_3","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":300,"wires":[[]]},{"id":"a7e5bb73.5a7a28","type":"server-state-changed","z":"287145b1.ace60a","name":"Temp Laden","server":"f1b76942.37d308","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.aqara_sensor_laden","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":70,"y":420,"wires":[["349d4391.affd9c"]]},{"id":"349d4391.affd9c","type":"combine-bangbang","z":"287145b1.ace60a","upper":"20.5","lower":"20.4","invert":true,"name":"Soll Temp Laden","x":250,"y":420,"wires":[["f8a72dd0.d2348"]]},{"id":"f8a72dd0.d2348","type":"BooleanLogicUltimate","z":"287145b1.ace60a","name":"","filtertrue":"both","persist":true,"sInitializeWith":"WaitForPayload","triggertopic":"trigger","outputtriggeredby":"all","inputCount":2,"topic":"result","x":440,"y":420,"wires":[["f2ce211f.91b9c"],[],[]]},{"id":"f2ce211f.91b9c","type":"switch","z":"287145b1.ace60a","name":"Steuerung Laden","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"false","repair":false,"outputs":2,"x":650,"y":420,"wires":[["98e403aa.8e3b2","a43476ac.0b4538"],["7fe96a81.461834","b339bf25.e8ea9"]]},{"id":"a43476ac.0b4538","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Laden AUS","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.fbh_switch_2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":900,"y":400,"wires":[[]]},{"id":"b339bf25.e8ea9","type":"api-call-service","z":"287145b1.ace60a","name":"Hzg Aktor Laden AN","server":"f1b76942.37d308","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.fbh_switch_2","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":900,"y":460,"wires":[[]]},{"id":"f1b76942.37d308","type":"server","z":"","name":"Home Assistant","addon":true}]

```

 ![Anmerkung 2020-03-12 115709](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9acf2e3b793c81e4bc3c3f736c9e38811de4578a.jpeg)

---

<div class="post-metadata">

**Author:** ![howl](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@howl](https://discourse.nodered.org/u/howl)\
**Post date:** [12 March 2020 11:32 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/7 "2020-03-12T11:32:58Z")

</div>

Hello,  
I have implemented my heating control with "node-red-contrib-ramp-thermostat". Take a look at this.

Holger

---

<div class="post-metadata">

**Author:** ![mimk97](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mimk97](https://discourse.nodered.org/u/mimk97)\
**Post date:** [14 March 2020 16:48 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/8 "2020-03-14T16:48:28Z")

</div>

I got it to work 🙂  
Thanks

 ![Anmerkung 2020-03-14 185130](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad4da052fc80cd2d9df0784d8b9ff16664c6981c.jpeg)

---

<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:** [13 May 2020 17:00 UTC](https://discourse.nodered.org/t/control-a-sonoff-switch-with-temperature-and-date/22977/9 "2020-05-13T17:00:56Z")

</div>

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