TheAgentK / tuya-mqtt

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

Setting switch state of a smart switch (custom template) #75

Closed psyciknz closed 2 years ago

psyciknz commented 2 years ago

Hoping someone can help. Just go my first tuya devices and are trying to get them going in openhab.

template: {
      state: {
        key: 1,
        type: 'bool'
      },
      power_state: {
        key: 1,
        type: 'bool'
      }
}

With the following partial template, for a smart switch with power monitoring (excluded the volts etc) I can get the volts fine (dp2/20).

But i can't get the power/state command to work to set the power on or off. I've left state as is and have tried:

sensors/tuya/kogan-02/dps/1/state 1
sensors/tuya/kogan-02/dps/1/state 0
sensors/tuya/kogan-02/DPS/1/state 0
sensors/tuya/kogan-02/dps/1/state false
sensors/tuya/kogan-02/dps/1/command 1
sensors/tuya/kogan-02/dps/1/command false
sensors/tuya/kogan-02/dps/1/command Off
sensors/tuya/kogan-02/dps/1/command OFF
sensors/tuya/kogan-02/dps/1/command off
sensors/tuya/kogan-02/state/command off
sensors/tuya/kogan-02/power/command OFF
sensors/tuya/kogan-02/power_state/command OFF

What am I doing wrong?

tsightler commented 2 years ago

This project is mostly unmaintained at this point, while you are welcome to use it, it's pretty dead around here so there's not much help.

Your template only needs a single entry per DPS key, no need for two, although for a simple switch you can use the predefined "SimpleSwitch" type and then you don't even need a template.

As to what you're doing wrong, you are adding /command to state topic, instead of replacing the word "state" with the word "command" in the existing topic, so with your above examples these should work:

sensors/tuya/kogan-02/command off
sensors/tuya/kogan-02/power_command OFF

Basically, every "state" topic creates a corresponding "command" topic automatically, so "power_state" will create a "power_command" topic for commands, but you instead tried "power_state/command".

psyciknz commented 2 years ago

Thanks, yeah I saw that. Disappointing, to see that it's gone maintenance only....there's doesn;t seem to be a similar replacement now. And given I'm not homeassistant with it's official integration, I'm still left to wanting an mqtt solution.

So I tried as you suggested:

