-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help with device #94
Comments
Hi @Electrik-rich546456 ! Happy Holidays to you too... I have enjoyed some time off which has afforded me some time to invest in TinyTuya. I added more TuyaCloud API functions which may actually help. It won't find the local IP but it will help us discover the DPS ID numbers that you can use to control the device. import tinytuya
# Turn on Debug Mode (optional)
# tinytuya.set_debug(True)
# Connect to Tuya Cloud
# c = tinytuya.Cloud() # This will use the tinytuya.json file created by the wizard or you can specify:
c = tinytuya.Cloud(
apiRegion="us",
apiKey="xxxxxxxxxxxxxxxxxxxx",
apiSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
apiDeviceID="xxxxxxxxxxxxxxxxxxID")
# Display list of devices
devices = c.getdevices()
print("Device List: %r" % devices)
# Select a Device ID to Test
id = "xxxxxxxxxxxxxxxxxxID"
# Display Properties of Device
result = c.getproperties(id)
print("Properties of device:\n", result)
# Display Functions of Device
result = c.getfunctions(id)
print("Functions of device:\n", result)
# Display DPS IDs of Device
result = c.getdps(id)
print("DPS IDs of device:\n", result)
# Display Status of Device
result = c.getstatus(id)
print("Status of device:\n", result)
# Send Command - This example assumes a basic switch
commands = {
'commands': [{
'code': 'switch_1',
'value': True
}, {
'code': 'countdown_1',
'value': 0
}]
}
print("Sending command...")
result = c.sendcommand(id,commands)
print("Results:\n", result) |
Hi there I tried the code using
|
Oh dear my bad how is the pip3 install version different to the github version ? When does it get updated ? |
This is the output of my thermostat.
How do just get the temperature as a human readable data (eg 22 `C ) ? Also seeing as we are getting the info via the tuya api, It has one button on it that seems to have two functions short press makes the thing update the api with current values. Long press resets it so the smartlife app needs to re find it. I got myself this unit as I thought it would let me get data from it via serial but all i got was "S2" and S3" as an output, tried lots of different ways of connecting to it to the extent I thought I had broken it at one point but its still working It doesn't seem to want to update itself when i start the smart life app so was thinking of flashing something else like 'tasmota' but have no idea as cant find any data about the hardware and also wanted to find a way to take a backup of existing firmware just in case i broke it but have not had any success. |
Hi @Electrik-rich546456, you are right, Human Readable Data?Here is what you would do to process the data into a human readable form (based on the units and scale listed in your output): # Grab Status of Device
response = c.getstatus(id)
# Parse Response
status = {}
for item in response['result']:
status[item['code']] = item['value']
# Print Values
print("Temperature = %0.2f °C" % (status['va_temperature']/10))
print("Humidity = %d %%" % status['va_humidity'])
print("Battery = %d %%" % status['battery_percentage']) Force Update via API?The function list that you pulled back from TuyaCloud indicates that it doesn't have any command functions we can call to update it:
Here is what I believe is happening: Most battery powered Tuya devices do not stay online. They "wake up" every so often, connect to the WiFi and update the TuyaCloud with their data. Your "button press" probably forces this. This is also why you do not see the device on the network when you do a Flash Device to Tasmota?That's an interesting option. I have never done this with any device, but I suspect it will be much harder to do with a battery powered device for the reasons I stated above. If you do manage to get to work, you will want an external power source for it or you will drain the batteries quickly. |
Happy new year @jasonacox in advance :-) I made these changes to your original code
it works nicely my output is this so i thought but then it did this error
I wonder how i can add your suggestion into my code ? But it may be updating enough in the loop but I think the error i got was due to the api kicking me off. I was also looking at different ttinyuya functions that could possibly be of use like |
The example code I gave doesn't have any error handling. Can you add this and try again? tinytuya.set_debug(True) Without seeing the debug info, I don't know what the Cloud API sent back, but there is the possibility that it will sometimes deliver a bad response (the devices do this too) or it may indicate that the token has expired. You can also add some basic error handling: while True:
try:
result = c.getstatus(id)
if "result" in result:
for item in result["result"]:
if item["code"] == 'va_temperature':
unit = (item['value']/10) # tuya stores temps as an integer (10x the real value)
print("temperature: %0.1f" % unit)
if unit >= 20.0:
print("hot")
elif unit <= 19.0:
print("cold")
except:
print("error - ignoring")
time.sleep(120) The |
Happy new year !!:-)@ Is there a way to work out how often the Cloud API gets updated from local device without the button being pressed. Maybe a timer to lo look for device update ? |
Do you know what IP address it uses when it comes online to send the update? If so, set up a constant ping and see how often it responds. If you don't have the IP, you can look at your router (if it has the ability to show DHCP assignments) or you could run Happy 2022!! :) |
Hi, Yes I found the IP address by cross referencing with the mac address found on smartlife app.
But its very strange as the output is this ....
I'm not sure why at 10pm ish it starts to be so active unless it comes online every time a change is detected by the thermostat. What do you think? Happy 2022!! :) |
I made these changes I'm going to leave it running over night to see the results.
|
Looks good! It will be interesting to see what happens. I suspect it is sending updates when the temperature values change (at least some %). |
This is the output :-(
Why is my token invalid, its fine if i start the script again? |
The token expired. I need to add some code into the library to detect that and refresh the token. Good catch. |
I just published v1.3.1 - see if this works. I don't how long the tokens last so I created an test by artificially changing the token to get the 'invalid token' error from Tuya. Hopefully this will capture the real expiration as well. Thanks for helping test these new cloud functions.
|
Would putting something like |
Also would putting |
Yes, you could but I added code to the library to re-authenticate with TuyaCloud when the token dies. Woud you be able to test it for me?
After upgrading to v 1.3.1, re-run your script to see if that fixes the issue. |
I updated timytuya and left the code running all night this is the output
|
This is great! There are 3 examples of the token expiring and tinytuya renewing it. Thanks for testing! Let me know if you see anything wrong.
|
I know its cool its working ! You are welcome I enjoyed testing. How do I pull in the use of a wifi plug by name to use instead of the print cold or hot statements?? |
Good questions. The "expire_time" value is set by the TuyaCloud (that is the response). In your example, that seems to hold true as it requires a new token every 2 hours. Tuya is setting that value so it likely isn't adjustable, but with the token renewal logic, it shouldn't matter. If you want to take local action on your Tuya devices, you just add the OutletDevice code: # At the start of your code
radiator = tinytuya.OutletDevice(DEVICEID, DEVICEIP, DEVICEKEY)
d.set_version(3.3)
# Add to section where you want to turn on/off
radiator.turn_off()
radiator.turn_on() |
Can't I use |
I think I misunderstood your request. If you want to turn it off via the Cloud API, you will need to do something like this instead: # Send Command - Turn on switch
commands = {
'commands': [{
'code': 'switch_1',
'value': True
}, {
'code': 'countdown_1',
'value': 0
}]
}
print("Sending turn on command...")
c.sendcommand(DeviceID,commands) |
Hi hows it going ?
|
My code that running
|
Are you able to ping the device from the PI? The error indicates it is not reachable from the PI:
|
Well no it seems to be pot pingable but if i change to tinytuya.BulbDevice instead of tinytuya.OutletDevice it works !! What's the difference ? |
BulbDevice class is similar to OutletDevice (both use the root Device class) but adds bulb attributes (type, brightness, color) and related functions for controlling smart bulbs. I don't know how it would make a difference in you case. Very interesting. |
Hi i think it was just a strange ghost in the code... as it stopped working due to the it not having an ip address anymore so i just used the new cloud function instead. |
Hi @jasonacox I seem to have a new problem.. Oh yeah and that |
I must be missing some context. Are you running the script from a web server? |
Hi @jasonacox Yeah it's running on my RPI 2b along side pi.hole.
I've created a PHP page with a load of buttons on it each one runs a little
Py script when clicked. They all work except the one using the new
tuya.cloud function.
I suspect it's some strange path of modules thing. 😞
…On Thu, 20 Jan 2022, 2:30 am Jason Cox, ***@***.***> wrote:
I must be missing some context. Are you running the script from a web
server?
—
Reply to this email directly, view it on GitHub
<#94 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN3GUW4SWVHZDASWKSQX7MDUW5XTFANCNFSM5KVX2BCA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Ah, I see. If you are using this line: # c = tinytuya.Cloud() # uses tinytuya.json
c = tinytuya.Cloud(
apiRegion="us",
apiKey="xxxxxxxxxxxxxxxxxxxx",
apiSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
apiDeviceID="xxxxxxxxxxxxxxxxxxID") |
Hi I need a bit of help with a new thermostat device. I cannot get any dps data from it as it seems to not have an ip address according to the debug info
or
Status {'Error': 'Network Error: Device Unreachable', 'Err': '905', 'Payload': None}
I've tried with
device22
option.I think this device is faulty as even the smart life app cannot do a "check device network"
I can see its mac address on smart life app and have found it on my router.
the 'tuya-raw.json' file reports data like ...
Any ways as always I look forward to fixing it :-)
P.s
Happy holidays @jasonacox
The text was updated successfully, but these errors were encountered: