How to use a Tuya Wireless Wifi Sensor with TuyAPI MQTT (ByAgentK)

Hello !!
I am from France and I would like to thank all the contributors who share their knowledge here, it has helped me a lot!
I will try to explain my problem as clearly as possible.

  • Environment :
    Openhab 3 image (openhabian) installed in Rpi 3
    tuya-mqtt by AgentK installed following this topic (Tuya devices to Openhab via MQTT - a working solution (without flashing)!)
    Everything works fine (monitor topics with mqtt explorer) with some Wifi wired Switch (no battery) !

  • Problem :
    I am trying to install a temperature/humidity Wifi sensor (wireless, with battery) in the same way.
    I don’t know its brand, but it looks exactly like this model :
    https://www.amazon.fr/Oewnvmd-Intelligent-TempéRature-Intelligente-Travailler/dp/B0B8JD59Y7
    No problem to connect it with Tuya App and get id/key from tuya cli wizard and get temp/hum from Tuya App or from Tuya log siteweb.
    However, it does not stay connected all the time, it connects to the wifi and sends its data only when it changes (connected for 2sec max).

    My problem is I have lots of difficulties to get its data.
    I can retrieve data but it is very random.
    Example, I analyse the log of the Tuya iot site, I see my temperature change 3 times in one hour (19°C, 19,5°C, 20°C) (each time the sensor connects/disconnects to the wifi)
    But on my server mqtt doesn’t get all the values directly, for example only the value 19,5°C. Even after 3 hours, I still have 19.5°C on openhab.

  • Explanation:
    The sensor only stays connected for less than 2 seconds.
    The TuyAPI tries to connect to devices at start-up. If a device is not found, the PLC tries again every 10 seconds. Of course, I am very likely to miss the moment when the temperature sensor is connected (less than 2 seconds).

  • My “bad solution”:
    In the api files, I have decreased the retry time before connecting after a failure from 10sec to 100ms… It’s works but it’s no very clean…

So, how can I manage this type of device which is not always connected to wifi with tuyamqtt ?
Maybe for example : get the connection packet of the device when it connect to wifi to “wake-up” TuyaAPI and get MQTT channels data ?

Thank you to take the time to read me. I hope that is clear. Don’t hesitate to ask me for more information.
Regards.

That seems to be odd. Normally the tuya server should retain those updates and hands it over to the client. You could try to dump it:

Or to run tuyamqtt in debug mode and see what comes via the api.

Unfortunately I can‘t test it as I don‘t have any of those devices around

Hello Flip.
Thank you very much to answer to me !

Here is a screenshot with more explanation with tuyamqtt in debug mode. No problem with devices that are always connected to Wifi (like my switches).
But the problem is when a device is only connected for only 2secondes to send its data to the cloud.


You can see I have decrease the reconnection time to 100ms. But by befault it is 10sec.
See with this bad solution, the api manages to connect when the device connects, sends its data and then disconnects.
But how do you do this without trying to connect to it all the time?

Here is my devices.conf:

I quickly checked the project behind tuya mqtt:

There are similar issues reported but no solution provided as far as I can see. We can do a PR on tuya mqtt once they have a solution in tuyapi. But there is no guarantee that it will be fixed soon (or ever).

I will do some research if there is another possibility to catch the values of those devices. Imho the states delivered by the device has to be retained somewhere. Otherwise you won‘t be able to see the values in the tuya app either

Ok thank you !

The tuyapi project I used is from AgentK : GitHub - TheAgentK/tuya-mqtt: Nodejs-Script to combine tuyaapi and openhab via mqtt
I don’t know if there is a big difference…

Why I can get the values from Tuya App ? Because when the device connect to the wifi, it send the data to the Tuya Cloud.
And the Tuya App ask data to the Tuya Cloud.

The goal of tuyapi is to intercept the communication between device and its cloud, and transform them into MQTT data for my MQTT server and Openhab.

It would be necessary to succeed in intercepting the wifi connection requests of a device

The project of @tsightler is based on tuyapi afaik. But maybe your device falls under this case:

  • TuyAPI does not support sensors due to the fact that they only connect to the network when their state changes. There are no plans to add support as it’s out of scope to intercept network requests.

To be honest, I prefer Zigbee sensors - they are just more reliable. Recently I added Lora-based sensors as their signal range is absolutely impressive!

If you want to stick with the sensor, I could only think of requesting the values via the Tuya Open API from the cloud. But this will of course break the original idea of controlling/reading your devices locally

1 Like

I read this sentence too :frowning_face: I was wondering if there wasn’t a solution…
Yes the original idea is to avoid using the cloud tuya :sweat_smile:

I will install zigbee devices using Zigbee2Mqtt !

Thank you for taking your time for me!
I won’t close the topic for now, maybe others will have ideas…

@Roms ,
@J-N-K has a Tuya Binding that I have been trying and it works quite well. Unfortunately, I only have always-on devices in my house, but i was wondering if the binding would work with battery powered sensors.

The Binding is quite straightforward, to install, so I just wanted to mention it in case you’d like to try and see if your battery-powered devices work with it. if they do, it would be great to know.

I am also a long time AgentK/tuya-mqtt user (In fact, currently, i have both AgentK solution and smarthome/j solutions installed), but i really liked the idea of having a binding that is easily installable from a repository, and that automatically detects all your devices. It is also great if your openhab setup fails and you need to rebuild, since is much less configuration.

Home this is interesting to you.

2 Likes

Hello @ricardol and thank you for your response !

Ok I will check that and try it. I’ll tell you if it works with battery operated devices.

So If I understand correctly, with the SmartHome/J Tuya Binding, no need for a configuration like AgentK/tuya-mqtt (MQTT server, devices.conf, …) ?

Do you think If I have to uninstall MQTT server, broker, bindings before install this ?

I will read the documention and try it this evening !

Hey @Roms ,

The documentation for installing the binding is here:

And the the documentation for configuring it is here:

https://docs.smarthomej.org/3.2.15/org.smarthomej.binding.tuya.html

If you already have a tuya iot project configured properly, the binding configuration consists on little more than entering your api key and secret. The binding will automatically discover your tuya devices by accessing the tuya iot project. The docs explain it quite well.

In my case, I found that defining an aditional on/off channel for eaxh device worked better with how
I define my light groups. But that is optional, and also very straightforward to do.

I did not have to remove AgentK/tuya-mqtt solution. In fact, I am running both solutions in parallel right now, since i am still testing.
Let me know how it goes.

Thanks!

1 Like

Thank you very much @ricardol ! I will test this in a few hours.

Ok so I can continue to use my wifi switches (without battery) with tuyamqtt, and just use the smarthomej binding tuya for my battery powered device. At least for the test…

But to be sure : Is the Tuya Cloud access is necessary for each Openhab startup (electrical disconnection/reconnection of my Rpi for example) ?
Or is it only needed for the first binding configuration ?
The idea after that is that I want my devices to be completely disconnected from Tuya Cloud after the first pairing or adding a new device (network setup)

Hey @Roms ,

Ok so I can continue to use my wifi switches (without battery) with tuyam-qtt, and just use the smarthome/J binding tuya for my battery powered device. At least for the test…

Yes, you can. You can actually use both solutions for everything, they do not conflict with each other in my experience (If the battery operated sensors work with smarthome/J)

But to be sure : Is the Tuya Cloud access is necessary for each Openhab startup (electrical disconnection/reconnection of my Rpi for example) ?

Tuya Cloud connectivity is only necessary for initial discovery. In fact, it is not necessary at all. You could also configure everything manually. Tuya Cloud connectivity is Just more convenient. (With the common exceptions being to add a new device, and when a device gets reset to factory, like, for example, power outages).

Hope that helps.
Thanks

1 Like

Hello !

I have a problem with Tuya Cloud… The IoT Core trial has expired, so I can’t get my devices keys.
I asked for an extension… I am waiting for an answer.
But it’s annoying because as soon as the license expires, you can’t use tuya-cli wizard for example or retrieve the keys on the Tuya Cloud…

Hello @Roms ,
When i configured this, my trial was expired too, but i was still able to read the key and secret from the interface.

Just to be clear, to configure the smarthome/j binding, you do not need the device keys. You need the project key and secret.

You can get that from iot.tuya.com. On the left hand side, click on the Cloud Icon, and you will be presented with a list of your projects. If you click on the project you use for tuya-mqtt, you will be presented with a page that shows your access id and secret in it (Look under “Authorization Key”).

Hope this helps.
Thanks

Hi @ricardol !

When i configured this, my trial was expired too, but i was still able to read the key and secret from the interface.

