Writing historic value in Managed Counter  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
lavola
Posts: 2
Joined: Thursday 21 January 2021 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Writing historic value in Managed Counter

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing historic value in Managed Counter  [Solved]

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
lavola
Posts: 2
Joined: Thursday 21 January 2021 8:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Writing historic value in Managed Counter

Post 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!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing historic value in Managed Counter

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
PSUtec
Posts: 9
Joined: Sunday 11 November 2018 12:25
Target OS: Linux
Domoticz version: 12896
Location: Ferrières (BE)
Contact:

Re: Writing historic value in Managed Counter

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing historic value in Managed Counter

Post 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 1963 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ragz
Posts: 10
Joined: Friday 10 January 2020 16:54
Target OS: -
Domoticz version:
Contact:

Re: Writing historic value in Managed Counter

Post 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
Attachments
Screenshot_20210928-090451__01.jpg
Screenshot_20210928-090451__01.jpg (48.77 KiB) Viewed 1553 times
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Writing historic value in Managed Counter

Post by heggink »

In case you missed the post, Waaren passed away.

Sent from my SM-G980F using Tapatalk

Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest