Home Automation Sonoff-Tasmota Sensors, LEDs Development Board, ESP12

by FlyingDrMike in Circuits > Wireless

50818 Views, 193 Favorites, 0 Comments

Home Automation Sonoff-Tasmota Sensors, LEDs Development Board, ESP12

sentitl.jpg

I have been blown away by the capability of the Sonoff-Tasmota firmware for the Sonoff devices. These, combined with a Raspberry Pi loaded with Node-Red and a MQTT broker (Mosquitto) provides huge scope for delivering many home automation needs. The Sonoff-Tasmota firmware enables a good deal more than the as-supplied Sonoff units. Some examples have appeared showing sensors, such as temperature and humidity being wired in. I was concerned that this might be unsafe and may not comply with EU and US regulations because the Sonoff circuit boards may not meet the requirements for double insulation. The track separation distances on the Sonoff Basic do not appear to comply. The development here gives an alternative and safe way to explore the additional capability. This board can be powered for most applications via a USB charger. The LED strings may need greater current capability, and the 12v RGB LEDs will also require a 12v supply.

The objective of this instructable is to provide a ‘get started’ reference for this additional capability. It took a while to find some of the code needed and hopefully this reference will smooth the way to whatever automation task you have in mind.

Node-Red has been used as the automation client, running on a Raspberry Pi. This Instructable can be seen as an extension of my earlier Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red. The examples should however be easily transferable to other platforms.

The ESP12 can be programmed in-situ by connecting a USB serial converter.

The additional functionality targeted by the ESP12 breakout board here is:

  • Temperature and humidity sensor (e.g. AM2301)
  • I2C device/devices (temperature, pressure, humidity, ambient light - see list on the wiki)
  • 5v, individually addressable RGB LED strings (Neopixels/WS2812)
  • 12v, RGB LED strings (single programmable colour)
  • Proximity sensor, or other sensor with on/off output
  • IR remote control
  • Up to 3 relays

Quite a few of these can be handled by a single ESP12! The latter two use the open collector outputs used for the RGB LED string plus appropriate changes to the configuration (that can be done via Node-Red). The system does not support both types of LED strings on one device.

So with one ESP12 device (costing £1.60) plus this board you could:

  • Measure temperature and humidity.
  • Communicate with a range of I2C devices for temperatue, humidity, pressure, ambient light.
  • Drive a NeoPixel LED light string display. This could be used to make a mood-light.
  • Add a proximity sensor for house alarm (email alert)
  • Select TV, HiFi etc. settings using the IR via a remote control interface(s) on ones smartphone.
  • Drive a couple of relays for other devices.

The board is small and cheap enough that one would be happy to use it for just one of these features. The principles can be applied to other setups and breadboard based development.

I will show the connections and initial programming for each of these devices and then show an automation example – turning on an electric blanket in time for bedtime, if the room temperature is below a selected value.

Circuit Board

senpcb.jpg

I have attached the PCB artwork. The same result could be achieved via breadboard or stripboard, but the PCB makes a much neater solution.

I have not included an overall circuit diagram as the PCB is single sided and hence easy to reverse engineer for breadboard/stripboard from the PCB picture and artwork.

Components required:

PCB (from attached artwork)

AMS1117 3.3 (20off for £0.99 on ebay)

3 off SOT-23 N channel MOSFET - see comment below

0805 4148 diode

5mm LED green (or red)

0805 ceramic capacitors >6v working voltage):

3 off 0.1uf

1 off 22uF

1 off 4.7uF

1206 resistors

2 off 0ohm

0805 resistors:

1 off 10ohm

1 off 100ohm

3 off 3k3ohm

1 off 5k1ohm

4 off 10kohm

0.1” Pin header 19 ways

2mm pinheader 16 ways

2 off 6mm tactile switch

ESP-12E or F (latter has better range)

I used an obsolete (but high current) MOSFET in the prototype when I realised that the popular 2N7002 was not going to drive more than around 30LEDs. My next boards will use the PMV40UN2 , Farnell code: 2242071. These have an on resistance of 50mohms with only 2.5v on the gate. There will be plenty more options. Check for working voltage over 15V and on resistance below 250mohms with 3v gate voltage.

My approach to PCB making is to print the artwork twice onto tracing paper. I then overlay these to double the contrast and cover any small imperfections in the printing (I use a laser printer). I punch holes in the edge of the upper layer, place Sellotape across the holes, align and then press on the holes to stick. I have a UV exposure unit. I used to use a UV black light that worked fine with spray coated PCBs and should work fine with positive photoresist boards. I use weak sodium hydroxide solution (drain cleaner) to develop and Di-Sodium Peroxodisulphate Hexahydrate to etch. Take special precautions with the chemicals, especially the sodium hydroxide that attacks flesh instantly. I then expose again and develop to get rid of the film over the tracks and finish off with some immerse tin (quite expensive – and limited life). The latter step is optional.

I drilled the holes for the for the LED and the 2mm pin strip for the ESP-12F at 0.7mm and the rest at 0.9mm.

For component placement see photo below. I use solder paste. Hence the approach - place component, place small blob of paste, solder. Move through all the components. Then apply paste to all the unsoldered pins and then solder these. I leave through-hole parts until after the SMD parts have been be completed.

Note that the LED cathode (flat on base) goes towards the USB connector edge.

Lastly add some guide marks on the board to show +, -, 5v TX and RX:

Downloads

Initial Programming and Set-up

senprog.jpg

A good starting point is to have set up a raspberry pi with Node-Red and mosquito as described in Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red

The key however is to have Node-Red running and connected to a MQTT broker.

I will also assume you have set up an Arduino IDE with the Sonoff-Tasmota firmware as also described in Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red

The first stage of the setup is to program the ESP-12F. Connect Ground, TXD and RXD from a USB to 3.3v serial converter. Power up the board either by connecting a USB lead to a USB host/power supply or connecting the serial converters 5v pin to one of the two 5v pins on the board. Set up the Arduino IDE as described in Instructable above, except that the flash size (Tools) can be set to 4M(1M SPIFFS). Also edit the user-config.h file with at least your wifi SSID and password and MQTT_HOST IP address. Now press and hold the GPIO 0 button while pressing and releasing the reset button, and continue to hold for at least 3 seconds. Then click upload and wait for confirmation this has been completed. Disconnect the serial converter, apply power via USB lead, and press the reset to restart the ESP.

If you are continuing where my earlier Instructable left off you will have set up usernames and passwords for security. It is easier to disable these while adding new devices. So log onto your pi via a PuTTY session (or direct) and change the mosquito config file to allow anonymous users:

sudo /etc/init.d/mosquitto stop

cd /etc/mosquitto/conf.d/

sudo nano mosquitto.conf This starts the editor.

Insert a # at start of the lines “allow_anonymous false” and “password_file /etc/mosquitto/conf.d/passwd” . This comments these lines out and is easy to uncomment later.

Save and exit (Ctrl+X), Y, enter.

Restart mosquitto via sudo /etc/init.d/mosquitto restart .

This disables the password security. Enable later as described in the Security step on my earlier Instructable.

Now log on to Node-Red using the Pi IP address:1880

In Node-Red, if you don’t already have it, add an mqtt input node and set the topic to a catch-all +/+/+. Connect this to a debug node to show the mqtt traffic:

Also, if you don’t already have it, set up an inject node and connect to an mqtt output node. Leave the mqtt node topic blank as we will set this in the inject node. The mqtt out node will need the IP address of the mqtt server (localhost if both are running on the same device):

The inject node (with Payload set to string) will be used to send combinations of Topic and Payload to set up the ESP-12F and set the GPIO pins to their appropriate functions.

Initial setup.

