Skip to content
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

set dimmer level #572

Open
vespino opened this issue Dec 21, 2024 · 15 comments
Open

set dimmer level #572

vespino opened this issue Dec 21, 2024 · 15 comments
Labels
tuya_device Support for specific Tuya Devices

Comments

@vespino
Copy link

vespino commented Dec 21, 2024

I'm looking into this script to control my Tuya devices locally. I'm not familiar with Python, but so far it's looking promising and I'm already able to control my RGBWW lightbulbs (on/off/level) and outlets (smartplugs). Continuing with the code used for my lightbulbs I was also able to switch on/off my dimmers, but I'm not able to control the level. The following turns on the device, but does nothing with the brightness level:

d = tinytuya.BulbDevice('x', 'y', 'z', version=3.3)
d.set_brightness_percentage(50)
d.turn_on()

I'm familiar with the Tuya Cloud API and from this I've been using bright_value_v1 to set the level of my dimmers and bright_value_v2 to set the level of my lightbulbs.

Am I on the right track? Or should I take a completely different approach?

@uzlonewolf
Copy link
Collaborator

It looks like the DPS that dimmer uses is slightly different than the DPS the bulbs use. Can you post the output of d.status()?

@jasonacox jasonacox added the tuya_device Support for specific Tuya Devices label Dec 22, 2024
@vespino
Copy link
Author

vespino commented Dec 22, 2024

Device status: {'dps': {'1': False, '2': 301, '4': 'LED', '6': 0}}

@vespino
Copy link
Author

vespino commented Dec 22, 2024

I think 301 is the brightness level, which translates to 30%. Other devices (bulbs) seem to allow for a level of 1-100%. Haven’t tried it, just typing out loud. Could be something completely different.

@uzlonewolf
Copy link
Collaborator

Possible, though I can't say I've seen the app use something besides 0 for the last digit when the range is 1.0 - 100.0. Can you use the app to set it to 0%, say 60%, and 100%, and call d.status() after each to see what it changes to?

@vespino
Copy link
Author

vespino commented Dec 22, 2024

No matter what I set as level (0,60,100), the last item (6) is always 0. The value of 2 does change accordingly. One thing I must add: an action on this dimmer takes its time to execute, while others simply fly. Strangely only with the on action, when turning it off it’s instant. The endgoal is to have it run locally mainly for not having to go outside my network, but speed is also a factor.

@jasonacox
Copy link
Owner

I would recommend running a monitoring loop (see https://github.com/jasonacox/tinytuya/blob/master/examples/monitor.py) and then change the dimmer levels to see what events get sent from the device.

@uzlonewolf
Copy link
Collaborator

Can you post the actual values from those different levels so I know what you're dealing with?

If you answered 'Y' to the "Download DP Name mapping?" question during the wizard there should be a hint as to what DP 6 does in the devices.json file.

@vespino
Copy link
Author

vespino commented Dec 23, 2024

I’m using tinytuya standalone and didn’t use the wizard as my devices are on a separate VLAN. The value I’m sending is “30”. Haven’t had time to run monitor.py. What I also haven’t tried is sending a value like 300 btw.

@vespino
Copy link
Author

vespino commented Dec 23, 2024

Does this help?

root@GP2-NAS01:/scripts# python3 monitor.py
 > Send Request for Status <
Initial Status: {'dps': {'1': False, '2': 299, '4': 'LED', '6': 0}}
 > Begin Monitor Loop <
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: None
Received Payload: {'dps': {'1': True}, 't': 1734978730}
Received Payload: {'dps': {'2': 293}, 't': 1734978732}
Received Payload: {'dps': {'2': 571}, 't': 1734978732}
Received Payload: {'dps': {'2': 625}, 't': 1734978732}
Received Payload: {'dps': {'2': 470}, 't': 1734978735}
Received Payload: {'dps': {'2': 299}, 't': 1734978735}
Received Payload: {'dps': {'2': 290}, 't': 1734978735}
Received Payload: {'dps': {'2': 484}, 't': 1734978736}
Received Payload: {'dps': {'2': 600}, 't': 1734978736}
Received Payload: {'dps': {'2': 671}, 't': 1734978736}
Received Payload: {'dps': {'2': 747}, 't': 1734978737}
Received Payload: {'dps': {'2': 804}, 't': 1734978737}
Received Payload: {'dps': {'2': 847}, 't': 1734978737}
Received Payload: {'dps': {'2': 886}, 't': 1734978738}
Received Payload: {'dps': {'2': 918}, 't': 1734978738}
Received Payload: {'dps': {'2': 940}, 't': 1734978738}
Received Payload: {'dps': {'2': 941}, 't': 1734978739}
Received Payload: {'dps': {'2': 740}, 't': 1734978740}
Received Payload: {'dps': {'2': 466}, 't': 1734978741}
Received Payload: {'dps': {'2': 406}, 't': 1734978741}
Received Payload: {'dps': {'2': 381}, 't': 1734978741}
Received Payload: {'dps': {'2': 356}, 't': 1734978742}
Received Payload: {'dps': {'2': 336}, 't': 1734978742}
Received Payload: {'dps': {'2': 322}, 't': 1734978742}
Received Payload: {'dps': {'2': 316}, 't': 1734978743}
Received Payload: {'dps': {'2': 309}, 't': 1734978743}
Received Payload: {'dps': {'2': 301}, 't': 1734978743}
Received Payload: {'dps': {'2': 300}, 't': 1734978744}
Received Payload: {'dps': {'1': False}, 't': 1734978746}
Received Payload: None
Received Payload: None

While the script was running, I set the brightness using the Tuya app which resulted in this output.

Meanwhile I have also tried setting the brightness to 500 using TinyTuya, this didn't change anything.

I also found out what was causing the delay mentioned ealier. I'm also setting "set_colourtemp_percentage" with every action to force the colour to the same for every device. Since this particular dimmer doesn't support this, this causes a delay as my turn_on action came after set_colourtemp_percentage. I have since changed this so turn_on comes first and level + set_colourtemp_percentage come second/third.

@jasonacox
Copy link
Owner

I would try some code like this to manage this devices:

import tinytuya
import time

# Connect to Device
d = tinytuya.OutletDevice(
    dev_id='DEVICE_ID_HERE',
    address='IP_ADDRESS_HERE',  
    local_key='LOCAL_KEY_HERE', 
    version=3.3)

# Get Status
data = d.status() 
print('set_status() result %r' % data)

# Turn On
d.turn_on()

# Read Value of DPS 2
data = d.status()  
print("Value of DPS 2 is ", data['dps']['2'])

# Change DPS 2 - go from 100 to 1000
for i in range(100,1000,100):
    print("Setting DPS 2 to ", i)
    # convert i to string
    d.set_value(2, str(i))
    time.sleep(1)

# Read Value of DPS 2
data = d.status()  
print("Value of DPS 2 is ", data['dps']['2'])

# Turn Off
d.turn_off()

@vespino
Copy link
Author

vespino commented Dec 24, 2024

This does seem to do the trick:

d.set_value(2, level)

Level needs to be a value from 10-1000. But is there a way to combine this in the project instead of having to add this only for this specific dimmer? Btw it’s a dimmer from MOES.

@uzlonewolf
Copy link
Collaborator

I started a rewrite of BulbDevice that would make it easy to remap the DPs for cases like this, but in my usual fashion I then got distracted and never finished it 🤣 Unfortunately the current BulbDevice doesn't have a way to override the DP. I'll see if I can get my rewrite finished and pushed within the next week or so.

@uzlonewolf
Copy link
Collaborator

I’m using tinytuya standalone and didn’t use the wizard as my devices are on a separate VLAN.

The Wizard is how you retrieve the local keys, it does not try to connect to devices unless you answer 'Y' to the "Poll Devices?" question. If you did not run it then how did you get your local keys?

@vespino
Copy link
Author

vespino commented Dec 24, 2024

I got the local keys from the Tuya developer login.

@vespino
Copy link
Author

vespino commented Dec 24, 2024

I started a rewrite of BulbDevice that would make it easy to remap the DPs for cases like this, but in my usual fashion I then got distracted and never finished it 🤣 Unfortunately the current BulbDevice doesn't have a way to override the DP. I'll see if I can get my rewrite finished and pushed within the next week or so.

Much appreciated! For now I’ve added an if statement to my script so I can go ahead and migrate all of my devices to local control instead of using the cloud api. My wife will be sooo happy. Oh and I’ve learned some Python so win-win 👌🏻🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tuya_device Support for specific Tuya Devices
Projects
None yet
Development

No branches or pull requests

3 participants