2021-10-21T21:34:58.374Z tuya-mqtt:command Received MQTT message ->  {"topic":"sensors/tuya/kogan-02/command","message":"off"}
2021-10-21T21:34:58.375Z tuya-mqtt:error TypeError: Cannot read property 'processDpsCommand' of undefined
    at MqttClient.<anonymous> (/home/node/tuya-mqtt/tuya-mqtt.js:162:32)
    at MqttClient.emit (events.js:400:28)
    at MqttClient._handlePublish (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:1277:12)
    at MqttClient._handlePacket (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:410:12)
    at work (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:321:12)
    at Writable.writable._write (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:335:5)
    at doWrite (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at Writable.write (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:307:11)
    at Socket.ondata (internal/streams/readable.js:731:22)

neither kogan-02/command or kogon-02/power_command worked. REgarding using the smart switch, that might give me access to the switch component, but I did actually want the power readings off the device as well, which I have managed to get via DPS 18, 19 and 20.

tsightler commented 2 years ago

Did you remove the double entries in your template, I have no idea what defining two values pointing at the same DPS key will do, but it might break in the way your seeing.

Regarding, using a predefined device like SimpleSwitch does not keep you from using the DPS values, the predefined devices are just templates themselves, just ones already created. Direct access to the device DPS values is always available in any case.

Unfortunately I haven't used this code in what feels like forever, so I can barely remember what it takes to configure it, even though I wrote it.

psyciknz commented 2 years ago

Yeah just left with

template: {
      power: {
        key: 1,
        type: 'bool'
      },
      volts_state: {
        key: 20,
        type: 'float',
        topicMin: 1,
        topicMax: 3000,
        stateMath: '/10'
      },
      watts_state: {
        key: 19,
        type: 'float',
        topicMin: 1,
        topicMax: 3000,
        stateMath: '/10'
      },
      amps_state: {
        key: 18,
        type: 'float',
        topicMin: 1,
        topicMax: 3000
      }

And I still get the same error with: sensors/tuya/kogan-02/command false

2021-10-21T21:34:58.374Z tuya-mqtt:command Received MQTT message ->  {"topic":"sensors/tuya/kogan-02/command","message":"off"}
2021-10-21T21:34:58.375Z tuya-mqtt:error TypeError: Cannot read property 'processDpsCommand' of undefined
    at MqttClient.<anonymous> (/home/node/tuya-mqtt/tuya-mqtt.js:162:32)
    at MqttClient.emit (events.js:400:28)
    at MqttClient._handlePublish (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:1277:12)
    at MqttClient._handlePacket (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:410:12)
    at work (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:321:12)
    at Writable.writable._write (/home/node/tuya-mqtt/node_modules/mqtt/lib/client.js:335:5)
    at doWrite (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at Writable.write (/home/node/tuya-mqtt/node_modules/readable-stream/lib/_stream_writable.js:307:11)
    at Socket.ondata (internal/streams/readable.js:731:22)

Same with OFF, on etc. I did try adding in a smpleSwitch and template items, and that still didn't seem to work. Giving it's not really posting updates unless I go into the app, I think I'll end up trying to switch to tinytuya library and have to write a python library around it.

Thanks for your help

SLaweck commented 2 years ago

Hi I use that config:

  {
    name: 'dev name',
    id: 'xxx',
    key: 'xxx',
    template: {
      state: { key: 1, type: 'bool' },
      amperes: { key: 4, type: 'float', stateMath: '/1000' },
      watts: { key: 5, type: 'float', stateMath: '/10' },
      volts: { key: 6, type: 'float', stateMath: '/10' }
    }
  }

Commands to switch on/off are: tuya/dev_name/command ON tuya/dev_name/command OFF

Best regards SLaweck

tsightler commented 2 years ago

Yeah, after looking at the above error the reason the config @psyciknz is using doesn't work has nothing to do with the template, rather it is because it looks like there's an extra topic level to the topic config. This isn't supported because the code currently determines the type of command by parsing the number of topic levels so adding two topic levels for the prefix instead of a single one completely changes the logic. This was a limitation I was planning to remove in the future (it's easy to fix), but once I decided to abandon Tuya devices, and thus maintenance of this project, it fell by the wayside.

Removing the sensors/ topic level and just leaving it with tuya/ would likely resolve the issue.

psyciknz commented 2 years ago

Ahh right, so because I've changed the default from tuya to sensors/tuya is why it's broken?

tsightler commented 2 years ago

Well, that's at least the reason for the error you reported above. The processDpsCommand() function should only be called if you are sending commands to the raw dps JSON command topic but the current code is hard coded to count the topic levels and call the appropriate function (processCommand(), processDpbCommand(), or processDpsKeyCommand()) based on if there are 3, 4, or 5 topic levels. Because you've added an extra topic level in the configuration the command to friendly topics have 4 levels instead of 3, so the code calls processDpsCommand() instead of just processCommand() function.

I can't say you might not have other issues, but that's the issue with the error above. I didn't notice it in your output previously and you didn't provide your config or indicate that you were otherwise changing the default settings, and it's been quite a while since I've even thought about this code, so it didn't even cross my mind.

Basically, the current code supports replacing "tuya" with "sensors", but you can't add an entire topic level with a / between them.

psyciknz commented 2 years ago

That's worked. The other problem I may have had was uppercase Kogan in devices and lower case in mqtt message.

Thanks for all your help.

Do you know if there a command for asking for devices to post their states?

I did try the official home assistant integration, but all it offers if on off, no power monitoring, so it's worse that this wonderful bridge (which allows me to control it via home assistant, openHAB and homebridge)

tsightler commented 2 years ago

Do you know if there a command for asking for devices to post their states?

If you mean for example to refresh power monitoring state, unfortunately tuyapi did not have support for this at the time I stopped maintaining this project. It was somewhat on the radar at the time, and the plan was to do something like add an additional property to set how often you wanted to send the command to force a device to update the power monitoring. My understanding is the tuyapi now has this capability, but, being unmaintained, it was never added to this project.

As far as I know the official Tuya integration for HA only uses cloud control. Everyone that is serious about using Tuya devices with HA uses the localtuya integration, which does support power monitoring via local control.

I'm not opposed to PRs to implement power monitoring, but as I no longer have Tuya devices (or at least any that actually have Tuya firmware left on them) so I don't have a platform to develop and test it and, unfortunately from the perspective of this project, I just don't care about Tuya devices anymore which is why this project requires a new maintainer going forward but so far none have stepped up, so I just don't think there's really that much interest here.

tsightler commented 2 years ago

Closing this since the initial issue has been resolved.