High amp dc power meter

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

High amp dc power meter

#1 Post by olleman » 18 May 2021, 21:22

Hi!

I have an INA219 with 350A shunt to meassure my total Ah consumption on my boat. I send the meassured value to domoticz each 10 seconds which is the minimum duration for sensor values being sent to domoticz. Domoticz then calculates the total Ah consumption from these readings. This works quite well but when I have high loads on for a very short time, like thrusters, it's not accurate enough.

Is there some way to meassure each second and somehow acumulate the values and then send them to domoticz each 10 seconds?

How else can I use the INA219 as an energy monitor?

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#2 Post by TD-er » 18 May 2021, 22:08

What is this 10 sec interval based on?
The INA219 can perform multiple measurements per sec.
If the limit is determined by the communication to Domoticz, then you could perform multiple measurements via rules and add them to a variable, store it in a dummy variable and then flush that from rules.

For example you could create a loop timer and call taskrun from the rules to read the sensor more often with a very regular interval.
If the task reads a new value, this will then result in an event which you can accumulate in a variable.
After N reads flush the variable to a dummy (and divide it by the number of samples) and call taskrun of that dummy to send the value to Domoticz.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#3 Post by olleman » 20 May 2021, 22:03

TD-er wrote: 18 May 2021, 22:08 What is this 10 sec interval based on?
The INA219 can perform multiple measurements per sec.
If the limit is determined by the communication to Domoticz, then you could perform multiple measurements via rules and add them to a variable, store it in a dummy variable and then flush that from rules.

For example you could create a loop timer and call taskrun from the rules to read the sensor more often with a very regular interval.
If the task reads a new value, this will then result in an event which you can accumulate in a variable.
After N reads flush the variable to a dummy (and divide it by the number of samples) and call taskrun of that dummy to send the value to Domoticz.
Thankyou, I can always rely on you to get an answer:) however this is quite a leap for me me who isn't familiar with scripting or programming. Could you please direct me to the relevant documentation for this? I can't seem to find the answers just by looking att rules in the wiki.

Feel free to give me an example of working code if you've got the time:)

10 seconds comes from domoticz. It's the minimum time between sensor updates. With your solution which is exactly what I'm looking for I could increase the time to 30 seconds or even 60.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#4 Post by olleman » 21 May 2021, 09:02

I think you actually did point me in the right direction. Could you please comment on my code below, am I on the right track?

Code: Select all

 On System#Boot do    //When the ESP boots, do
    timerSet,1,1      //Set Timer 1 for the next event in 1 seconds
    timerSet,2,10      //Set Timer 2 for the next event in 10 seconds
 endon

On Rules#Timer=1 do  //When Timer1 expires, do
   TaskRun, 5
   timerSet,1,1       //Set Timer 1 for the next event in 1 second
endon

On Rules#Timer=2 do  //When Timer2 expires, do
   timerSet,2,10       //Set Timer 2 for the next event in 10 seconds
   TaskValueSet 6,1,0
endon


on INA#Current do
TaskValueSet 6,1,[Test#Dummy]+%eventvalue%/3600 //Add 1 seconds worth of Ah the dummy value. 1 hour = 60*60=3600
endon
Last edited by olleman on 21 May 2021, 09:04, edited 1 time in total.

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#5 Post by TD-er » 21 May 2021, 09:03

Right now I don't have time to write down the whole script, but the least I can do is give already some links to the documentation parts:

To set a loop timer:
https://espeasy.readthedocs.io/en/lates ... oop-timers

for storing the value, you could either use a dummy task, or store it in a variable.
If I'm not mistaken, Domoticz also supports energy logging via ever increasing counters. (e.g. a Wh meter)
It then takes the previous value and the time passed since to perform some computations to convert the measured values into some Wh unit of measure.
Using a dummy can also be useful to send directly to a controller without having to know how the other and needs the data to be formatted.
For interacting with a dummy task, you may need taskvalueset to set a value in a dummy task and taskrun to "flush" the values to a connected controller.

See here some example of counting values in a dummy task and send them to Domoticz via HTTP: https://espeasy.readthedocs.io/en/lates ... onsumption


Or you do the calculations yourself on ESPEasy and just push what you've computed.

This requires the "let" command to store a value in a variable.

In either case you need the "taskrun" to trigger a run of the INA219 sensor and you will get some event as soon as it is done processing the sensor measurement.
For example if your INA219 task is called "ina" and the value you need is "Amp" you will get an event named "ina#Amp" on which you act in the rules like this:

Code: Select all

on ina#Amp do
  // value is stored in %eventvalue1%
  let,1,[var#1]+%eventvalue1%  // Add new value to the existing one in variable #1
  let,2,[int#2]+1  // Count the number of samples stored in variable #2
endon
N.B. when referring to the value of "ina#Amp" you need to use [] => [ina#Amp]
When acting on its event, you should not use the [] as shown in my code.

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#6 Post by TD-er » 21 May 2021, 09:06

olleman wrote: 21 May 2021, 09:02 I think you actually did point me in the right direction. Could you please comment on my code below, am I on the right track?

Code: Select all

 On System#Boot do    //When the ESP boots, do
    timerSet,1,1      //Set Timer 1 for the next event in 1 seconds
    timerSet,2,10      //Set Timer 2 for the next event in 10 seconds
 endon

On Rules#Timer=1 do  //When Timer1 expires, do
   TaskRun, 5
   timerSet,1,1       //Set Timer 1 for the next event in 1 second
endon

On Rules#Timer=2 do  //When Timer2 expires, do
   timerSet,2,10       //Set Timer 2 for the next event in 10 seconds
   TaskValueSet 6,1,0
endon


on INA#Current do
TaskValueSet 6,1,[Test#Dummy]+%eventvalue%/3600 //Add 1 seconds worth of Ah the dummy value. 1 hour = 60*60=3600
endon
Better to use the looptimer, as that will schedule its new run on the old one.
Setting a new timer in the rules may cause some drift, as it may take some time to process the rules.

Also you can better use a variable to count the number of samples instead of hoping the timers will be in phase.
Otherwise you may one time only have processed 9 samples and the next can be 11 samples.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#7 Post by olleman » 21 May 2021, 09:10

TD-er wrote: 21 May 2021, 09:06
olleman wrote: 21 May 2021, 09:02 I think you actually did point me in the right direction. Could you please comment on my code below, am I on the right track?

Code: Select all

 On System#Boot do    //When the ESP boots, do
    timerSet,1,1      //Set Timer 1 for the next event in 1 seconds
    timerSet,2,10      //Set Timer 2 for the next event in 10 seconds
 endon

On Rules#Timer=1 do  //When Timer1 expires, do
   TaskRun, 5
   timerSet,1,1       //Set Timer 1 for the next event in 1 second
endon

On Rules#Timer=2 do  //When Timer2 expires, do
   timerSet,2,10       //Set Timer 2 for the next event in 10 seconds
   TaskValueSet 6,1,0
endon


on INA#Current do
TaskValueSet 6,1,[Test#Dummy]+%eventvalue%/3600 //Add 1 seconds worth of Ah the dummy value. 1 hour = 60*60=3600
endon
Better to use the looptimer, as that will schedule its new run on the old one.
Setting a new timer in the rules may cause some drift, as it may take some time to process the rules.

Also you can better use a variable to count the number of samples instead of hoping the timers will be in phase.
Otherwise you may one time only have processed 9 samples and the next can be 11 samples.
Ok, I will look at your code and see if I can understand how it all works :)

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#8 Post by olleman » 21 May 2021, 09:29

If I'm not mistaken, Domoticz also supports energy logging via ever increasing counters. (e.g. a Wh meter)
It then takes the previous value and the time passed since to perform some computations to convert the measured values into some Wh unit of measure.
This is what I have used before but since domoticz seems to have a minimum of 10 seconds between each sensor update the resolution becomes to low. For example, if I use the bow trhuster for 6 seconds @ 400A it might not "see" it at all or it might count it as 10 seconds.

Please correct me if I'm wrong but to get better resolution it seems that I will have to use the ESP for the computations and then send the Ah to domoticz.

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#9 Post by TD-er » 21 May 2021, 09:41

I am convinced you need to sample more often, just to see those peaks in consumption.
I don't know how Domoticz does internally process the values, so maybe sending more frequently may not improve things.
But by just adding sample readings to a counter at a regular interval you do create some incremental Wh like value which may later need to be corrected for the actual measurement interval.

So then you might just collect samples every second and push the total count every 10 minutes (maybe already divide the counter when adding it)

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#10 Post by olleman » 21 May 2021, 11:23

So I got it nailed down to this now, does this seem better to you?

One thing I'm concerned about is the loop timer. I'm now counting samples and relying on the fact that 60 samples = 60 seconds (Since I divide each sample by 3600). If the timer were to drift for whatever reason, the Ah
won't be correctly computed.

Code: Select all

On System#Boot do    //When the ESP boots, do
  looptimerset_ms,1,1000     // Start loop timer 1, 1000 msec interval
endon

On Rules#Timer=1 do
taskrun,11
if [var#2] >= 60 //if 60 samples have been collected (60 seconds passed) send Ah to domoticz and reset counter
   let,2,0
   SendToHTTP 192.168.50.10,8080,/json.htm?type=command&param=udevice&idx=49&nvalue=0&svalue=[batteri_total#Amps];[var#1]
endon


on batteri_total#Amps do
  // value is stored in %eventvalue1%
  let,1,[var#1]+%eventvalue1%/3600  // Add new value to the existing one in variable #1 as Ah (1 sample taken each second so each sample is 60*60=3600th of and Ah)
  let,2,[int#2]+1  // Count the number of samples stored in variable #2
endon
I can't reset the consumption since domoticz wants the accumulated value sent for the sensor device I'd like to use.

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#11 Post by TD-er » 21 May 2021, 11:34

You're missing an endif

Code: Select all

On System#Boot do    //When the ESP boots, do
  looptimerset_ms,1,1000     // Start loop timer 1, 1000 msec interval
endon

On Rules#Timer=1 do
taskrun,11
if [var#2] >= 60 //if 60 samples have been collected (60 seconds passed) send Ah to domoticz and reset counter
   let,2,0
   SendToHTTP 192.168.50.10,8080,/json.htm?type=command&param=udevice&idx=49&nvalue=0&svalue=[batteri_total#Amps];[var#1]
endif // <<<< This one was missing
endon


on batteri_total#Amps do
  // value is stored in %eventvalue1%
  let,1,[var#1]+%eventvalue1%/3600  // Add new value to the existing one in variable #1 as Ah (1 sample taken each second so each sample is 60*60=3600th of and Ah)
  let,2,[int#2]+1  // Count the number of samples stored in variable #2
endon
What you can do, along with clearing variable 2, is storing the last time you sent values.
Since very recent commits you now can use the %uptime_ms% flag, so you can correct for the actual time spent collecting values.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#12 Post by olleman » 21 May 2021, 12:23

Thanks for your feedback. One thing I noticed is that every time I hit "submit" on the rules (like now when I added "endif") the variable where the Ah are stored gets reset. Can I do anything aboout this? Even if i'm not editing the rules all the time it would be nice to have it stored a bit more permanently if possible.

Initially I got values to domoticz a bit faster then expected. Turned out that I had the task set for 10 seconds interval which caused an extra sensor reading each 10th second. Now I have set the interval for 3600 and for the passed 10 minutes I'm getting new updates each 10th seconds with no drift at all. What is the highest setting for the interval or can I disable it totally for this task? Hopefully the error caused by collecting samples will be so minor that it doesn't affect the outcome in practice. I don't have the correct build on the ESP in my boat and it's a pain to change :)

I'm so thankful for your help, I'm almost there now and this is something that has buggered me for the last 2 years :)

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#13 Post by TD-er » 21 May 2021, 13:13

You no longer need the task interval itself as you're calling taskrun yourself.
So set it as high as possible.

About the variable being cleared when submitting the rules.
Can you check to see if the unit may be rebooting?
The variables are cleared at boot.
If you need to survive a reboot, you should store them in a dummy task instead of using a variable (taskvalueset is the command you're looking for)
N.B. storing in a dummy task will only survive "warm reboots" like a crash or an intended reboot, not a power cycle.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#14 Post by olleman » 21 May 2021, 13:42

Ok, will look for this. I want to have a couple of hours of meassuring data to domoticz before anymore rebooting.

I think I read somehwere that using the internal variable instead of a dummy was less resource hungry for the ESP. Is this true? Maybee I can learn to live with this if that's the case.

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#15 Post by TD-er » 21 May 2021, 13:48

Depends on where your focus is on resources.
For example on ESP8266 you only have 12 tasks, so adding a dummy task is taking over 8% of those resources :)

In the end it doesn't really matter I guess, unless you need a lot of variables.

One thing that does matter is that the accuracy of floating point values in a variable is higher as it is stored as a double.
But for your use case that's also not an issue, as a float can store upto 6 - 7 decimal digits.

If you need more precision (e.g. to store a unix timestamp) you will see a difference in accuracy.

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#16 Post by olleman » 21 May 2021, 14:00

hehe this ESP is more or less overloaded allready with 9 real devices. 1 more dummy device might not make that much of a difference :)
1 temp sensor HD1080
1 BME280
2 Ds18b20
1 Flow meter
1 Weight sensor
2 INA219 devices
1 relay

Thanks again, I'll evaluate and see how this compares to my old way of meassuring. In the end I sense that the difference is more or less negligable :roll:

olleman
Normal user
Posts: 53
Joined: 27 Aug 2017, 08:10

Re: High amp dc power meter

#17 Post by olleman » 27 May 2021, 13:47

TD-er wrote: 21 May 2021, 13:13 You no longer need the task interval itself as you're calling taskrun yourself.
So set it as high as possible.

About the variable being cleared when submitting the rules.
Can you check to see if the unit may be rebooting?
The variables are cleared at boot.
If you need to survive a reboot, you should store them in a dummy task instead of using a variable (taskvalueset is the command you're looking for)
N.B. storing in a dummy task will only survive "warm reboots" like a crash or an intended reboot, not a power cycle.
Yes, it does indeed reboot when this problem happens. For now I have changed to dummy tasks and the value is now stored between these reboots. I wonder if you could give me advice on something related to this. I now have this code

Code: Select all

On System#Boot do    //When the ESP boots, do
  looptimerset_ms,1,1000     // Start loop timer 1, 1000 msec interval
endon

On Rules#Timer=1 do
taskrun,11
if [var#2] >= 10
   let,2,0
   let,3,[var#3]/10
   SendToHTTP 192.168.50.10,8080,/json.htm?type=command&param=udevice&idx=49&nvalue=0&svalue=[var#3];[Current_Ah#Ah]
   let,3,0
endif
endon


on batteri_total#Amps do
  // value is stored in %eventvalue1%
  TaskValueSet 12,1,[Current_Ah#Ah]+%eventvalue1%/3600  // Add new value to the existing one in variable #1
  let,2,[int#2]+1  // Count the number of samples stored in variable #2
  let,3,[var#3]+%eventvalue1%  // Add new value to the existing one in variable #3 for average power
endon
My intent with the var#3 is to send an average energy reading to domoticz for the last 10 seconds.

The energy output file attached to this post shows the data I'm getting to domoticz with above rules. There's something funny going on with the energy readings (yellow) at the marked places. The spikes in consumption is my fridge turning on and off. And I can see from the temperature in the fridge that it does in fact run during these periods when the energy never exceeds ~1000 (red markings). The consumed energy (blue) during one hour seems to be correct but the energy is wrong. This can be a problem with domoticz also but do you see anything in my code that could cause this seamingly random beahavior?
Attachments
energy output.JPG
energy output.JPG (49.78 KiB) Viewed 8175 times

TD-er
Core team member
Posts: 9221
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: High amp dc power meter

#18 Post by TD-er » 27 May 2021, 17:10

Not really sure if I get the problem you try to describe.
And maybe I do need some more info.

For example, do you still have a set interval in the task running the INA219?
Maybe that one interferes with the rules script?

Second, I am wondering if this line does what you intend to do:

Code: Select all

TaskValueSet 12,1,[Current_Ah#Ah]+%eventvalue1%/3600
Why not just add the value and when you try to send it divide its total by 3600.
The rules calculate should do the calculations correct for the "let" command, but I'm not sure if it also works correct on the TaskValueSet call.
So maybe store it first in a new variable and then store this variable using TaskValueSet.

Not sure if the INA219 plugin can return a "NaN" value and if so, what the rules calculation will do with it.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests