Tuya LOCAL with energy monitoring and without tuya-convert



image

Here some pics with the smartplug working.

yes, but what consumption is your device ratet for, just 75,2 W ?

A hairdryer has a lot more.

Did you measure the voltage in the house with a voltmeter ?
Did you compare these figures here with the smartlife app values ?

It looks good so far, but you need to check it again. I have used a precise energy meter to get an idea how far off the smart plug figures might be and that is important at least if you are on the hunt for hidden consumers that need more than expected.

And then next steps will be charts for Watt - which devices consumes right now what amount of power and then of cause the energy over time like here a chart about 3 plugs and total.
Here is one of our holiday “shed” and you can see the pikes in the blue line?
That is 200 cm tall fridge that cools against 25°C … cause summer arrived this week. Therefore the fridge runs longer.

And the blue line is also responsible for the total amount of consumption.

Hi can u share the yaml for energy consumed KWh , thanks

Dude thank u so much, worked like charme.
I just need now put to work the switch, i thought it was working, but when a turn off the switch, it turns on again but the relay didnt work.

there is no such yaml and it works wizard.

You have to dig into what Watt means and how to get to Wh used.
In school or at least gymnasium you had to learn about functions like differentials and integrals.

In the integral part you had to learn how to calculate the area below a graph and after that most likely how to calculate the volume of a rotating graph for example.

Power consumption or better energy is calculated by power times time or a 1 kW hair dryer running for 1 hour full steam which would mean 1 kW x 1 h = 1 kWh.

If for example the green line of this chart would represent your plug then it would mean that you need the amount of space / area below that green curve which is jumping up and down like a fever curve and sometimes at a higher base levell from 13:00 to 14:00 around 40W and then lower. If you look at the blue curve you see a complete different pattern right ?

Look at it and recognice the spike or peak it starts with and then settle at a lower level to follow a repeating pattern: that is caused by a fridge and its compressor which has a high demand power demand in the second they have to start the motor against a very high resistance of a compressor. And then there is a curve or patternover about 45 minutes of cooling but at different consumption levels. The more you would opend the door and loose cold to the environment which means more warm air inside the fridge (cold air falls literally out of your fridge doors down to the ground to be replaced by warm air) the more the cooling phases kick in. During night for example you will see the similar pattern but with longer breaks cause the door will be opened less and the fridge is not fighting against summer heat in your kitchen cause usually the night is colder.

That being said you should be able to recognice that both, the blue and the green curve offer different challenges far beyond the basic maths you had learned once in school in math, but the principles are the same.
Imagine you had a fridge like a hairdryer, that turns on and runs fully speed or is of then you would have not such fever curve but flattened lines with 2 levels like that blue line.
If on it uses 30W and if off at 0 W . 45 minutes turned on and 15 off would meand 3/4h x 30W or 90 / 4 Wh or 22,5 W x 1 h = 22,5 Wh

But imagine you would only do such power measurement 1 time per hour than it might be 30W or 0W … and now the sampling rate kicks in, how often do you measure the current or power consumption every hour and how often does it change during that time. The fridge is changing a lot while a light would stay the same and much easier to calculate. 1 hour means 60 minutes and 3600 seconds you could measure but your plug would go crazy.

The important point is that the tuya does not measure energy consumption itself or how many W were used for what a mount of time to get an hourly figure like 22 Wh from 2 to 3 pm. The tuya only reports the watts from time to time and if you get a report like 60W at 2:01 pm and the next with 0W at 2:02 pm you need to do the math based on assumptions like the watts stayed the same for 1 min or better 1/ 60 h x 60 W and needed 1 Wh in that 1 minute and 0W in the second minute so 1 Wh in 2 minutes. If you will do that for 60 times you would have 60 figures and the consumption for every minute always based on an assumption that the W would stay the same for the next 60 seconds which is not real life measurement and works for a light bulb but not for a fridge, even though over a long period the results will be quite good.
1 measurement per minute would mean a sampling rate of 1 and usuually those tuya seem to work quite well below a sampling rate of 20. I use 15 which means 15 values per minute or every 4 seconds an update.
imagine your fridge did not turned on at the beginning of a measurement then 60W would be missed for 4 seconds, or 60 W times 4 / 3600 hour or 60 W x 4 / 3600 h or 60 W x 1 / 900 h = 0,06 Wh would be missing. But considering you missed to count those 3,9 seconds in the beginning you also will miss that i shuts off earlier than expected so the average will stay the same. But sample rate of 15 is a lot better than only once a minute.

That being said you have know learned how to measure Wh or better energy and that is done for electricity by the RIEMAN Integral function and that is available for HomeAssistant and you will simply need to use your Watt sensor to create a Wh sensor based on the Rieman integral which is based on the sampling rate and the sensor measurements.

