TheAgentK / tuya-mqtt

Nodejs-Script to combine tuyaapi and openhab via mqtt
MIT License
173 stars 81 forks source link

get-states not fully using templates in devices.conf #47

Closed thomasleitner closed 3 years ago

thomasleitner commented 3 years ago

I've tried to use the "get-states" command to get all DPS from a device. In addition, I have a template defined in devices.conf. Now it seems as the template is not fully used in this case.

Here's the devices.conf file:

[
  {
    name: 'Aiibot Air Purifier',
    id: 'xxxxxxxxxxxxxxxxx',
    key: 'yyyyyyyyyyyyyyy',
    ip: '10.10.1.42',
    version: '3.3',
    type: 'GenericDevice',
        template: {
            power_state: {
              key: '1',
              type: 'bool'
            },
            pm25_state: {
              key: '2',
              type: 'int'
            },
            mode_state: {
              key: '3',
              type: 'string'
            },
            speed_state: {
              key: '4',
              type: 'string'
            }
        }
  }
]

and here's what happens when I issue the get-states command:

tuya/aiibot_air_purifier/command get-states
tuya/aiibot_air_purifier/power_state ON
tuya/aiibot_air_purifier/pm25_state 8
tuya/aiibot_air_purifier/dps/state {"1":true,"2":8,"3":"auto","4":"1","7":false,"19":"0","22":"1"}
tuya/aiibot_air_purifier/dps/1/state true
tuya/aiibot_air_purifier/dps/2/state 8
tuya/aiibot_air_purifier/dps/3/state auto
tuya/aiibot_air_purifier/dps/4/state 1
tuya/aiibot_air_purifier/dps/7/state false
tuya/aiibot_air_purifier/dps/19/state 0
tuya/aiibot_air_purifier/dps/22/state 1

I would have expected to get the topics:

tuya/aiibot_air_purifier/power_state
tuya/aiibot_air_purifier/pm25_state
tuya/aiibot_air_purifier/mode_state  
tuya/aiibot_air_purifier/speed_state

Instead I only get values for the first two (power_state and pm25_state) and mode_state and speed_state are missing.

tsightler commented 3 years ago

I think this is actually a documentation error on my part. In the docs it says to use type: 'string', but it should be type: 'str'. I think this is causing the friendly topic publish function to end up with a null value for those two keys since it doesn't have a matching type. Could you change those two lines in your template for keys 3 & 4 so it uses 'str' instead of 'string' and try again? I'll go fix the docs.

thomasleitner commented 3 years ago

Yes, this works. Now I get:

tuya/aiibot_air_purifier/command get-states
tuya/aiibot_air_purifier/power_state ON
tuya/aiibot_air_purifier/pm25_state 9
tuya/aiibot_air_purifier/mode_state auto
tuya/aiibot_air_purifier/speed_state 1
tuya/aiibot_air_purifier/dps/state {"1":true,"2":9,"3":"auto","4":"1","7":false,"19":"0","22":"1"}
tuya/aiibot_air_purifier/dps/1/state true
tuya/aiibot_air_purifier/dps/2/state 9
tuya/aiibot_air_purifier/dps/3/state auto
tuya/aiibot_air_purifier/dps/4/state 1
tuya/aiibot_air_purifier/dps/7/state false
tuya/aiibot_air_purifier/dps/19/state 0
tuya/aiibot_air_purifier/dps/22/state 1

Thanks a lot!

tsightler commented 3 years ago

Good to know it works. I've updated the docs so I'm going to go ahead and close this for now.

I'm realizing that I can probably just automatically detect the type for the state and DPS key topics since I know if it's a bool, number or string based on the JSON data type. I'll try to implement some automatic handling of this in the future to make this setup even easier.