Tuya LOCAL with energy monitoring and without tuya-convert

I would also like to know how to get the KWh into Home Assistant.

You have to use the integration and the utility meter and all will be ok.
this is my example

#Template Integration

  - platform: integration
    source: sensor.smartplug01_current_consumption
    name: smartplug01_energy_spent
    unit_prefix: k
    round: 2

and Utility meter

# Geladeira
  kwh_gasto_dia_ge:
    source: sensor.smartplug01_energy_spent
    cycle: daily
  kwh_gasto_semana_ge:
    source: sensor.smartplug01_energy_spent
    cycle: weekly
  kwh_gasto_mes_ge:
    source: sensor.smartplug01_energy_spent
    cycle: monthly

There is the integration first

sensor:
  - platform: integration
    source: sensor.plug_1_power
    name: energy_spent01
    method: left
    unit_prefix: k
    round: 2

and then the meter

utility_meter:
  dailykitch1_energy:
    source: sensor.energy_spent01
    cycle: daily

This can be daily, weekly, monthly etc…

HTH

2 Likes

what is the latest method to add local tuya with energy monitoring? I have successfully installed local tuya, through both UI and YAML methods. what I am missing is the Current, Power, Voltage and total energy(KWh) measure available in tuya/smart home apps.

what should be entered here

2 Likes

can you share your yaml code for local tuya or if done from UI then how?

Finally made it to work.
image

  • Followed the following video to find the local key and device id.
  • Used config flow method to setup
  • first setup switch id 1
  • select sensor and use following for each sensor total of 4 sensors.
  • following id’s should be used
ID 1 is for switch 
ID 4 is  the current   unit used will be "mA"
ID 5 is  power/consumption   unit will be "W"  scaling factor "0.1"
ID 6 is Voltage  Unit will be  "V"  scaling factor "0.1" 

===============
Comparison with Sonoff POW2

2 Likes

Just a question here. I have this all setup with my iBBQ temp probes. It works great. But a BBQ probe is something not online all the time. In fact it is offline way more than online. Is there some setting to prevent the 8000 log entries failing to connect? Or do I just disable in HA?

What do I choose to get kWh?

Finally got my plugs working on local Tuya, with energy monitoring. For those still trying: the part that had me stumped was I kept doing it at night when I had time, so the plug was off and the power readings had no values to compare.
So for example in the above video at time 7:17, if you have something running when you do the get device details, it will have the amps/watts etc listed.
So the step at 9:07 when you select the values (I read this elsewhere) will only list new values when first added, so to match up the power usage codes try and do these 2 parts as close together as possible. (may have to remove the device and re-add to get it with values)

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