If you have a plug that measures your Echo show consumption it might show a 3 W figure and while running a video via silk browser or youtube it might need 6 W …
Then your switch.echo_show sensor might report as switch.echo_show_Wh the W 15 times per minute and the riemann integral will do the math for you and calculate the Wh for every 4 seconds and sum that up. So this is a counter that knows just 1 direction: going up

So you will get a sensor that grows and grows and grows and therefore your will need additional counters that will reset themselves like a switch.echo_show_Wh_1 where 1 stands for hourly reset and 2 for daily and 3 for weekly and 4 for monthly reset.
If you look at the sensor switch.echo_show_Wh_1 at 1 pm you will always see a 0 !

Why?
An hourly sensor will be resetted just every hour and at 1 or 2 or 4 pm that will have happened while the whole hourly consumption will be only visible at 00:59:56 pm

So you will need to know that you have a tuya plug that at least has a
WATT SENSOR like
switch.echo_show

Then you will use the rieman integral (taking care for the right sampling rate and right measurement parameters especially LEFT for higher consumptions and jumps you see from microwaves for higher accuracy) to create a Wh or Watthour sensor called
switch.echo_show_Wh

And finally you want to keep track on consumption per hours (in the beginning that is very usefull cause you will not wanna wait for days) , days, weeks and months where I use a convention to keep track on all the sensors I have.

switch.echo_show_Wh_1 = hour
switch.echo_show_Wh_2 = day
switch.echo_show_Wh_3 = week
switch.echo_show_Wh_4 = month

in total 6 sensors for Watt, then the riemann integral energy counter and the 4 periodically resetted counters.

And of cause you have to teach yourself in how to use these sensors and parameters right instead of asking for ready made yaml soultions and not learning the basics.
I do not wanna support that cause all those who do not understand all of these steps will not be able to interpret or understand the values and then start talking about false figures and consumptions that are not measured right … garbage in garbage out.

The HA documentation is good, the rieman integration works and is well described and from there you need the power metering and all those tuya local users that have gone the tuya local way and wanted to get the energy consumption went the same route cause otherwise you only can rely on smartlife / tuya online app - or you have to buy shelly products cause they have integrated the Wh or consumption measurement into that plug, so that unit can count Wh independently from HA by its own power and logic.

Check your coding, make sure you check on one time, not multiple times. I got a similar error due to getting more information at the same time which causing tuya to fail to connect. Anyway, local tuya is not very stable compare to sonoff which is why I do not add more tuya devices.

hi everybody. I have checked all the replies of this topic and, unless I am wrong, I have not find anything relating this point: is there a way to setup the period (timing) of the update of the sensor?
I have 4 different smart plug with meter and some of them update the value after a few seconds and other ones after minutes.

You are looking for something like a consistent update rate and there is something that deals with that topic discussed over there

And there are still somehow unsolved issues I guess but I am pretty happy with the following.

There is a parameter that seems to have different names but should be for tuya sampling_size as someone had mentioned.

sampling_size is the rate how often per minute the sensor is asked for a reply or current state like the Watt sensor we usually have to use to get further regarding consumption. This Watt sensor will reply “20.5” and we know that this means ‘W’

The sampling size should work up to 20 or in particular 20 / minute so you would get a curve for a minute with 20 values or every 3 seconds a reply while other sensors and manufacturers might reply quite more …

But that depends also on your situation you did not mention and most people do not mention cause they usually come over here after a while playing with those sensors and the smartlife app and of cause no one talks about his real situation cause they do not care.

I bet if you are coming from that smart life app and have arrived here recently you do not have cut the cord to the tuya smartlife app, right ?

And maybe your plugs are suffering ffrom overload and replying to your HA server asking for Watt and whatever else while the good old tuya server is also asking his plugs for replies.

Imagine all those that are stupidly collecting all the answer from current over volt to watt which means that you are constantly asking each of your plugs 3 sensors per each plug for replies while you do what with the current and voltage results ?

Nothing. zero, cause for consumption you will need Watt hours, so you need Watt x hours or in particular you have to calculate the area below the watt graph. Turn a 12 W light on and watch the graph over 1 hour and you will find what ?
12 W x 1 h = 12 Wh consumption.
Imagine you would turn that light on and off every quarter hour. What will be the result ?

12W x 1 / 4 h + 12 W x 1 / 4 h = 12 W x ( 2 / 4 h) = 12 W x 1 /2 h = 6 Wh

