Home Assistant Integration for Tinxy

It was easy to integrate your py script and use the data( passkey encoded) with node red function to get the local api working with my automation system. Just wanted to know if I can completely port it to node red function rather than executing a py script from node red. What all things are used in tinxy dependency ( pip tinxy source code for local api passkey encryption).

Can you share the code for the flow and the list of plugins used?

every plugin comes by default with node red.
the function code is this:
have used exec node for executing cli “python3 toggle py” scrypt

var passkey = msg.payload.replace("\n","");
var json={
“password”: passkey,
“action”: msg.action,
“relayNumber”: msg.relaynumber
};
var out = {
“url”:“http://yourTinzyIPHere/toggle”,
“payload”:JSON.stringify(json)
}
return out;

have tweaked the py script to print(data) .
Still I would like to transfer it all to node red function ( without executing py script for passkey )

Finally some light in the tunnel. :slight_smile:

Is it possible to share json flow from node red? Also how is the response time?

Will try to share tonight. The delay here is around 200ms, majority of time taken by py script to encode passkey, I will try to port it all to js ( for node js simplicity). Want help from script maker arvind to port it to node red completely. Also one more thing remains is to get the current ip of tinxy node, currently I have allocated static IP to tinxy from router, It works great, the local rest api is fast af in response. Also can load up commands ( its asynchronous) great work done by tinxy embedded devs. I hope they won’t make any breaking changes with this local api, otherwise I will have to flash the tinxy with my own or 3rd party fw, I want to use the online services by tinxy and want to integrate local control in NR/HA for my non supervised automations to work

We do not plan to break offline api . If we do , we will let you guys know in this thread

Thats a great relief , this post will be a good decision making point for many home automation freaks like us here. I have already bought multiple units, will buy more to completely automate my home( with additional automations by me :wink: with node red and HA).

I wanted to ask one more thing, the offline control from Android app works great but it takes more time compared to this py script implementation. Why is it so, why it takes more than 500-700ms for offline control, also if I press any other button in the app before the first command is parsed then there is command drops . Just wanted to know is this normal or is there any issue with my unit.( tested on 2 unit after upgrading fw to v5)

Okay so here are the steps for it.

  1. Download the py script with this command " sudo wget https://gist.githubusercontent.com/arevindh/b24bf626c0d55cd5afd0aa5fecd1e875/raw/1f4fbcb40a2f30bf976edaccd12b107d1db460c4/toggle.py" also install the dependency with command “pip install tinxy”
  2. Modify the script as below.

pip install tinxy

Python 3

import time
import base64
import requests
from tinxy import tinxy
import time

def encrypts(arg1, mqttpass):
en_arg1 = tinxy.strToLongs(arg1.encode(‘utf-8’).decode())
en_mqttpass = tinxy.strToLongs(mqttpass.encode(‘utf-8’).decode())
ed = tinxy.encodes(en_arg1, en_mqttpass)
ciphertext = tinxy.longsToStr(ed)
cipherutf2 = ciphertext.encode(‘latin-1’)
cipherbase64 = base64.b64encode(cipherutf2)
return base64.b64decode(cipherbase64).hex()

