Script to add Tasmota device to MQTT 2.4

Now that I have made peace with the fact that the new mqtt binding is the way to go it is time to make things a bit easier.

I have quite a few SonOff Tasmota devices and have to add them all to my system.
Keep in mind that this is a “dirty” fix as you will need to first setup up the sonoff.things file as well as the sonoff.items file. There after you just need to add the sonoff topic to the payload and the device will be added to the sonoff.things and sonoff.items files. As node red is part of the openhabian system I used that.

Hope it helps !

> [{"id":"a6e87c9e.dd0b3","type":"file in","z":"da672b25.aefb78","name":"Things","filename":"/etc/openhab2/things/sonoff.things","format":"utf8","chunk":false,"sendError":false,"x":430,"y":440,"wires":[["97a6ce6f.eff1b"]]},{"id":"846d666e.951698","type":"debug","z":"da672b25.aefb78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1070,"y":440,"wires":[]},{"id":"e954eda9.87b95","type":"inject","z":"da672b25.aefb78","name":"","topic":"","payload":"add_your_topic","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":440,"wires":[["89660302.fa61d"]]},{"id":"89660302.fa61d","type":"function","z":"da672b25.aefb78","name":"","func":"var topic = msg.payload ;\nvar firstl = topic.charAt(0);\nvar last = topic.substring(1) ;\nvar caps = firstl.toLocaleUpperCase() ;\nvar desca = caps+last ;\nvar desc =desca.replace(/_/g, ' ' );\nvar newthing ='//'+desc+ \n'\\nType switch : '+topic+ '\\t\\t\\t\\t\"'+desc+'\" \t\t\t\t[ stateTopic=\"stat/'+topic+'/POWER\", commandTopic=\"cmnd/'+topic+'/POWER\"]'+\n'\\nType string : '+topic+'_Reachable\\t\\t'+'\"'+desc+'\" : Status \" \t[ stateTopic=\"tele/'+topic+'/LWT\"' +\n'\\nType number : '+topic+'_RSSI\\t\\t\\t'+'\"'+desc+' : RSSI\"\t\t\t[ stateTopic=\"tele/'+topic+'/STATE\",transformationPattern=\"JSONPATH:$.Wifi.RSSI\"]'+\n'\\nType number : '+topic+'_vcc\\t\\t\\t'+'\"'+desc+': Vcc\"\t\t\t[ stateTopic=\"tele/'+topic+'/STATE\",transformationPattern=\"JSONPATH:$.Vcc\"] '+\n'\\nType number : '+topic+'_uptime\\t\\t'+'\"'+desc+': Uptime\"\t\t[ stateTopic=\"tele/'+topic+'/STATE\",transformationPattern=\"JSONPATH:$.Uptime\"]'+\n'\\nType string : '+topic+'_ip\\t\\t\\t'+'\"'+desc+': ip\"\t\t\t[ stateTopic=\"stat/'+topic+'/ip\"]' +\n'\\n//Extra Items'+\n'\\n//Type number : '+topic+'_bmptemp\\t\\t'+'\"'+desc+' Temperature\"\t\t[ stateTopic=\"tele/'+topic+'/SENSOR\",transformationPattern=\"JSONPATH:$.BMP180.Temperature\"]'+\n'\\n//Type number : '+topic+'_bmppress\\t'+'\"'+desc+' Pressure\"\t\t[ stateTopic=\"tele/'+topic+'/SENSOR\",transformationPattern=\"JSONPATH:$.BMP180.Pressure\"]'+\t\n'\\n//Type number : '+topic+'_temp\\t\\t'+'\"'+desc+' Temperature\"\t[ stateTopic=\"tele/'+topic+'/SENSOR\",transformationPattern=\"JSONPATH:$.DS18B20.Temperature\"]';\nflow.set(\"topic\" , topic)\nflow.set(\"desc\" , desc)\nflow.set(\"newthing\", newthing);\nmsg.payload = desc;\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":440,"wires":[["a6e87c9e.dd0b3"]]},{"id":"97a6ce6f.eff1b","type":"function","z":"da672b25.aefb78","name":"Set Payload","func":"var oldthing = msg.payload.substring(0, msg.payload.length - 4)\nflow.set(\"oldthing\", oldthing);\nmsg.payload = oldthing ;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":440,"wires":[["c4e987c9.f1a048"]]},{"id":"c4e987c9.f1a048","type":"function","z":"da672b25.aefb78","name":"","func":"var newthing = flow.get(\"newthing\");\nvar oldthing = flow.get(\"oldthing\");\nmsg.payload = oldthing + newthing + '\\n\\n}\\n}\\n';\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":440,"wires":[["ff7f4cf5.2a709"]]},{"id":"ff7f4cf5.2a709","type":"file","z":"da672b25.aefb78","name":"Write new Thing","filename":"/etc/openhab2/things/sonoff.things","appendNewline":false,"createDir":false,"overwriteFile":"true","x":880,"y":440,"wires":[["846d666e.951698","a9116af9.d95e58"]]},{"id":"60653132.33646","type":"debug","z":"da672b25.aefb78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1070,"y":520,"wires":[]},{"id":"b1d809ae.29a598","type":"file in","z":"da672b25.aefb78","name":"Items Sonoff","filename":"/etc/openhab2/items/sonoff.items","format":"utf8","chunk":false,"sendError":false,"x":410,"y":520,"wires":[["d26ddbbf.c5e448"]]},{"id":"d26ddbbf.c5e448","type":"function","z":"da672b25.aefb78","name":"Set Payload","func":"var olditem = msg.payload\nflow.set(\"olditem\", olditem);\nmsg.payload = olditem ;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":520,"wires":[["40d8b047.99cec"]]},{"id":"40d8b047.99cec","type":"function","z":"da672b25.aefb78","name":"","func":"var newitem = flow.get(\"newitem\");\nvar olditem = flow.get(\"olditem\");\nmsg.payload = olditem + newitem + '\\n\\n';\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":520,"wires":[["c33884e1.206488"]]},{"id":"a9116af9.d95e58","type":"function","z":"da672b25.aefb78","name":"","func":"var topic = flow.get(\"topic\");\nvar desc = flow.get(\"desc\");\n\nvar newitem ='\\n\\n//'+desc+ \n'\\nSwitch '+topic+ '\\t\\t\\t\\t \"'+desc+'\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'\" }'+\n'\\nString '+topic+'_Reachable\\t\\t\\t\"'+desc+': Status [%s]\"\t\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_Reachable\" }'+\n'\\nNumber '+topic+'_RSSI \\t\\t\\t\\t\"'+desc+': RSSI [%d %%]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_RSSI\" }'+\n'\\nNumber '+topic+'_vcc \\t\\t\\t\\t\"'+desc+': Vcc \t[%.2f v]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_vcc\" }'+\n'\\nString '+topic+'_uptime \\t\\t\\t\\t\"'+desc+': Uptime [%s]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_uptime\" }'+\n'\\nString '+topic+'_ip\t\\t\\t\\t\\t\"'+desc+': ip [%s]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_ip\" }'+\n'\\n//Extra Items'+\n'\\n//Number maingeaserctr_bmptemp \\t\\t\\t\\t\"'+desc+' Temperature [%.2f °C ]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_bmptemp\" }'+\t\n'\\n//Number maingeaserctr_bmppress\\t\\t\\t\\t\"'+desc+' Pressure \t[%.0f ]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_bmppress\" }'+\n'\\n//Number maingeaser_temp\t\\t\\t\\t\\t\"'+desc+' Temperature [%.2f °C ]\"\\t\\t\\t\\t{ channel=\"mqtt:topic:mainBroker2018:sonoff2019:'+topic+'_temp\" }';\n\nflow.set(\"newitem\", newitem);\nmsg.payload = newitem;\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":520,"wires":[["b1d809ae.29a598"]]},{"id":"c33884e1.206488","type":"file","z":"da672b25.aefb78","name":"Write new Items","filename":"/etc/openhab2/items/sonoff.items","appendNewline":false,"createDir":false,"overwriteFile":"true","x":880,"y":520,"wires":[["60653132.33646"]]},{"id":"fa5ba2c.33f4e6","type":"comment","z":"da672b25.aefb78","name":"Set Topic","info":"","x":80,"y":400,"wires":[]}]

1 Like