That is what has to be done to get from Watt or power to energy consumption in Wh.
Therefore you will need only 1 sensor and that is Watt and then the riemann integration which is a function that does what I described above as calculating the area below the graph over time. Old school math in 10 th grade on a gymnasium.
Why is it important?
Just to understand the full story which starts with an led bulb that turns on and off every 15 minutes or 1 / 4 of an hour.
And once you understood that you can go on and turn that bulb on every 1 / 60 of an hours … or every minute but for calculating energy hours are king, so 1 / 60 hours on and off and on and off will lead again to 30 x 1 / 60 hour on or 30 / 60 hours.
Now add a second bulb and turn that on every 20 minutes for just 10 minutes while running the first bulb for 1 min. on and off then your graph will look quite different but that is what is going on behind such plug power meter where multiple devices can be attached to , right ?

I have 1 line where the fridge is connected to and then also a light in the kitchen and a microwave and another device. So each of those can turn on and off (fridge does it automatically) and lead to jumps or spikes in consumption and the total consumption will still be the area below that watt graph which the riemann integration will calculate for you.

That being said you are dealing with the question how often those plugs are answering and my explanations try to tell you that you first have to know what is going on in your network with those plugs (avoid the stress to ask for current and voltage if you do not need those from the HA assistant side). Then try to cut the chord to tuya server sooner or later to simply get rid of the traffic and load on the plugs.
Having learned that it is time to deal with the plug itself and to think about what you really need.

Of cause you can be the scientist and claim that you will need 120 reports from each plug and Wattsensors to get the right energy consumption but is that true ?

No, you do not need every second a report of the plug how much W you are using.
For me the reliablility is king and better a lower report / sample rate than from time to time no reports and you will not even get behind that if you do not take care and check every sensor for the past 24 hours.

20 was the sampling size someone had found out here on the forum as the right size and I am simply not keen of loosing a few days due to too high sample size and therefore went a different road: 15 times a minute or every 4 seconds is enough for me.

Just to give you an idea what you are dealing with I simply ask you how you can make sure that each of these plugs is delivering or measuring the right way ?

Isn’t that the first topic you have to find out what these figures are worth , how reliable or trustworthy those are ?

Is a 20,5 W tuya reported value a 20,5 W figure or just in real world a 28 W figure ?
If it is a 28W figure your bills will be a lot higher than HA will report about the tuya plugs.

So get a good powermeter you can rely on and put that first into your grid and into that your tuya socket and then let both run for days or weeks and compare those results you will get just to make sure how far off they are. A good wattmeter is about 30 to 50€ and not to get from cheap china.

Once you will have gotten such a power meter you can check one socket by another and note the deviation you find out between plug and the powermeter at that use case.

ATTENTION: the riemann integration is part of this equation cause riemann has a few parameters you can choose on like “left” which means it will be more accurate in case you are using higher loads that can turn on and off in high frequencies for example a microwave oven program turns on multiple times a minute and switches between 200W and 1600W to warm something up that you had taken out of the freezer.

Once you have understood and done all of that you will come to realize more or less that a 15 times per minute report by the plug to the HA is good enough to give you a perfect estimation how much energy is running through this plug and the sum of all those plugs will match the annual bill quite well and that only 15 times a minute will deliver quite good results.
Why ? If the plug turns on in second 0,5 after last report your consumption calculated by HA will be too low for 3,5 seconds this time, but same will happen in the other direction too when you turn it off and that will bring it to a balance …

And all that has to be said and explained cause it would have hurt you sooner or later in the following questions that will occure later like how acurate are the tuya reports regarding consumption, how trust worthy and what has an impact on that and what does not.

If you would only measure every 30 minutes then the sample rate would be too low, but every 4 seconds is quite good.
If you try to go for 60 your sensors will get messed up and reports will sooner or later go bad , the data will be wrong or there will be no data recorded. And if you go for 20 then it might also get worse from time to time depending if you are still connected to the tuya server backend cause your tuya plugs are not talking to your android smartlife app but to the tuya / smartlife server and they are doing all the math needed to calculate the Wh that had ran through each plug to your devices that had burned or consumed the energy. And your app is then asking the tuya server for the Wh used today, this week, current month, last year and so on. But this tuya server plug communication might still be running in the background as long as you have them connected to the tuya network and the HA network too.

And here is what you can do to get a more consistant flow of data from each plug considering your local tuya installation is running well.