tm = str(int(time.time()))
data = encrypts(tm, “your mqtt pass here”)
print(data)

  1. import the following json in node red flow.
    [{“id”:“c0b0665caa6cfd7f”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 0 in relay 2”,“props”:[{“p”:“relaynumber”,“v”:“2”,“vt”:“num”},{“p”:“action”,“v”:“0”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:400,“y”:60,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“7a9bcbdef2764716”,“type”:“exec”,“z”:“600597defeb9b915”,“command”:“python3 toggle.py”,“addpay”:"",“append”:"",“useSpawn”:“false”,“timer”:"",“winHide”:false,“oldrc”:false,“name”:"",“x”:590,“y”:60,“wires”:[[“57068fc962c583c4”,“333f898707d87936”],[“f594cdedcf472beb”],[“b99b6fe5df99dd2d”]]},{“id”:“f594cdedcf472beb”,“type”:“debug”,“z”:“600597defeb9b915”,“name”:"",“active”:false,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“targetType”:“full”,“statusVal”:"",“statusType”:“auto”,“x”:770,“y”:160,“wires”:[]},{“id”:“b99b6fe5df99dd2d”,“type”:“debug”,“z”:“600597defeb9b915”,“name”:"",“active”:false,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“targetType”:“full”,“statusVal”:"",“statusType”:“auto”,“x”:770,“y”:200,“wires”:[]},{“id”:“57068fc962c583c4”,“type”:“debug”,“z”:“600597defeb9b915”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“targetType”:“full”,“statusVal”:"",“statusType”:“auto”,“x”:790,“y”:120,“wires”:[]},{“id”:“73f172d3ec41f7af”,“type”:“http request”,“z”:“600597defeb9b915”,“name”:"",“method”:“POST”,“ret”:“txt”,“paytoqs”:“ignore”,“url”:"",“tls”:"",“persist”:false,“proxy”:"",“authType”:"",“senderr”:false,“credentials”:{},“x”:650,“y”:360,“wires”:[[“700c8acff3d68bcc”]]},{“id”:“333f898707d87936”,“type”:“function”,“z”:“600597defeb9b915”,“name”:"",“func”:“var passkey = msg.payload.replace(”\n","");\nvar json={\n “password”: passkey,\n “action”: msg.action,\n “relayNumber”: msg.relaynumber\n};\nvar out = {\n “url”:“http://YOURTINXYIPHERE/toggle”,\n “payload”:JSON.stringify(json)\n}\nreturn out;",“outputs”:1,“noerr”:0,“initialize”:"",“finalize”:"",“libs”:[],“x”:520,“y”:320,“wires”:[[“73f172d3ec41f7af”,“700c8acff3d68bcc”]]},{“id”:“700c8acff3d68bcc”,“type”:“debug”,“z”:“600597defeb9b915”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“targetType”:“full”,“statusVal”:"",“statusType”:“auto”,“x”:730,“y”:420,“wires”:[]},{“id”:“9e32e0ac1aefd5ee”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 1 in relay 2”,“props”:[{“p”:“relaynumber”,“v”:“2”,“vt”:“num”},{“p”:“action”,“v”:“1”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:400,“y”:100,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“435dfe37a4cebd2c”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 0 in relay 1”,“props”:[{“p”:“relaynumber”,“v”:“1”,“vt”:“num”},{“p”:“action”,“v”:“0”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:180,“y”:60,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“5b3741883c366066”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 1 in relay 1”,“props”:[{“p”:“relaynumber”,“v”:“1”,“vt”:“num”},{“p”:“action”,“v”:“1”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:180,“y”:100,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“61cd223d0295953c”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 0 in relay 4”,“props”:[{“p”:“relaynumber”,“v”:“4”,“vt”:“num”},{“p”:“action”,“v”:“0”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:400,“y”:160,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“87a9d90bb47eecff”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 1 in relay 4”,“props”:[{“p”:“relaynumber”,“v”:“4”,“vt”:“num”},{“p”:“action”,“v”:“1”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:400,“y”:200,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“c448257230a537c9”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 0 in relay 3”,“props”:[{“p”:“relaynumber”,“v”:“3”,“vt”:“num”},{“p”:“action”,“v”:“0”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:180,“y”:160,“wires”:[[“7a9bcbdef2764716”]]},{“id”:“a0e1782428caf93e”,“type”:“inject”,“z”:“600597defeb9b915”,“name”:“inject 1 in relay 3”,“props”:[{“p”:“relaynumber”,“v”:“3”,“vt”:“num”},{“p”:“action”,“v”:“1”,“vt”:“str”}],“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“topic”:"",“x”:180,“y”:200,“wires”:[[“7a9bcbdef2764716”]]}]
  2. After importing the flow assign a local IP to your tinxy device with mac reservation in your router.
  3. set the same IP in the function of node red.
    Deploy and it will start working.

This is just the start, I am trying to get the dynamic IP so that the function wont need any kind of static IP.
Also make sure to change the py command location as per your saved location.

2 Likes

So I have tested in Node Red. Works great. Few pointers for wider community who wants to try it out.

  1. If you have NodeRed installed as AddOn in home assistant , it is difficult to install additional python packages (tinxy, requests) as you need to access underlying docker container. Also the packages got wiped after every restart of home assistant.
  2. The JSON script has trivial “” encoding issue. We need to replace double quotes properly before importing to nodered
  3. I am yet to find best way to really make it work for my use case which is controlling devices through wall mounted dashboard. I can create button entity for every switch and then link it to each node red flow but thats a lot of work (for total 25-30 switches).

Neverless, great work and very simple code. Do let us know if you manage to port it to js with all dependencies included.

Also my suggestion is not to invest much time into dynamic Ip thing. Most of the folks who will make it work should be fine with some extra static IPs :slight_smile:

Here is the code with markups

[{"id":"33fd95b26c3a8765","type":"tab","label":"Tinxy API","disabled":false,"info":"","env":[]},{"id":"6cc4d4be8fab825e","type":"exec","z":"33fd95b26c3a8765","command":"python3 /config/pyscript/toggle.py","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"fancy lights","x":390,"y":240,"wires":[["4a1964653fab90b3","f950ee8947dfee37"],["c524be4b5e0aad42"],[]]},{"id":"4a1964653fab90b3","type":"debug","z":"33fd95b26c3a8765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":220,"wires":[]},{"id":"f950ee8947dfee37","type":"function","z":"33fd95b26c3a8765","name":"","func":"var passkey = msg.payload.replace(\"\\n\",\"\");\nvar json = {\"password\":passkey,\"action\": msg.action, \"relayNumber\":msg.relaynumber};\nvar out = {\"url\":\"http://192.168.68.162/toggle\",\"payload\":JSON.stringify(json)};\nreturn out","outputs":1,"noerr":0,"initialize":"\n","finalize":"","libs":[],"x":540,"y":340,"wires":[["66768bbba9ca7afb","73f172d3ec41f7af"]]},{"id":"66768bbba9ca7afb","type":"debug","z":"33fd95b26c3a8765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":340,"wires":[]},{"id":"73f172d3ec41f7af","type":"http request","z":"33fd95b26c3a8765","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":570,"y":440,"wires":[["cf515e1995a56e9d"]]},{"id":"cf515e1995a56e9d","type":"debug","z":"33fd95b26c3a8765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":440,"wires":[]},{"id":"2197350cd8094944","type":"ha-button","z":"33fd95b26c3a8765","name":"test button","version":0,"debugenabled":false,"outputs":1,"entityConfig":"d14eab9a1a994f37","outputProperties":[{"property":"relaynumber","propertyType":"msg","value":"1","valueType":"str"},{"property":"action","propertyType":"msg","value":"1","valueType":"str"}],"x":200,"y":240,"wires":[["6cc4d4be8fab825e"]]},{"id":"c524be4b5e0aad42","type":"debug","z":"33fd95b26c3a8765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":280,"wires":[]},{"id":"d14eab9a1a994f37","type":"ha-entity-config","server":"c0831150.1f2cf","name":"nodered trigger","version":2,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""},{"property":"device_class","value":""}],"entityType":"button"}]

@omkarbest @XxaA I was thinking of making it into a small docker container that will convert everything to MQTT with auto-discovery. What is your opinion?

I tried to get it integrated as HA addon but my python skills are not enough for that.

I did get tasmota working with the 6/4/2 node so you do have a good option for using the relays with home assistant

Can you share the details

Let me know if you need any more details

I am in , but you said mqtt, that would be online mode only right?

Ah sorry for that direct copy paste lol, I use HA in supervised mode in rpi os as all my automation are on NR and I do use pi for other works too. Also you can just make additional lights/switches in HA with local Mqtt, integrate it with NR. To make it even more attractive with perfect feedback, make some automations in HA like whenever the light is turned on via MQTT ( online Tinxy) then change the state of offline switch to on. I will do this and will only make offline mqtt switch as visible in HA

1 Like

In local mode using the python package.

Great !! meanwhile I was unable to decode the function which you wrote in tinxy dependency xD. Will give it another shot when I get enough free time.

Hello,
Can someone please help with the detailed steps to install TInxy custome repo with the home assistant?
Thanks

  1. Install HACS like mentioned in the top post
  2. Open HACS, click on integrations and then click on the 3 dots on the top right
  3. click on Custom repositories and add the URL for the GitHub in it. Category doesn’t really matter but select integration, and click add.
  4. click on the Explore & Download Repositories button on the bottom right, search for the name of the repository and click “Download this repository with HACS” on the bottom right.
  5. It will install the repository, restart home assistant for it to take effect