Tuya LOCAL with energy monitoring and without tuya-convert

3. Create flow and publish tuya socket to HomeAssistant
Prerequisete is that you have MQTT working in HomeAssistant in order to publish the data to HA

Install this flow:

[{"id":"b40f19b3.81c0e8","type":"change","z":"d98cf64c.d7fed8","name":"set tuya_devices","rules":[{"t":"set","p":"tuya_devices","pt":"flow","to":"(\t   $s1          := \"switch_1\";\t   $s2          := \"switch_2\";\t   [\t       {\t           \"devId\"      : \"82180808c44f33a6743a\",\t           \"ip\"         : \"10.10.4.81\",\t           \"name\"       : $s1\t       },\t       {\t           \"devId\"      : \"82180808c44f3387a0fe\",\t           \"ip\"         : \"10.10.4.82\",\t           \"name\"       : $s2\t       }\t   ]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":160,"wires":[[]]},{"id":"546e596.a25eea8","type":"inject","z":"d98cf64c.d7fed8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":160,"wires":[["b40f19b3.81c0e8"]]},{"id":"bf85621d.da07d","type":"inject","z":"d98cf64c.d7fed8","name":"","topic":"","payload":"tuya_devices","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":240,"wires":[["5795380.6f9a1c8"]]},{"id":"5795380.6f9a1c8","type":"split","z":"d98cf64c.d7fed8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":290,"y":240,"wires":[["c1f88cc8.ad8e"]]},{"id":"c1f88cc8.ad8e","type":"change","z":"d98cf64c.d7fed8","name":"set mqtt devices","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t$topic_prefix := \"tuya\";\t$dev_name := payload.name;\t   {\t       \"platform\":\"mqtt\",\t       \"name\": $dev_name,\t       \"state_topic\":$topic_prefix & \"/\" & $dev_name & \"/status\",\t       \"value_template\": \"{{ value_json.state }}\",\t       \"command_topic\":$topic_prefix & \"/\" & $dev_name & \"/set\",\t       \"availability_topic\":$topic_prefix & \"/\" & $dev_name & \"/available\",\t       \"payload_available\":\"online\",\t       \"payload_not_available\":\"offline\",\t       \"json_attributes_topic\": $topic_prefix & \"/\" & $dev_name & \"/status\",\t       \"json_attributes_template\": \"{{ value_json.attributes | tojson }}\",\t       \"payload_on\":\"ON\",\t       \"payload_off\":\"OFF\",\t       \"optimistic\":false,\t       \"qos\":0,\t       \"retain\":false\t   }\t)","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"\"homeassistant/switch/\" & payload.name & \"/config\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":240,"wires":[["879a9a27.016f58"]]},{"id":"879a9a27.016f58","type":"mqtt out","z":"d98cf64c.d7fed8","name":"","topic":"","qos":"","retain":"","broker":"7b9a492a.76f2c8","x":650,"y":240,"wires":[]},{"id":"a2e39ae1.1c8008","type":"tuya-local","z":"d98cf64c.d7fed8","devName":"switch_1","devIp":"10.10.4.81","devId":"82180707c44f33a6743a","devKey":"df2ad2b927d1dac","protocolVer":"3.3","renameSchema":"","filterCB":"","x":660,"y":340,"wires":[["82d03748.37a7c8"]]},{"id":"743700a3.27eee","type":"mqtt in","z":"d98cf64c.d7fed8","name":"","topic":"tuya/+/set","qos":"2","datatype":"auto","broker":"7b9a492a.76f2c8","x":160,"y":380,"wires":[["74d7a3e9.b626cc"]]},{"id":"87ac07d9.edda68","type":"change","z":"d98cf64c.d7fed8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.available ? \"online\" : \"offline\"","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"\"tuya/\" & data.name & \"/available\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":400,"wires":[["7ee9b2f.05c694c"]]},{"id":"7ee9b2f.05c694c","type":"mqtt out","z":"d98cf64c.d7fed8","name":"","topic":"","qos":"","retain":"","broker":"7b9a492a.76f2c8","x":1190,"y":360,"wires":[]},{"id":"74d7a3e9.b626cc","type":"change","z":"d98cf64c.d7fed8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t$new_state := (payload = \"ON\") ? true : false; \t{ \"set\" : $new_state, \"dps\": 1 }\t)","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"(\t$name:= $split(topic, \"/\")[1];\t   $tuya:= [\t       {\t           \"devId\":\"82180707c44f33a6743a\",\t           \"ip\":\"10.10.4.81\",\t           \"localKey\":\"acb2ad7b927d1dac\",\t           \"name\":\"switch_1\",\t           \"state_t\":\"medion/switch_1\",\t           \"cmd_t\":\"medion/switch_1/set\",\t           \"attr_t\":\"medion/switch_1/attributes\",\t           \"avai_t\":\"medion/switch_1/available\",\t           \"entity_id\":\"switch.medion_switch_1_mqtt\"\t       },\t       {\t           \"devId\":\"82180707c44f3387a0fe\",\t           \"ip\":\"10.10.4.82\",\t           \"localKey\":\"58c3558e3ca45c9e\",\t           \"name\":\"switch_2\",\t           \"state_t\":\"medion/switch_2\",\t           \"cmd_t\":\"medion/switch_2/set\",\t           \"attr_t\":\"medion/switch_2/attributes\",\t           \"avai_t\":\"medion/switch_2/available\",\t           \"entity_id\":\"switch.medion_switch_2_mqtt\"\t       }\t   ];\t$device := $filter($tuya, function($x) {\t    $x.name = $name\t});\t$device.ip\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":380,"wires":[["27fe737.1f92e8c"]]},{"id":"27fe737.1f92e8c","type":"switch","z":"d98cf64c.d7fed8","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"tuya_devices.0.ip","vt":"flow"},{"t":"eq","v":"tuya_devices.1.ip","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":470,"y":380,"wires":[["a2e39ae1.1c8008"],["4b43fc85.5c39a4"]]},{"id":"4d1e51e4.123a","type":"function","z":"d98cf64c.d7fed8","name":"","func":"const dps =  msg.payload.dps;\nlet state = null;\nlet power = null;\nlet voltage = null;\nlet current = null;\nif (\"1\" in dps) {\n    state = (dps['1']) ? \"ON\" : \"OFF\";\n}\n\nmsg.payload = {\n    state: state,\n    attributes: {power: \"0 W\",current:\"0 mA\",voltage:\"0 V\"}\n};\n\nif (\"19\" in dps) {\n    msg.payload.attributes.power = (dps['19']/10).toString() + \" W\";\n}\n\nif (\"20\" in dps) {\n    msg.payload.attributes.voltage = (dps['20'] /10).toString() + \" V\";\n}\n\nif (\"18\" in dps) {\n    msg.payload.attributes.current = (dps['18']).toString() + \" mA\";\n}\n\n\n\nif (msg.payload.state === null) {\n    delete msg.payload.state;\n}\n\nmsg.topic = \"tuya/\" + msg.data.name + \"/status\"\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":360,"wires":[["7ee9b2f.05c694c"]]},{"id":"3a1376d8.ad1c3a","type":"comment","z":"d98cf64c.d7fed8","name":"set device information","info":"","x":160,"y":100,"wires":[]},{"id":"ab82d464.f622c8","type":"comment","z":"d98cf64c.d7fed8","name":"set homeassistant discovery","info":"","x":180,"y":200,"wires":[]},{"id":"16e068de.061e07","type":"comment","z":"d98cf64c.d7fed8","name":"control-status","info":"","x":130,"y":320,"wires":[]},{"id":"4b43fc85.5c39a4","type":"tuya-local","z":"d98cf64c.d7fed8","devName":"switch_2","devIp":"10.10.4.82","devId":"82180707c44f3387a0fe","devKey":"58c2568e3ca45c9e","protocolVer":"3.3","renameSchema":"","filterCB":"","x":660,"y":420,"wires":[["82d03748.37a7c8"]]},{"id":"82d03748.37a7c8","type":"function","z":"d98cf64c.d7fed8","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":800,"y":380,"wires":[["4d1e51e4.123a"],["87ac07d9.edda68"]]},{"id":"7b9a492a.76f2c8","type":"mqtt-broker","z":"","name":"hass","broker":"10.10.0.152","port":"1883","clientid":"nr-docker","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

And configure it according to your data. Do not forget to configure MQTT IN and MQTT OUT with your MQTT settings (and password)
(I will add more detailed instructions next time)

4. Create sensors and switches
Put the following data into you HomeAssistant configuration.yaml and adjust it according to your data

switch:
  - platform: mqtt
    name: "Plug1"
    state_topic: "devices/tuya/plug1"
    value_template: "{{ value_json.dps['1'] }}"
    command_topic: "devices/tuya/plug1/trigger"
    payload_on: "true"
    payload_off: "false"
    state_on: "True"
    state_off: "False"
    optimistic: false
    qos: 0
    retain: true

sensor:
  - platform: mqtt
    name: "Plug1_Power"
    state_topic: "devices/tuya/plug1"
    unit_of_measurement: W
    value_template: "{{ value_json.dps['19'] | multiply(0.1) | float(2) | round (2) }}"
  - platform: mqtt
    name: "Plug1_Current"
    state_topic: "devices/tuya/plug1"
    unit_of_measurement: mA
    value_template: "{{ value_json.dps['18'] | float(2) | round(2)  }}"
  - platform: mqtt
    name: "Plug1_Voltage"
    state_topic: "devices/tuya/plug1"
    unit_of_measurement: V
    value_template: "{{ value_json.dps['20'] | multiply(0.1) | float(2) | round (2) }}"

Add sensors and switches to lovelace and use the created entities in your automations and flows!

This looks really useful as I want to get my tuya smartplug to give me power usage not just be able to turn off and on which works fine for me in HA.
However since I have a standard install of HA I am getting lost even at the first step. Could you get a bit more explicit for people unfamiliar with the underlying linux world. I can see the folder config/node-red/ in file editor but where do I go from here.?

sorry, I didn’t understand what exactly is your problem

@monaco to be honest I can’t remember. Stay safe.

How do you use your tuya switches now?

I have found better and way easier solution!

Just be warned, instructions in repository are a bit confusing, you can not just copy code, since there is some inconsistency! But you may copy mine:

switch:

- platform: localtuya
  host: 192.168.1.76
  local_key: 1234567891234567
  device_id: 12345678912345671234
  name: tuya_g4
  friendly_name: tuya_g4
  protocol_version: 3.3
  switches:
    sw01:
      name: tuya_g4
      friendly_name: G4 plug
      id: 1
      current: 4
      current_consumption: 5
      voltage: 6

sensor:

- platform: template
  sensors:
    tuya_g4_voltage:
      friendly_name: "G4 voltage"
      unit_of_measurement: 'V'
      value_template: "{{ states.switch.tuya_g4.attributes.voltage }}"
      
    tuya_g4_current:
      friendly_name: "G4 current"
      unit_of_measurement: 'mA'
      value_template: "{{ states.switch.tuya_g4.attributes.current }}"
      
    tuya_g4_current_consumption:
      friendly_name: "G4 current consumption"
      unit_of_measurement: 'W'
      value_template: "{{ states.switch.tuya_g4.attributes.current_consumption }}"

You may change all “friendly_name” but when you are changing “tuya_g4” under sw01, make sure that you will change also all instances for switch! I had some issues getting sensor data since in the instructions on repository for switch is name “tuya_01”, but in sensor was once “tuya-sw01” and in value template “sw01”.

11 Likes

@monaco I played with switches but really use tuya for a underfloor heating thermostat. The cloud based tuya integration has limited functionality and I wanted more. In particular the device a BHT-002 has an underfloor temp sensor and a room temp sensor. The underfloor one is not available in the tuya interface - only in the phone app (eg SmartLife).

I have combination. Some of my Tuya sockets are tasmotized and some of them are using the local tuya from my last update. Actually I do no see much difference between both options. Tasmotized send a bit more data, but on the other hand I can use stock ones also without running HA.

But anyway if someone will decide to flash Tasmota, I really suggest this tutorial https://www.digiblur.com/2020/01/tuya-convert-23-update-flash-tuya.html The whole procedure is very easy!

1 Like

I just bought my first Tuya flashed device (smart switch with energy monitoring). I read this thread and followed your advice to use the tutorial.

I must say for me at least this was not easy and took me around 8 hours to get through it start to finish with all the problems in between.

All of my problems came from the tuya-convert not downloading all the files it was supposed to but not realizing this and getting so far along and running into errors then googleing the errors and then going back and forth.

Eventually I worked out that all I had to do was delete the tuya-convert folder and redownload 3 times until there was no errors during the downloading and unpacking. Then errors stopped randomly appearing and I all the way to the end where I now have a Tuya smart plug with Tasmota installed.

I’ve got mine working in that I can switch it on and off but the power values are just showing V, mA and W.

I think I skipped these steps below though. When I get home I’ll try and fix that part up and hopefully it will work. Tuya convert doesn’t work on the plugs I have and opening them up to flash is a pain Asni don’t have the security screwdriver bit it needs. A really strange trip groove one.

  1. Identify on your Home-Assistant logs (putting your logging into debug mode), the different attributes you want to handle by HA.
  2. Find in the switch.py file that part, and edit it for ID/DPS that is correct for your device.

If you have tuya-convert, I think you have selected the wrong template in Tasmota. They are really different even the plugs look the same. Just get the propper one from https://templates.blakadder.com/

I’m not using tasmota, I have the original firmware and am using the localtuya custom component you linked above.

I’m just at the step where i need to set the DPS but I can’t make sense of it.

    @property
    def device_state_attributes(self):
        attrs = {}
        try:
            attrs[ATTR_CURRENT] = "{}".format(self._status['dps'][self._attr_current])
            attrs[ATTR_CURRENT_CONSUMPTION] = "{}".format(self._status['dps'][self._attr_consumption]/10)
            attrs[ATTR_VOLTAGE] = "{}".format(self._status['dps'][self._attr_voltage]/10)
#            print('attrs[ATTR_CURRENT]: [{}]'.format(attrs[ATTR_CURRENT]))
#            print('attrs[ATTR_CURRENT_CONSUMPTION]: [{}]'.format(attrs[ATTR_CURRENT_CONSUMPTION]))
#            print('attrs[ATTR_VOLTAGE]: [{}]'.format(attrs[ATTR_VOLTAGE]))

The instructions say “Find in the switch.py file that part, and edit it for ID/DPS that is correct for your device.”
In my logs I found what looks to be the DPS values for my smart plug but not sure how to enter it. In my logs I have

DEBUG (SyncWorker_13) [custom_components.localtuya.pytuya] decrypted result='{"devId":"XXXXXXXXXXXXXXXXXXXXXXXXXX","dps":{"1":true,"9":0,"18":0,"19":0,"20":2451,"21":1,"22":604,"23":29911,"24":17310,"25":1200}}'

hopefully it makes a bit more sense to you than it does to me haha

This are your dps settings. Now you have to figure out which number is what.
I usualy compare values from tuya app.
Then you just replace bolded numbers with yours. I think you have
voltage: 20 but current and consumption depends what you have connected

switches:
sw01:
name: tuya_g4
friendly_name: G4 plug
id: 1
current: 4
current_consumption: 5
voltage: 6

Ok I followed your advice so it looks like it is 18, 19 and 20.

Just checking do I need to put these values in the switch.py file as well? Like this:

@property
    def device_state_attributes(self):
        attrs = {}
        try:
            attrs[ATTR_CURRENT] = "{}".format(self._status['dps']['18'][self._attr_current])
            attrs[ATTR_CURRENT_CONSUMPTION] = "{}".format(self._status['dps']['19'][self._attr_consumption]/10)
            attrs[ATTR_VOLTAGE] = "{}".format(self._status['dps']['20'][self._attr_voltage]/10)
#            print('attrs[ATTR_CURRENT]: [{}]'.format(attrs[ATTR_CURRENT]))
#            print('attrs[ATTR_CURRENT_CONSUMPTION]: [{}]'.format(attrs[ATTR_CURRENT_CONSUMPTION]))
#            print('attrs[ATTR_VOLTAGE]: [{}]'.format(attrs[ATTR_VOLTAGE]))

Or do I leave the switch.py file alone and just put the do’s values in the config.yaml like you explained above. On the git page it explains about modifying the switch.py which is why I’m now confused.

Really appreciate you taking the time to help me with this. It’s had me stumped for the best part of a week.

no, just leave switch.py alone.

I actually didn’t have even to look for logs or even open switch.py since I used first my initial proposal with nodered and got dps numbers there. So I can confirm you do not need to do anything else than just putting correct numbers to yaml.

Thanks mate put it back to normal and it’s now working with 18, 19, 20

1 Like

Hi.
How do i find my DPS settings?
And this: host IP ,local_key, device_id ?

I got in my yaml:
logger:
default: debug

But I cant find “DPS” in logs.
Thx.

I used android packet capture as described here.

Invalid config for [sensor.template]: [tuya_g4_voltage] is an invalid option for [sensor.template]. Check: sensor.template->tuya_g4_voltage. (See ?, line ?).
I keep getting this error!

https://paste.ubuntu.com/p/HgXbHK39Vs/
there is my code what am i doing wrong?

Paste the whole code you have for switch and sensor. I think you have an error in value.template. There should be the same as you have it as name on swihtch.