Open the [customize.yaml] file.
Add the following for each plug ( do not forget what a 15 there really means: 15 x 60 minutes x 24 hours or 21600 message about the watt used right now every 4 seconds.

10 plugs and you will get 216.000 reports each day … just to keep track of Watts per plug to calculate the live consumption and to feed the energy counters that might run too hourly, daily, weekly, monthly and so on, each of those creating again a lot of data cause every 4 seconds each of those counters will be updated in the same intervall. 1 Watt sensor creating 1 Wh sensor counter which will then feed 4 other energy consumption counters means 6 counters in total for just 1 plug and W and Wh only or 120.000 datapoints per plug each day … and maybe x 10 plugs or 1,2 Million data to tell you about the energy consumption.
And that might be the point to think twice if 20 or 15 or 10 or just 5 per minute might be enough - at least if you compare the whole tuya local stats of a plug with a wh meter that you can trust.

What you need is this for each plug [customize.yaml]

switch.desk:
  sampling_size: '15'

From then on HA or local tuya will ask the switch.desk plug every 4 seconds for an update and I have only created 1 sensor that I am tracking that way.

After that you should try to create a card with a graph and a time frame of 1 hour where you can hover over with your mouse and that should give you a time stamp and a report like 15.6 W and a few seconds later 16.5 or so or even 0
1 hours with 15 reports every minute means that you should find in that graph 900 values / timestamps … but I guess you will see the curve going up and down and not staying the same that you simply will trust it instead of counting if there are really 900 values or not in that graph of the last hour.

You can also check the status in the developer tools where you should get a result like this

At the end there is the parameter sampling_size: 15 and above you can see the tuya values I had to configure as current and voltage cause I simply got only these 3 values.

And of cause 227.5 must be volt and 113 can not be Watt, so have to be current or mA or what ever cause 15.1 has to be the Watt figure cause my energy meter was showing 15.08 watt …

Therefore this might be a reminder that the tuya sensors can be totaly off, do not try to do the Watt math based on Voltage and Current reported by local tuya integration and plug cause these can be completely off as in my case, while 15.1 Watt was pretty accurate.
But this is the first point you have to make sure that the values you will trust are the right ones and you have to do it for each and every tuya plug cause they are different and not so well calibrated except you had purchased a batch like we had done back then 8 or 2 packages of 4 from amazon … those were pretty close to each others and had not much deviation among each other.

1 Like

Sorry it’s been a while since I was here :slight_smile:

This is my package file, I’ve trimmed it a bit:

input_select:
  select_integration:
    name: 'Select Integration:'
    options:
      - Tuya 1 - Jed Fan
      - Tuya 2 - Tim Fan
      - Tuya 17 - Fin desk
      - Tuya 18 - Loft
    initial: Tuya 1 - Jed Fan

script:
  reload_integration:
    alias: Reload integration
    mode: restart
    sequence:
      service: rest_command.reload_integration
      data:
        entry_id: >
          {% set mapper =
            {'Tuya 1 - Jed Fan':'ae3f0d01118c11ebb31bc5038f27c09b',
             'Tuya 2 - Tim Fan':'ae3f2a79118c11eb85c23972f1e69333',
             'Tuya 17 - Fin desk':'df57abe323972ce704ac4c484b737bfe',
             'Tuya 18 - Loft':'2bc3eb2b62efcd6017b06a2084e99a38'} %}
          {% set state = states('input_select.select_integration') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}

  diagnostics_tuya_plugs:
    alias: Diagnostics Tuya plugs
    sequence:
      - service: input_select.select_option
        data_template:
          entity_id: input_select.select_integration
          option: >
            {% if is_state("sensor.tuya_smart_plug_1_power", "unavailable") %}
              Tuya 1 - Jed Fan
            {%-elif is_state("sensor.tuya_smart_plug_2_power", "unavailable") %}
              Tuya 2 - Tim Fan
            {%-elif is_state("sensor.tuya_smart_plug_17_power", "unavailable") %}
              Tuya 17 - Fin desk
            {%-elif is_state("sensor.tuya_smart_plug_18_power", "unavailable") %}
              Tuya 18 - Loft
            {% endif %}
      - service: homeassistant.turn_on
        entity_id: script.reload_integration

automation:  
  - alias: Diagnostics Tuya plugs looper
    initial_state: false
    trigger:
      platform: state
      entity_id: automation.diagnostics_tuya_plugs_looper
      from: 'off'
      to: 'on'
    action:
      repeat:
        until:
          - condition: template
            value_template: "{{ (not is_state('sensor.tuya_smart_plug_1_power', 'unavailable')) }}"
          - condition: template
            value_template: "{{ (not is_state('sensor.tuya_smart_plug_2_power', 'unavailable')) }}"
          - condition: template
            value_template: "{{ (not is_state('sensor.tuya_smart_plug_17_power', 'unavailable')) }}"
          - condition: template
            value_template: "{{ (not is_state('sensor.tuya_smart_plug_18_power', 'unavailable')) }}"
          # - condition: template
          #   value_template: '{{ repeat.index <= 20 }}'
        sequence:
          - service: homeassistant.turn_on
            entity_id: script.diagnostics_tuya_plugs
          - service: persistent_notification.create
            data_template:
              title: 'Tuya diagnostic {{repeat.index}} @ {{now().timestamp()|timestamp_local}}'
              message: 'Tuya diagnostic running. Iteration number {{repeat.index}}.'
          - delay: '00:00:03'

How to get these values im showing in red??

2 Likes

If the values aren’t readily available, you can use the Utility Meter integration.

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)