How do you do that ? From Tuya Website ?
I know 2 methods to get the Id/key of a device:
-Use the API Explorer from Tuya Website and make a Device Informatio request : But it said me I haven’t the permission (trial expired)
-Use the tuya-cli wizard in Openhabian : But it don’t works anymore for now. I don’t know why but I guess it is because of my expired trial ?

Just to be clear, to configure the smarthome/j binding, you do not need the device keys. You need the project key and secret.

I have configured the ‘project’ thing with my project Id and Secret, it is online and seems to be fine.
In the documentation it says “The best way to configure a tuyaDevice is to use the discovery service”.
Ok, but how do you use the discovery service?
Nothing seems to discover automatically. When I click on Scan button, it doesn’t detect anything. So I tried to create the ‘tuyadevice’ thing manually…
I am just thinking about it now… but do I need to re-pair my devices with the app to start discovery service of the binding ?

Thanks for your help.

edit : My IoT core licence has been renewed for 6 Month just now.

Hello @Roms ,

How do you do that ? From Tuya Website ?
Correct. I get the access ID and secret for the project from iot.tuya.com

I have configured the ‘project’ thing with my project Id and Secret, it is online and seems to be fine.
In the documentation it says “The best way to configure a `tuyaDevice` is to use the discovery service”.
Ok, but how do you use the discovery service?

Yes. Once the Tuya cloud thing is online, you just scan for devices, and they show up.
3 Things come to mind:

  1. Make sure that the Data Center, app username, app password, country code and app type are correctly defined according to your specific scenario

  2. In iot.tuya.com, under cloud, got o “Link Tuya App account”. Is the account linked? If it is, make sure that under “Manage Devices”, you have given the account permissions to handle all the devices. Same Thing with “Link tuya App”. The easiest think to do is to choose “Automatic Link” as devices managment method, and to give “read, write and manage” permissions (I am actually not sure “Link Tuya App” is needed. This will let you link your custom apps, and unless you have devices in those customs apps, it won’t serve any purpose for discovery).

Thanks

Thank you for your response @ricardol

Yes you can get the Acess ID and Secret for the Project without needed the Trial Licence.
But, to get the Ip, Local Key, Product Id of a Device, do you need the Trial Licence ?
I know it is not necessary for this Tuya Binding, but I want to know if I can get them manually without the licence.

Yes all is ok for Data Center, app username (email ?), app password, country code and app type.
In tuya.com all is ok, I’ve just checked it : All my 3 devices are linked and have the 3 permissions and are in automatic link.

I will try to unpaired and paired the devices from the Tuya Smart App

@ricardol
I have just trying and it is works, my 3 devices are detected in Paper UI !

I don’t change anything in the setup. The only difference between today and yesterday : My Trial Licence has been renewed this morning…
That is not good if the Trial Licence is mandatory each time I have to add a device :frowning:
My assumption is the discovering service of the binding use the API Explorer of the Tuya Cloud to get IDs.
But to use the API Explorer you need the Tuya Trial Licence (I test it manually yesterday without the Licence, It said me Permission refused because of Trial expired)

@Roms

Yes you can get the Acess ID and Secret for the Project without needed the Trial Licence.
But, to get the Ip, Local Key, Product Id of a Device , do you need the Trial Licence ?
I know it is not necessary for this Tuya Binding, but I want to know if I can get them manually without the licence.

My Tuya iot account and setup is more than 2 years old. I never renewed or had to upgrade the license. I have added devices lately, and never needed a license either.
Additionally, i have had a lot of power outages, (which sometimes reset your devices), and due to this, i had to re-add several devices to my app, and regenerate the devices.json file for tuya-mqtt, because id and secrets change…i never had a problem doing it.

So, your battery operated devices work? Have you checked that they report properly, and at a periodic rate? Do they appear off line or online most of the time? (I wonder if they “sleep” during measurements, to save battery). If they work, i might be tempted to go ahead and by some door sensors for testing…

Thanks!

Ok I trust you ^^
So something is (was ?) wrong with my project or setup … I don’t know why it didn’t work before. I would try to do it all again with a new project and new tuya count without Trial Licence later.

I am not at home for now so I can’t move the sensor to “change” its temperature. At the moment it is offline. When the temperature changes, it will be online for 1 or 2 seconds. I hope this Tuya Binding will capture his data!

I will keep you informed !