Tuya LOCAL with energy monitoring and without tuya-convert

Thanks for the replies and the great work in this integration. I’ll try it soon, cheers.

Great work! I no longer experience delays when switching my bulb, thanks!

1 Like

I love the new release!
Well done!

Hi Carlo,
I tried to migrate from a first version of your excellent custom components, but I was unable to integrate my Teckin SP22 sockets.

Here are the steps:
In the configuration.yaml I entered “localtuya:! Include localtuya.yaml” and in the file I entered this code:

#PRESA STUDIO (Smart Plug 1)

  • host: 192.168.x.x
    device_id: myID
    local_key: myLocalKey
    friendly_name: teckin_plug1
    protocol_version: “3.3”
    entities:
    • platform: switch
      friendly_name: Smart Plug 1
      id: 1
      current: 18 #optional
      current_consumption: 19 #optional
      voltage: 20 #optional

I also tried to manually enter the integration discovery values, but nothing.

The switches are not manageable and even if they are reported in the integration, they are “unavailable”.

In the log file I found these errors:

File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 283, in async_update_ha_state
self._async_write_ha_state ()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 323, in _async_write_ha_state
attr.update (self.device_state_attributes or {})
File “/config/custom_components/localtuya/switch.py”, line 83, in device_state_attributes
self.dps (self._config [CONF_CURRENT_CONSUMPTION]) / 10
TypeError: unsupported operand type (s) for /: ‘NoneType’ and ‘int’

This error, however, by entering the socket configuration directly in the configuration.yaml:

Logger: homeassistant.components.switch
Source: helpers / entity_platform.py: 121
Integration: Switch (documentation, issues)
First occurred: 02:01:10 (4 occurrences)
Last logged: 02:01:10
The localtuya platform for the switch integration does not support platform setup. Please remove it from your config.

I also restarted the raspberry, but nothing has changed.

Can I try anything else?

For the moment I have gone back to the very first version which works well. I also report the old code that works perfectly with the previous version of your custom components:

  • platform: localtuya
    host: 192.168.x.x
    device_id: myID
    local_key: myLocalKey
    scan_interval: 15
    name: teckin_plug1
    friendly_name: “teckin_plug1”
    icon: mdi: power-socket-de
    protocol_version: 3.3
    switches:
    sw01:
    name: smartplug1
    friendly_name: Smart Plug 1
    id: 1
    current: 18
    current_consumption: 19
    voltage: 20

Thanks!

Try to remove the “# Optional” comment, I would say… It should work with the Integration GUI configuration, though. Let us know…

You are using the old syntax for adding a switch somewhere that you probably forgot to remove (you should only use localtuya:).

Anyways, the TypeError you get suggests that your plug doesn’t support power monitoring. Or is using other datapoints for that. Does it support power monitoring? When using config flow however, it should not be possible to end up in this situation. Not sure if you tried that?

1 Like

Hi Carlo,
I try to remove the comment, but I don’t think that’s the problem. Even my friend @liuk4friends, in his configuration, left it and it works.

1 Like

Hi,
I have been using localtuya for a few months and it works (with the old version). I can see watts, volts, and mAh. I try to delete all references (packages) to the old version and then I try to install version 2.0 again, configuring only one switch to verify the possibility of managing it locally.

1 Like

It has to be something related to your configuration. Again, have you tried to configure it using the Integration config flow?

Hi Carlo,
yes, I tried to configure it manually through the integration flow, I had also reported it in my first post.
I also tried to name it differently, but nothing.
During configuration via integration, parameters 18, 19, 20 were shown. Once the configuration is completed, the sensor is created, it becomes “unavailable”.
Tonight I try to start from 0 by deleting the old packages.
I will communicate the result
Thanks again for the great job

Something suggests that the status updates are not propagating (or we are handling them) incorrectly. Can you enable some debug logs:

logger:
  logs:
    custom_components.localtuya: debug

Hi @rospogrigio @postlund,
I just tried to install the custom component 2.0 version again after uninstalling the previous version, the reference packages and checking that there are no more switch entities.

This is my configuration.yaml:
localtuya:

  • host: 192.168.x.x
    device_id: myID
    local_key: myLocalKey
    friendly_name: teckin_plug1
    protocol_version: “3.3”
    entities:
    • platform: switch
      friendly_name: smartplug1
      id: 1
      current: 18
      current_consumption: 19
      voltage: 20

These are the error messages that appear after trying to integrate the first switch:

Logger: custom_components.localtuya.common
Source: custom_components / localtuya / common.py: 89
Integration: LocalTuya integration (documentation)
First occurred: 00:21:06 (1 occurrences)
Last logged: 00:21:06
Failed to update status of device 192.168.x.x

Logger: custom_components.localtuya.common
Source: custom_components / localtuya / common.py: 214
Integration: LocalTuya integration (documentation)
First occurred: 00:21:28 (24 occurrences)
Last logged: 00:26:59

Entity switch.smartplug1 is requesting unknown DPS index 18
Entity switch.smartplug1 is requesting unknown DPS index 19