First it is useful to give the unit a new name/Topic. The firmware defaults to ‘sonoff’ that is a good starting point but can only be one off.

I named the unit ‘sensor’ by entering cmnd/sonoff/Topic as Topic and sensor as Payload in the inject mode. Click deploy and then when the mqtt nodes show connected click grey box on the LHS of the inject node to send (publish) the command (data). Check the responses in the debug tab – a restart with the new name.

We now need to tell the firmware what type of module it is loaded on. The nearest is a WeMos D1 mini, code 18. So send Topic: cmnd/sensor/Module, Payload: 18 . Deploy and send. The unit will restart and show "Module":"WeMos D1 mini".

Now we can start playing.

LEDs Relays, Buttons and Switches

Before looking at the extended functionality it is worth summarising the core capability. I was initially confused as to how to use a switch as a sensor input (see Microwave/Radar People Detection, later). I hope that this description complements the Wiki information that focuses on commands rather than function. Replace 'sonoff' in the instructions below with device name if this has been changed.

LEDs

Sonoff-Tasmota supports up to 4 LEDs. However it looks like only LED1 is controllable. It can be turned on and off using cmnd/sonoff/LedPower, ON or OFF. It can also be set to respond to relay power and MQTT messages in different ways. This is set by cmnd/sonoff/LedState. See Wiki for options. When set to respond to power state the LED is on if any relay is on. It will blink to all MQTT messages if this option is selected.

If one wants a LED to show a relay is activated the best way is to wire the LED and dropping resistor in parallel with the relay.

Relays

The firmware now supports up to 8 relays. However add relays in order. i.e. do not install just a Relay1 and a Relay5. The firmware will try to drive two relays from cmnd/sonoff/POWER1 and cmnd/sonoff/POWER2 . So to avoid confusion install relays as 1,2,3……

Buttons and Switches

The firmware supports up to 4 buttons and 4 switches. The default is that these drive their associated relays. So Button1 toggles Relay1 and Switch1 will switch Relay1 and so on.

Buttons can also do other things, including resetting the device. The extended button functionality can be disabled using cmnd/sonoff/SetOption1 . See Wiki for details.

If a button input is used as a sensor input with the intention that a different device should respond then the ButtonTopic instruction should be used to set the topic to that of the device that should be changed. This disables the link from all the buttons to their associated relays on the same device. So if Button1 on a device called sonoff should drive another device set with a name say 'loungelight'. Then use cmnd/sonoff/ButtonTopic with payload loungelight. The Button1 action will then publish cmnd/loungelight/POWER1 , Toggle . The link still has to be from Button1 to Relay1 and so on.

Switch actions can be set individually to toggle, follow, inverted follow and some others via the command cmnd/sonoff/SwitchMode . See Wiki for details.

If a switch input is used as a sensor input with the intention that a different device should respond then the SwitchTopic instruction should be used to set the topic to the device that should be changed. This disables the link from all the switches to their associated relays on the same device. So if switch1 on a device called sonoff should drive another device set with a name say loungelight. Then use cmnd/sonoff/SwitchTopic with payload loungelight. The Switch1 action will then publish cmnd/loungelight/POWER1 ON or OFF. The link still has to be from Switch1 to Relay1 and so on.

Temperature and Humidity

I started with the AM2301 because it was under £2 on ebay. It is fine to show the principles and the code required, but I was not impressed with the accuracy – reading high by more than 2°C.

First send the topic cmnd/sensor/gpios to get a list of the gpio options. The AM2301 is “2”. We want to connect this to gpio 1 so send topic cmnd/sensor/gpio1 with payload 2 . You should see a response in the debug output that includes "GPIO1":"2 (AM2301)" and the device will restart.

Connect 3.3v, data and ground of the AM2301 to the 3 way header, double checking polarity:

