Page 1 of 1

Writing historic value in Managed Counter

Posted: Thursday 21 January 2021 9:27
by lavola
Hi,
I'm trying to get an overview of how my yearly power (and gas) consumption changes over time. I know now we're working from home most of the time my power and gas consumption will be higher, and I want to see how much it changes.
I do have a script (Thanks @waaren, https://domoticz.com/forum/viewtopic.php?t=30376) which calculates the consumption over the last year (value(current) - value(today - 1 year)) and I now write that in a managed counter. That works just fine. But if I want to see how it evolves over time, I should also get these values into the history log.
parts of my code:

Code: Select all

local powerYearConsumption = dz.devices('Power Year consumption') -- Counter device showing power Consumption
powerTotal = (powerToday - powerLastYear)
powerYearConsumption.updateCounter(powerTotal)
Possible options I see now:
1. Use a custom sensor to register the values. That will keep a history log of all values and will also calculate the daily and monthly values. However, I don't need a value every 5 minutes (or every time I calculate it), I only need a daily value over time.
2. Store the value directly into the history log, which for me sounds ideal. I've found a way to do this using a JSON call:

Code: Select all

/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=COUNTER;USAGE;DATE
source: https://www.domoticz.com/wiki/Domoticz_ ... ed_counter
But as I'm using a dzVents script, I'd rather have a dzVents code to do this.
Any way to directly store a historic value into a managed counter using a dzVents command?
Many thanks to you all!
L

Re: Writing historic value in Managed Counter  [Solved]

Posted: Thursday 21 January 2021 10:28
by waaren
lavola wrote: ↑Thursday 21 January 2021 9:27 But as I'm using a dzVents script, I'd rather have a dzVents code to do this.
Any way to directly store a historic value into a managed counter using a dzVents command?
Many thanks to you all!
Using this function? (call with updateHistory(id, date, nvalue, svalues )

Code: Select all

-- only available for managed counters
local function updateHistory(id, date, nValue, sValues)
	local url = domoticz.settings['Domoticz url'] ..'/json.htm?type=command&param=udevice&idx=' .. id ..
                '&nvalue=' .. nValue .. '&svalue=' .. sValues .. ';' .. date
   domoticz.openURL(url)
   return
end

Re: Writing historic value in Managed Counter

Posted: Thursday 21 January 2021 14:01
by lavola
Wow, why didn't I think of that?!
Actually not really the dzVents command I was looking for, but creating a function as a Workaround is of course just as perfect :-)

Many thanks for the quick response
Kudos for being so inventive!

Re: Writing historic value in Managed Counter

Posted: Thursday 21 January 2021 15:52
by waaren
lavola wrote: ↑Thursday 21 January 2021 14:01 Actually not really the dzVents command I was looking for, but creating a function as a Workaround is of course just as perfect :-)
Thx. This needs some more thinking but could well be I will implement this as a dzVents method for managed counters. Quite a number of dzVents functions are already implemented in this way.

Re: Writing historic value in Managed Counter

Posted: Sunday 07 February 2021 0:06
by PSUtec
Dear Waaren,

I tried to use the dzvents (3.1.4) updateHistory function as decribed in the script.
Question: in your example: mCounter.updateHistory('2021-01-22', 10;1777193'), what means 10;1777193; I guess that one of the figures is the value to attach to the date, but the other?
Also I suspect a typo: missing ' beforer 10.

Anyway, I've created a managed counter (water) and launched:

dz.devices('VS_water').updateHistory( '2021-02-06','4.6' ).

As the result, the counter indicates: 20.210 m³ and nothing in the device log.

Thanks for your answer.

Re: Writing historic value in Managed Counter

Posted: Sunday 07 February 2021 1:05
by waaren
PSUtec wrote: ↑Sunday 07 February 2021 0:06 Also I suspect a typo: missing ' beforer 10.
Is indeed a typo. Corrected now

If you use this in your script (replace 783 with 'name' or idx of your device)

Code: Select all

        dz.devices(783).updateCounter(12345678 ) -- this sets the counter on the utility tab
        dz.devices(783).updateHistory( '2021-02-07','11;345678' ) -- this updates the long term log = one value / day  = table meter_calendar
        dz.devices(783).updateHistory( '2021-02-07 00:45:00','0;345678' ) -- this updates the short term log. Can be many values / day  = table meter   
You will get this in the database

Code: Select all

sqlite> select * from meter_calendar where devicerowid =783;
DeviceRowID|Value|Counter|Date
783|345678|11|2021-02-07

sqlite> select * from meter where devicerowid =783;
DeviceRowID|Value|Usage|Date
783|0|345678|2021-02-07 00:45:00
and this in the GUI (utility tab)
testManagedCounter.png
testManagedCounter.png (18.56 KiB) Viewed 1965 times

Re: Writing historic value in Managed Counter

Posted: Tuesday 28 September 2021 9:14
by ragz
Hi Warren, thank you for all your great work for this community.

Did this updateHistory for counters get removed since your post here? It's giving me nil-errors when trying to use it

Re: Writing historic value in Managed Counter

Posted: Tuesday 28 September 2021 9:16
by heggink
In case you missed the post, Waaren passed away.

Sent from my SM-G980F using Tapatalk