Logger: homeassistant
Source: custom_components / localtuya / switch.py: 83
First occurred: 00:22:01 (12 occurrences)
Last logged: 00:26:59
Error doing job: Task exception was never retrieved

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 283, in async_update_ha_state
self._async_write_ha_state ()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 323, in _async_write_ha_state
attr.update (self.device_state_attributes or {})
File “/config/custom_components/localtuya/switch.py”, line 83, in device_state_attributes
self.dps (self._config [CONF_CURRENT_CONSUMPTION]) / 10
TypeError: unsupported operand type (s) for /: ‘NoneType’ and ‘int’

Restarting Home Assistant for the second time, I no longer have the error on the IP address (Failed to update status of device 192.168.x.x), but the other errors remain and the switch is still in the “unavailable” state

I also tried to integrate the switch with the integration config flow, but I had the same problem as the device unavailable

Should further information be needed, I will be happy to provide it.
In the meantime, I install the working version of the custom component :slight_smile:

PS: Having the Teckin SP22, I remember that to adapt your first version of the custom component, you advised me to make this change in pytuya / __ init__.py:

json_data [‘dps’] = {“1”: None, “101”: None, “102”: None}
to
json_data [‘dps’] = {“1”: None, “18”: None, “19”: None, “20”: None, “101”: None, “102”: None}

Right, now I believe I know what the problem is. @rospogrigio Likely a type 0d device abs we are not requesting platform specific DPS?

Probably. @SmartM-ui , can you try to configure voltage/current as sensors instead of attributes and see if it fixes? Instructions are in the Readme, let us know…
Edit: yes @postlund, I believe we add_dps_to_request only for the main DP and not for the attributes’ ones.

1 Like

Hi,
yesterday I only configured the switch without the sensor monitor because the switch status was already unavailable.

In the custom component (v.1) I configured the sensors for monitoring in this way and sensors monitoring work:

PRESA STUDIO

  • platform: template
    sensors:
    smartplug1_voltage:
    friendly_name: “Smart Plug 1 voltage”
    unit_of_measurement: ‘V’
    value_template: “{{states.switch.smartplug1.attributes.voltage}}”

    smartplug1_current:
    friendly_name: “Smart Plug 1 current”
    unit_of_measurement: ‘mA’
    value_template: “{{states.switch.smartplug1.attributes.current}}”

    smartplug1_current_consumption:
    friendly_name: “Smart Plug 1 current consumption”
    unit_of_measurement: ‘W’
    value_template: “{{states.switch.smartplug1.attributes.current_consumption}}”

OK then you have 2 choices:

  1. change your config to something like:
[...]
    entities:
      - platform: switch
        friendly_name: smartplug1
        id: 1
      - platform: sensor
        friendly_name: smartplug1 Current
        id: 18
        unit_of_measurement: 'mA'
      - platform: sensor
        friendly_name: smartplug1 Current Consumption
        id: 19
        scaling: 0.1
        unit_of_measurement: 'W'
      - platform: sensor
        friendly_name: smartplug1 Voltage
        id: 20
        scaling: 0.1
        unit_of_measurement: 'V'
  1. wait for us to fix this. I created the related PR: https://github.com/rospogrigio/localtuya-homeassistant/pull/65

Cheers,
rospogrigio

Hi @rospogrigio @postlund
Thanks for working on my problem!
Tonight I try the code you posted and let you know. Thanks again

Thanks @rospogrigio and @postlund
You were able to understand and solve the problem immediately!

With these settings it works perfectly.
GREAT

Is it possible to integrate Tuya bulbs or is this functionality under development?

I use TuyaCustom, but there is a lot of delay on the status change, although better than the official Tuya integration.

@SmartM-ui , @postlund has just fixed this issue so in the new 2.0.1 release just published it should work: please revert to the previous configuration and confirm that it’s working for you.
Tuya bulbs should be already supported using the light component, however they have never been tested because we don’t own any of these devices: feel free to try it and provide feedback.
Bye!

Thanks @rospogrigio and @postlund,
I updated to version 2.01 and reconfigured the .yaml file and everything works fine for my Teckin SP22 sockets too!

Is it possible to change the scan time of the socket status?
I noticed that even if the operation should be local, the waiting time to monitor the status change is between 7 and 20 seconds.
Is it possible to force the update of the status change?
(example scan_interval: 5)

Thanks again.
In the evening I check the functioning of my bulbs

These are my current settings:

localtuya:

  • host: 192.168.x.x
    device_id: myID
    local_key: myLocalKey
    friendly_name: “teckin_plug1”
    protocol_version: “3.3”
    entities:

    • platform: switch
      friendly_name: smartplug1
      id: 1
      current: 18
      current_consumption: 19
      voltage: 20
  • platform: template
    sensors:
    smartplug1_voltage:
    friendly_name: “Smart Plug 1 voltage”
    unit_of_measurement: ‘V’
    value_template: “{{states.switch.smartplug1.attributes.voltage}}”

    smartplug1_current:
    friendly_name: “Smart Plug 1 current”
    unit_of_measurement: ‘mA’
    value_template: “{{states.switch.smartplug1.attributes.current}}”

    smartplug1_current_consumption:
    friendly_name: “Smart Plug 1 current consumption”
    unit_of_measurement: ‘W’
    value_template: “{{states.switch.smartplug1.attributes.current_consumption}}”