Power up (USB supply). The data is sent every 5 minutes. It can also be obtained on demand via a request for sensor data. This is done with Status 10. So send Topic: cmnd/sensor/Status , Payload: 10 . You will see in both cases the data appears in a somewhat complex/nested JSON string. The 5 minute data comes with topic tele/sensor/SENSOR. So this is what we need to subscribe to. The first level of the JSON string has names Time, Switch1, AM2301 and TempUnit. Within the AM2302 data we have another JSON string with names Temperature and Humidity. This needs to be converted from a JSON string to a JSON object before we can extract the data. Fortunately there is a node to do this.

Set up an mqtt input node with Topic: tele/sensor/SENSOR and connect this to a JSON function and from there into a function node and thence to a debug node. The function node can then extract temperature using the code:

var temp=msg.payload.AM2301.Temperature;

var msg = {

payload:temp

};

return msg;

The setup can be imported by copying the contents of NRAM2301.txt attached . Click the Node-Red options button (top right) and select Import>Clipboard. Then paste the code and place it into your flow.

A similar approach can extract Humidity.

At this stage the data is just being sent to the debug. It can also be sent to a web interface using a dashboard text, gauge or chart. It can also be made available globally for decision making as will be shown later in the electric blanket example.

Downloads

NeoPixel LED String

senneo.jpg

The individually addressable LED string will be limited in speed when updating individual LEDs via Wifi. However there is a suite of patterns/settings embedded in the Sonoff-Tasmota firmware that can be selected via commands from the system. One of these is shown later.

From a hardware perspective these need a 5v supply (around 1.5A per 60 LEDs) and a 5v data signal. The inputs are Schmitt trigger and hence will not respond to the ESP 3.3v output. The ideal solution is to use a logic buffer – e.g. 74HCT125. These are 5v but with TTL level inputs that will respond to 3.3v levels. There are single gate versions in SOT-353 form: 74HCT1G125/74AHCT1G125. There are also 74HCT1G126/74AHCT1G126 gates that work the same but have a non-inverted output enable input. I didn’t have any of these available and so used a different method, using a diode and pull up resistor to shift the 0-3.3v levels to 0.7-4v – that works – or at least does if you also add a small capacitor (10-22pF) on the output. The circuit is:

This worked fine for testing with the string attached directly, but I would use the logic chip if there was to be a lead from the board to the string.

The string connects as in the picture above. Note also to add a capacitor to the 5v string supply. 1000uF is advised but I found 680uF worked fine. Working voltage should be 6.3v or greater.

We want to connect the LED String to gpio 13 so send topic cmnd/sensor/gpio13 with payload 7 (latter found from the cmnd/sensor/gpios sent earlier). You should see a response in the debug output that includes "GPIO13":"7 (WS2812)" and the device will restart.

We need to tell the system how long the string is. So send topic cmnd/sensor/Pixels with payload 60 (or other if different length string).

A Rainbow sequence can be set up using Topic cmnd/sensor/Scheme and Payload 11 .

There are many more options to play with here. Try the other schemes. There are also settings for the width for each colour and the speed plus a fade option to smooth transitions. See the Wiki>Commands and have a play. I found that the blue and green LEDs were too bright compared to RED. So I made some adjustments to the colours in xdrv_ws2812.ino changing the code to the settings below:

WsColor kRainbow[7] = { 255, 0, 0, 255, 47, 0, 255, 149, 0, 0, 255, 0, 0, 0, 255, 127, 0, 255, 255, 0, 79 };

I found the numbers to use by setting up a colour wheel to drive the string to a single colour. To do this add a colour picker node and connect this to a mqtt output node. In the colour picker set the Group to new name Colour and new tab LED. Also tick the “Always show picker” and “Always show value field” . In the mqtt node set the topic to cmnd/sensor/Color . Click deploy. Log onto the dashboard with a browser set to IP address:1880/ui and have a play.

The Node-Red colour picker setup is attached as NRcolour.txt. Import as in the Temperature and Humidity example if required.

