Tuya LOCAL with energy monitoring and without tuya-convert

Hi, I can’t make these working in HA. I tried many different configurations, but while I can see the values from my Tuya plug, I can’t see them in the Energy dashboard. My configuration looks like this

localtuya:
  - host: xx
    device_id: xx
    local_key: xx
    friendly_name: my-plug
    protocol_version: "3.3"
    entities:
      - platform: switch
        friendly_name: Plug
        id: 1

      - platform: sensor
        friendly_name: Plug-current
        device_class: current
        id: 18
        
      - platform: sensor
        friendly_name: Plug-current-consumption
        device_class: energy
        id: 19
        
      - platform: sensor
        friendly_name: Plug-voltage
        device_class: voltage
        id: 20

sensor:
  - platform: template
    sensors:
        my-plug-consumption:
          friendly_name: "Plug consumption"
          unit_of_measurement: 'kWh'
          value_template: "{{ states(my-plug.sensor.Plug-current-consumption) }}"

I see the three sensors, but the template reports unavailable. I tried also many other value_templates but none seem to work.

I think in the sensor all needs to be lower case?
there is a capital P in:
my-plug.sensor.Plug-current-consumption
you also have dashes instead of underlines.
also, the current consumption value is simply W, not kWh

my sensor is

kitchen_current_consumption:
        value_template: "{{ states.switch.kitchen.attributes.current_consumption }}"
        device_class: power
        unit_of_measurement: 'W'

I don’t use the Energy dash, so can’t help you there.

I managed to make it work with this file:

localtuya:
  - host: xx
    device_id: xx
    local_key: xx
    friendly_name: bibe
    protocol_version: "3.3"
    entities:
      - platform: switch
        friendly_name: bibe_switch
        id: 1

      - platform: sensor
        friendly_name: bibe_current
        device_class: current
        id: 18
        
      - platform: sensor
        friendly_name: bibe_consumption
        device_class: energy
        id: 19
        
      - platform: sensor
        friendly_name: bibe_voltage
        device_class: voltage
        id: 20

sensor:
  - platform: template
    sensors:
        bibe_current_template:
          friendly_name: "Current"
          unit_of_measurement: 'A'
          value_template: '{{ states("sensor.plug_current") | float / 10 }}'
          device_class: current
          
        bibe_consumption_template:
          friendly_name: "Consumption"
          unit_of_measurement: 'kWh'
          value_template: '{{ states("sensor.plug_consumption") | float / 10 }}'
          device_class: energy
          
        bibe_voltage_template:
          friendly_name: "Voltage"
          unit_of_measurement: 'V'
          value_template: '{{ states("sensor.plug_voltage") | float / 10 }}'
          device_class: voltage

however, although I can now see the correct values, I can’t add this device in the Energy tab. Any idea on this?

Moreover, I don’t know if I add another localtuya device how can I reference it, since in the templates I have no bibe reference whatsover, which is my identifier.

I had to add this in my sensor.yaml file for it to appear in the energy tab -

- platform: integration
  source: sensor.gosund2_lt_current_consumption
  name: Pi4_Power_Usage_Gosund
  unit_prefix: k
  unit: kWh
  round: 2

Guys, how to add a device that has 2 sockets with yaml? i tried adding an entity? but i only see one?

thnx

  - host: !secret tuya_switch_1_ip
    device_id: !secret tuya_switch_1_id
    local_key: !secret tuya_switch_1_key
    friendly_name: Tuya Switch1
    protocol_version: "3.3"
    entities:
      - platform: switch
        friendly_name: Tuya Switch1_1
        id: 1
        current: 18
        current_consumption: 19
        voltage: 20
      - platform: switch
        friendly_name: Tuya Switch1_2
        id: 1
        current: 18 
        current_consumption: 19 
        voltage: 20

You probably mean a 2-Gang switch.

I would do the configuration directly through config flow (Settings → Integrations → Add Integration → LocalTuya Integration) rather than fiddling around with configuration.yaml for LocalTuya.

Remember to delete the entries for LocalTuya inside configuration.yaml and restart HA before you go the config flow-way!

i figured it out, just need to change the id for the second one to : 2

still prefer yaml :slight_smile:

1 Like

Been a while since I played with these, but I just realised that my local tuya setup only works if I have internet, which obviously is not really a local setup.
I set up the plugs using the config flow method, then have a consumption template sensor.
Switching on/off works fine, just can’t get the power data when local.

OFFTOPIC just to let you know the easiest way I’ve seen to get local key is using https://pypi.org/project/tinytuya/

Anyone also get this to work with your Tuya plug? I can’t get them in the drop down list to add, even with this in the sensors

i need some help, whit customize.yaml for →
switch.desk:
sampling_size: ‘15’

cant understand how i must do

Where is the sensor.yaml file?

If you split all your integrations up into packages you would have sensors under their own .yaml file. If you have your sensors combined into the configuration.yaml file you would need to add it in there.

Same problem for me too :slight_smile:

I can’t find either

I don’t understand how to do it

Here is the guide

1 Like

How do you manage to get the daily/monthly/total consumption, i.e., kWh?

My integration so far:

localtuya:
  - host: 29.9.81.xxx
    device_id: xxxxx
    local_key: xxxxx
    friendly_name: smart_solar_switch
    protocol_version: "3.3"
    entities:
      - platform: switch
        friendly_name: solar_switch
        id: 1

      - platform: sensor
        friendly_name: solar_switch_current
        scaling: 0.1
        device_class: current
        unit_of_measurement: 'A'
        id: 18
        
      - platform: sensor
        friendly_name: solar_switch_consumption
        scaling: 0.1
        device_class: power
        unit_of_measurement: 'W'
        id: 19

      - platform: sensor
        friendly_name: solar_switch_voltage
        scaling: 0.1
        device_class: voltage
        unit_of_measurement: 'V'
        id: 20

Thank you

1 Like

Hey mate, which values did you use for current/voltage/power?