vinodsr / node-red-contrib-tuya-smart-device

a node-red module for managing tuya smart devices
https://flows.nodered.org/node/node-red-contrib-tuya-smart-device
MIT License
43 stars 15 forks source link

New COMMAND to handle a verbose device. #96

Closed msillano closed 1 year ago

msillano commented 1 year ago

The imagination of the designers of Tuya devices never ceases to amaze me.

The device I am using now, a (DIN Power Meter OPWTY-63) presents a strange behavior, never met before. All DPS are more or less normal, except DP 6, that sends all RT data (V ,A, W, I_leak) in an encoded structure. DP 6 is only PUSHED in case of data change, as [event:dp-refresh], (all others sent as [event:data]) often at full speed that is one message per second. Any REFRESH command is ignored. The DP 6 cannot be read ! A SCHEMA returns all DPS, except DP 6 ! There is a DP called 'mute mode', but I don't know its purpose: it certainly has nothing to do with data throughput. 1 message per second from one device (and I plan to use two) is too much !. An update every 60-90 seconds is enough for my application (basically a data logger).

I have to slow down this flood of messages, and the best solution I have found is to disable shouldSubscribeRefreshData at intervals. I need a COMMAND that allows the user to modify the 'Listen to tuya events' parameter in real time. The algorithm I thought of is the following: In Polling, every 60-90 seconds:

  1. shouldSubscribeRefreshData = true
  2. I await the receipt of one or two messages from the DP 6
  3. shouldSubscribeRefreshData= false

The code required in tuya-smart-device.js is simple, now I'm testing it:

          case 'CONTROL':
             ... here existing commands .....
            } else if (msg.payload.action == 'SET_DATA_EVENT'){
          shouldSubscribeData = true;
                  shouldSubscribeRefreshData = true;
         if (msg.payload.value === 'event-data') 
                     shouldSubscribeRefreshData = false;
             if (msg.payload.value === 'event-dp-refresh') 
                    shouldSubscribeData = false;
                node.log(  `Event subscription : shouldSubscribeData=>${shouldSubscribeData} , shouldSubscribeRefreshData=>${shouldSubscribeRefreshData}`
           )

note: this works only if the device is configured 'both' as default.

I don't know if this feature is worth considering, but it's the only solution I've found: it might be useful to other users. Best regards m.s.

update: 2 more changes in callbacks:

              //  if (shouldSubscribeRefreshData) {      // deplaced 1 line down
        tuyaDevice.on('dp-refresh', (data) => {
      if (shouldSubscribeRefreshData) {          // here
                    node.log(....
              .....more.....
              }                                                      //  here
         });
       //  }                                                          // deplaced up

same:

        //  if (shouldSubscribeData) {             // deplaced 1 line down
  tuyaDevice.on('data', (data) => {
     if (shouldSubscribeData) {                     /  /here
             node.log(... 
         ...more...
              }                                                      //  here
         });
       //  }                                                          // deplaced up

LOG:

              27 Aug 06:41:23 - [info] [tuya-smart-device:Breaker DIN] Recieved input : {"payload":{"operation":"GET","schema":true},"topic":"Fast cmd for DIN power meter","_msgid":"cc952072d0d3641b"}
              27 Aug 06:41:23 - [info] [tuya-smart-device:Breaker DIN] Data from device  [event:data]: {"dps":{"1":470,"9":0,"11":false,"12":false,"13":10020,"16":true,"19":"FSE-F723C51D0A720B","101":272,"102":172,"103":45,"104":35,"105":false,"106":false}}

              27 Aug 06:41:24 - [info] [tuya-smart-device:Breaker DIN] Recieved input : {"payload":{"operation":"CONTROL","action":"SET_DATA_EVENT","value":"alldata"},"_msgid":"7b2876be8697b1b5","_event":"node:3a1df4f8.435cec"}
              27 Aug 06:41:24 - [info] [tuya-smart-device:Breaker DIN] Event subscription : shouldSubscribeData=>true , shouldSubscribeRefreshData=>true
              27 Aug 06:41:28 - [info] [tuya-smart-device:Breaker DIN] Data from device  [event:dp-refresh]: {"dps":{"6":"CNQAAGQAAAEAAA=="},"t":1661575288}
              27 Aug 06:41:28 - [info] [tuya-smart-device:Breaker DIN] Recieved input : {"payload":{"operation":"CONTROL","action":"SET_DATA_EVENT","value":"event-data"},"_msgid":"ab57eefcaba5120f","_event":"node:3a1df4f8.435cec"}
              27 Aug 06:41:28 - [info] [tuya-smart-device:Breaker DIN] Event subscription : shouldSubscribeData=>true , shouldSubscribeRefreshData=>false
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 1 year ago

Auto closing the issue