Downloads

RGB LED Strings and Relays

The RGB LED string is driven via three pwm outputs, one for each colour. These also need 12V.

First set up the pwm outputs. For Red set up pwm1 on gpio4. pwm is '33' from the gpios enquiry sent earlier. So send topic cmnd/sensor/gpio4 with payload 33. The response will include "GPIO4":"33 (PWM1)". Now do the same for green and blue with topic cmnd/sensor/gpio2 with payload 34 and then topic cmnd/sensor/gpio15 with payload 35 .

Next check the LED string to find with pin is which colour by connecting to a 12v supply. On mine the common +12v was white, the green wire was blue, red wire red and blue wire green.

Now connect as below:

The system can either drive a WS2812 neopixel string or an RGB string but not both at the same time. To switch to the RGB string and pwm outputs use topic cmnd/sensor/SetOption15 and payload 1 . If a WS2812 string was connected it will hold the last colour set. When switching back the RGB outputs are set to Green. With SetOption15 set to 1 or ON the RGB string can be controlled by the colour picker as setup for the NeoPixel string in the previous section. If you want to drive the PWM outputs you need to remove (or not install) the WS2812 NeoPixel gpio (remove via cmnd/sensor/gpio13 and payload 0).

Relays

A relay can be driven from each of the three MOSFET drives used for the RGB strings. To set up the 'red' output (gpio 4) to be Relay1 we need to send cmnd/sensor/gpio4 with payload 17 . We can then turn this on and off with cmnd/sensor/POWER1 and Payload ON or OFF. To put Relay2 on green use cmnd/sensor/gpio2 with payload 12 . This can be controlled with cmnd/sensor/POWER2 .

IR Remote

The IR remote capability has been programmed for major players e.g. Sony, Samsung. Check the Sonoff-Tasmoto Wiki for details (IR at bottom of page).

Connect an IR diode to the 2 pin connector. The negative pin is next to the flat on the diode base. I used an LD271. Hopefully you will have a diode from that old IR remote you didn't throw away!

We need to set gpio15 for the IR. So send topic cmnd/sensor/gpio15 with payload 8.

Follow the link on the wiki for the IR remote database. My test was on an LG television. This uses the NEC protocol and 32 bits. I could not find the exact model but one that was pretty close. Power was 0x10EF and the pre-data 0x20DF so all together 0x20DF10EF or 551489775 decimal. So the data/payload to send to topic cmnd/sensor/IRsend is {"Protocol": "NEC","Bits": 32,"Data": 551489775} . The response should be: {"IRSend":"Done"} . This turned my TV on and off. The range was around 3m with the IR diode pointed in the right direction. There is scope for increasing the power by reducing the 10ohm resistor. It I were to do this I would also add a capacitor, say 22uf across the power rails at the location below.

From an interface perspective it is easy to add dashboard buttons, label these and add the payload with the IR code and send then to a mqtt out node with the topic as above.

Microwave/Radar People Detection

I was intrigued by the incredibly cheap radar/microwave detection modules. So I thought I would set one up to tell me someone is at the door. With the sensor inside it will respond to someone around 4m away - through a grp (I think) side panel to the door.

The sensor has 5 connections but we only need VIN, OUT and GND. These need to be connected to 5v, input and ground. I used an old servo lead to make a connector, swapping the yellow and red so the input would be the middle pin. This connects to the board as below:

The input goes to gpio12 and the action is switch1 in follow mode (1).

So send topic cmnd/sensor/gpio12 with payload 9 and then topic cmnd/sensor/SwitchMode1 with payload 1 .

The system is set up on the assumption that the switch will change the output relay. To use it as an input to drive another device we need to change the topic. In this case Node-Red needs to pick it up. So use cmnd/sensor/SwitchTopic with senSW as payload. We can subscribe to this and trigger a voice alert.

So set up a mqtt input node with topic cmnd/senSW/POWER1 and connect this to a function node and the function node to a dashboard audio out node. The code for the function is:

if(msg.payload == "OFF") return null;

var str = "someone is at the door";

msg={

payload:str

};

return msg;

The audio out node needs the Group (web dashboard page) setting, but it can be any as there is no visible icon. Also tick the ‘play audio when window not in focus’.

I2C Sensors

This is pretty straight forward and similar to the AM2301 Temperature and Humidity case shown earlier. The Wiki has the list of devices supported. I tested this system with a BMP280 sensor (temperature and pressure). I actually ordered a BME180 that includes humidity - but with ebay and China things don't always arrive as described - so double check the supplier understands the difference if you are ordering one.

The I2C clock and data pins need to be defined.

So send topic cmnd/sensor/gpio3 with payload 5 for the I2C clock and then topic cmnd/sensor/gpio5 with payload 6 for the I2C data.

The connection order on the 4 pin connector on the board is: Ground, SCL, SDA, 3v3

The Node-Red setup is the same as the AM2301 Temperature and Humidity case:

The only difference is the Get temp function to extract the data where AM2301 is replaced by BMP280:

var temp=msg.payload.BMP280.Temperature;
var msg = { payload:temp };
return msg;

The board will auto-detect the I2C sensor and add the data to the tele/sensor/SENSOR payload.

For other I2C sensors connect a debug node to the tele/sensor/SENSOR MQTT node and look at the output to see the format and modify the function appropriately.

Automate an Electric Blanket

Screenshot_20171025-164311.png

The main point of this case is to show an example of a timed event that includes decision making using several inputs.

Home automation is about comfort so I thought I would set this up. The rule I wanted to code is:

"If the bedroom temperature is below a user set temperature, 30 mins before bed time, then turn the electric blanket on. Then turn off at bedtime."

A problem that has to be addressed is how to handle a decision (function) where the inputs come in at different times. The solution used here is to save data in global variables that are persistent and can be accessed by any function. An alternative, where the data is in JSON strings and hence can be identified, is to use 'context' to save data within a node and then take action when all the data is assembled.

In this case we have inputs of time, bedtime, actual temperature and set temperature. We need a function to output ON or OFF to drive the blanket switch plus something to trigger the function at a suitable frequency.

While setting this up I had browsers open both to the Node-Red page (IP address:1880) and the Node-Red dashboard page (IP address:1880/ui)to check progress. I had also programmed a sonoff switch and set the topic to bed1blnkt . See Powerful Standalone Home Automation System - Pi, Sonoff, ESP8266 and Node-Red for how to do this.

Before setting up the flows we need a time function to deliver real time as the timestamp is only UTC and ignores summer time/daylight savings. After some search I decided that the one to use is one called 'moment'. In Node-Red go to the settings (top right) and select 'Manage palette'. Then click the Install tab and enter 'time' in the search bar. I was tempted by 'BigTimer' that has many features such as calculating dusk and dawn times based on location but thought I would keep it simple at this stage. Click more to get to 'moment' and click install.

The order the pieces of the jigsaw go together is not important. I had a rough sketch of what I wanted the dashboard display to look like and so started at the top and worked down. However moving items around on a dashboard page is pretty easy so any other approach would be just as good.

So lets start with showing the current time updated every second. Add an inject node with timestamp and feed this to a moment note and from this to a dashboard text. The timestamp needs to be set to have its repeat interval set to 1 second. On the moment node set input time zone to ETC/GMT and output tz to Europe/London and Output Format to HH:mm:ss . These will be different for non-uk locations. In the text node set the Group to a new group (click the pencil) called Blanket and new Tab called Bedroom1 . Then set size to 2x1, Label: Time, and layout with Label above value. Click deploy and check Node-RED Dashboard that the time is there and ticking away.

Next add a slider and text display to give bed time in steps of 5 minutes from 10pm to 12pm - so 24 steps. Add a slider for the 24 steps, feeding a function to convert these to Bedtime and a text display to show the Bedtime. For the latter is is easiest to copy and paste the Time node so all we have to do is change the name to Bedtime. Check slider is set to Group Blanket[Bedroom1]. Set the Label to Bedtime and Range 0 to 24, Step 1. Name the function No. to Time and enter the code:

var v= msg.payload;

var str = 22+parseInt(v/12);

str += ":";

var mins = 5* (v - 12*parseInt(v/12));

if (mins<10) str += "0";

str += mins;

var msg = { payload:str };

return msg;

Click deploy and move the slider on the web dashboard to inject a value to send the Bedtime.

Note: Node-Red has good documentation and the section on functions is at: https://nodered.org/docs/writing-functions

I also wanted a manual switch as an additional way to turn the blanket on/off. So add a mqtt input note and feed this to a dashboard switch and from this to mqtt output note.

Set the mqtt input topic to stat/bed1blnkt/POWER and set QOS to 1. Set the output topic to cmnd/bed1blnkt/POWER and QOS 1. For the switch set size 2x1, untick 'if msg arrives on input, pass through to output' and select 'Switch icon shows state of the input' , and set on payload to ON and off to OFF , and set name to Bed1BlnktSW . Note this setup makes sure the switch icon shows the switch position regardless of from where it is changed.

Next we can add the bones of the setup to implement the action. I decided on running the function every 10 seconds. So setup an inject node, feed this to a moment node and from this to a function and from there to the mqtt output node driving the switch. Set the inject node to send a timestamp at an interval of 10 seconds. Set the moment node with Input Timeszone UTC, Output Format: object, and Output Tz to Europe/London. Set the function name to Blnkt ON/OFF. We will set the code for this when we have the temperature data.

Most of the room temperature setup is as shown earlier for the temperature humidity example. However instead of sending the output to a debug node sent it both to a dashboard gauge and to a function. I named the gauge Room temp , set the group to Blanket[Bedroom1], size 2x2, type donut, label Room, value format {{value}}°C , range 0-20, colour gradient blue to green to red. Name the function Bed1RoomT and enter the code:

var v= msg.payload;
global.set("Bed1RoomT",v);

return null;

This saves the temperature to the global variable Bed1RoomT .

Now we just need the slider to set the trigger temperature. Add a dashboard slider and set label to Set temp , set the rage to 0 to 20 and check the Group is Blanket[Bedroom1]. This needs to feed a gauge and a function similar to Room temp. So select these, copy and paste. Then connect to the slider output, change the gauge label to Set and the name to Set temp gauge . For the function change the name to Bed1blnktSetT and set the second line of its code to:

global.set("Bed1BlnktSetT",v);

We have the room and set temperatures as global variables. We also need the number that set the bedtime. So in the function 'No. to Time' above add just under the first line:

global.set("n2bed",v);

Now we are ready to set the code for the decision function 'Blnkt ON/OFF'. First we need to change the number from the set temp slider to the number of minutes after 10pm. Then we can do the same with the time input from moment. In order to limit the number of times the function operates when the minutes match I added the conditional that seconds were less than 30. So the code required is:

var object = msg.payload;
var v = (object.hours-22)*60 + object.minutes;

var msg = {

payload:"OFF"

};

if(v==global.get("n2bed")*5 && object.seconds <30) return msg;

var msg = {

payload:"ON"

};

if(v==(global.get("n2bed")*5-30) && global.get("Bed1RoomT")< global.get("Bed1BlnktSetT") && object.seconds <30) return msg;

return null;

One last detail is to connect the output of the 'Blnkt ON/OFF' function to the input of the mqtt output cmnd/bed1blnkt/Power .

Also go to the dashboard tab and then to layout and change the order so to get the dashboard above.

I hopes this Instructable shows some useful examples and will get you on your way to your own home automation requirements.

Mike

Downloads