get a counter value of a specific date Topic is solved

Moderator: leecollings

Post Reply
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

get a counter value of a specific date

Post by steef27 »

Hi guys,

I need your help.
I would like to retrieve the gas usage over the past 365 days or the position of the gas meter on a specific date.
With the url below I get the counter values of each (!) day.

/json.htm?type=graph&sensor=counter&idx=IDX&range=year

Is it possible to insert a date parameter?

gtz
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Monday 02 December 2019 15:51 I would like to retrieve the gas usage over the past 365 days or the position of the gas meter on a specific date.
Is it possible to insert a date parameter?
This is not possible in the API call itself but quite easy to get it with dzVents. How do you want to use this ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

I want to calculate the annual usage by comparing the current position with the position 365 days ago. Every day.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Monday 09 December 2019 9:26 I want to calculate the annual usage by comparing the current position with the position 365 days ago. Every day.
Can you try this one ?

Code: Select all

--[[
    this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure  
]]--

local scriptVar = 'gasHistory' 

return
{
    on =  
    { 
        timer = { 'every 5 minutes' },
        httpResponses = { scriptVar },
    },

    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        -- ***** Your settings below this line 

        local gas = dz.devices('Gas') -- change to name of your gasMeter between quotes or id without quotes
        local gasText = dz.devices('gasText') -- text device showing gas Consumption 

        -- ***** No changes required below this line

        _G.logMarker =  _G.moduleLabel
        local day = 86400 -- (24 * 60 * 60)

        local function callYearGraph(id)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar })
        end

        local function daysInLastYear() 
            local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
            return math.floor(os.difftime(os.time(), lastYear) / day )
        end

        local function processResult(t)
            local gastToday,gasLastYear, gasNow, gasYear = 0, 0, 0 ,0
            local dateFmt = "%Y-%m-%d"

            local today = os.date(dateFmt,os.time())
            local yesterday = os.date(dateFmt,os.time() - day)
            local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)

            for index, gas in ipairs(t) do
                if gas.d == today then gasToday = gas.v gasNow = gas.c
                elseif gas.d == yesterday then gasYesterDay = gas.v 
                elseif gas.d == sameDayLastYear then gasLastYear = gas.v gasYear = gas.c
                end
            end

            dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
            dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
            dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
            dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
            
            return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
        end  

        local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
            gasText.updateText
                (   'Gas used today: ' .. gasToday .. ' M³ \n' .. 
                    'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' .. 
                    'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' .. 
                    'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³' 
                )
        end

        -- main    
        if item.isHTTPResponse and item.isJSON then
            updateTextSensor(processResult(item.json.result))
        elseif item.isTimer then
            callYearGraph(gas.id)
        else
            dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
MadMedic
Posts: 31
Joined: Wednesday 26 October 2016 14:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Huizen, the Netherlands
Contact:

Re: get a counter value of a specific date

Post by MadMedic »

waaren wrote: Monday 09 December 2019 14:14 Can you try this one ?
Hey Waaren, perfect dz vents script...
can I alter this for Energy (Power) consumption too ? Since power has 4 or 6 different values,
instead of gas which has only one, as far as I know

please let me know
.
"Life is too short for bad coffee"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

MadMedic wrote: Thursday 19 December 2019 16:36 can I alter this for Energy (Power) consumption too ? Since power has 4 or 6 different values,
Should be possible to do that but you will have to decide what you want to compare. Below you see the result for one day.
v + v2 is usage , r1 + r2 is return

Code: Select all

{
			"c1" : "6560.563",
			"c2" : "2941.709",
			"c3" : "4764.800",
			"c4" : "6741.620",
			"d" : "2018-12-19",
			"r1" : "0.000",
			"r2" : "0.531",
			"v" : "3.964",
			"v2" : "12.504"
		},
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

thanks, this is perfect!
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

Can I also use multiple custom sensors instead of 1 text sensor with all values?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Monday 27 January 2020 11:11 Can I also use multiple custom sensors instead of 1 text sensor with all values?
Please provide some more detail on what you mean / want.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

Instead of using one single textsensor, i would like to store each value in a separate custom sensor.
I would like to split the text values.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Monday 03 February 2020 11:28 Instead of using one single textsensor, i would like to store each value in a separate custom sensor.
I would like to split the text values.
That should probably be not too hard. If you share what you have now I can help.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

Code: Select all

--[[
    this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure  
]]--

local scriptVar = 'gasHistory' 


return
{
    
    on =  
    { 
		--devices = { 4484, },
        timer = { 'every 5 minutes' },
        httpResponses = { scriptVar },
    },

    --logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        -- ***** Your settings below this line 

        local gas = dz.devices(4484) -- change to name of your gasMeter between quotes or id without quotes
        local gasText2 = dz.devices(4746)
        local gasText3 = dz.devices(4747)
        local gasText4 = dz.devices(4748)
        local gasText5 = dz.devices(4749)


        -- ***** No changes required below this line

        _G.logMarker =  _G.moduleLabel
        local day = 86400 -- (24 * 60 * 60)

        local function callYearGraph(id)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar })
        end

        local function daysInLastYear() 
            local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
            return math.floor(os.difftime(os.time(), lastYear) / day )
        end

        local function processResult(t)
            local gasToday,gasLastYear, gasNow, gasYear = 0, 0, 0 ,0
            local dateFmt = "%Y-%m-%d"

            local today = os.date(dateFmt,os.time())
            local yesterday = os.date(dateFmt,os.time() - day)
            local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)

            for index, gas in ipairs(t) do
                if gas.d == today then gasToday = gas.v gasNow = gas.c
                elseif gas.d == yesterday then gasYesterDay = gas.v 
                elseif gas.d == sameDayLastYear then gasLastYear = gas.v gasYear = gas.c
                end
            end

            --dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
            --dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
            --dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
            --dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
            
            -- [[Aanpassing zodat verbuik vandaag ook wordt meegerekend ]]
            --return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
            return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear + gasToday)
        end  

     --   local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
     --       gasText.updateText
     --           (   'Gas used today: ' .. gasToday .. ' M³ \n' .. 
     --               'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' .. 
     --               'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' .. 
     --               'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³' 
     --           )
     --   end
        
        local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
            --gasText2.updateText('Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³')
            gasText2.updateCustomSensor(gasToday)
            gasText3.updateCustomSensor(gasYesterDay)
            gasText4.updateCustomSensor(gasLastYear)
            gasText5.updateCustomSensor(gasTotal)
        end
        
                   
        -- main    
        if item.isHTTPResponse and item.isJSON then
            updateTextSensor(processResult(item.json.result))
        elseif item.isTimer then
            callYearGraph(gas.id)
        else
            dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Friday 07 February 2020 20:15
can you try this ?

Code: Select all

--[[
    this dzVents script collects the current energy return / usage and gas usage to enable comparing with Same date last year figure  
]]--

local scriptVar = 'gasHistory' 


return
{
    
    on =  
    { 
		--devices = { 4484, },
        timer = { 'every 5 minutes' },
        httpResponses = { scriptVar },
    },

    --logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        -- ***** Your settings below this line 

        local gas = dz.devices(4484) -- change to name of your gasMeter between quotes or id without quotes
        local gasText2 = dz.devices(4746)
        local gasText3 = dz.devices(4747)
        local gasText4 = dz.devices(4748)
        local gasText5 = dz.devices(4749)


        -- ***** No changes required below this line

        _G.logMarker =  _G.moduleLabel
        local day = 86400 -- (24 * 60 * 60)

        local function callYearGraph(id)
            url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=year&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar })
        end

        local function daysInLastYear() 
            local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
            return math.floor(os.difftime(os.time(), lastYear) / day )
        end

        local function processResult(t)
            local gasTable = {}
			
            local dateFmt = "%Y-%m-%d"

            local today = os.date(dateFmt,os.time())
            local yesterday = os.date(dateFmt,os.time() - day)
            local sameDayLastYear = os.date(dateFmt,os.time() - daysInLastYear() * day)

            for index, gas in ipairs(t) do
                if gas.d == today then 
					gasTable.Today = gas.v 
					gasTable.gasNow = gas.c
                elseif gas.d == yesterday then 
					gasTable.YesterDay = gas.v 
                elseif gas.d == sameDayLastYear then 
					gasTable.LastYear = gas.v 
					gasTable.gasYear = gas.c
                end
            end

			gasTable.gasYearConsumption = gasTable.gasNow - gasTable.gasYear + gasTable.gasToday
            --dz.log('gasToday: ' .. gasToday .. ' M³',LOG_DEBUG)
            --dz.log('gasYesterDay: ' .. gasYesterDay .. ' M³',LOG_DEBUG)
            --dz.log('gasLastYear: ' .. gasLastYear .. ' M³',LOG_DEBUG)
            --dz.log('gasYearConsumption: ' .. (gasNow - gasYear) .. ' M³',LOG_DEBUG)
            
            -- [[Aanpassing zodat verbuik vandaag ook wordt meegerekend ]]
            --return gasToday, gasYesterDay, gasLastYear, (gasNow - gasYear)
            return gasTable
        end  

     --   local function updateTextSensor(gasToday, gasYesterDay, gasLastYear, gasTotal)
     --       gasText.updateText
     --           (   'Gas used today: ' .. gasToday .. ' M³ \n' .. 
     --               'Gas used yesterday: ' .. gasYesterDay .. ' M³ \n' .. 
     --               'Gas used same day last Year: ' .. gasLastYear .. ' M³ \n' .. 
     --               'Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³' 
     --           )
     --   end
        
        local function updateTextSensor(t)
            --gasText2.updateText('Gas used in last ' .. daysInLastYear() .. ' days: ' .. gasTotal .. ' M³')
            gasText2.updateCustomSensor(t.gasToday or 0)
            gasText3.updateCustomSensor(t.gasYesterDay or 0)
            gasText4.updateCustomSensor(t.gasLastYear) or 0
            gasText5.updateCustomSensor(t.gasTotal or 0)
        end
        
                   
        -- main    
        if item.isHTTPResponse and item.isJSON then
            updateTextSensor(processResult(item.json.result))
        elseif item.isTimer then
            callYearGraph(gas.id)
        else
            dz.log('Error while retrieving gas data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

thanks, great!
Piacco
Posts: 69
Joined: Friday 14 November 2014 9:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by Piacco »

I want to calaculate the energy cost since I got my new energy contract until now.

An example I get a new energy contract on the 26th of june 2019, and i want to calculate how many energy I have used en produced since now.
I tought something like this:
26-06-2019 Get countervalue of the P1-meter C1 and C2 and subtract it with the countervalue of my P1-meter C1 and C2 from today and do exact the same for the delivered energy with counter C3 and C4.

But how do i get the countervalue on a specific day?

Greetz,

Piacco
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

Piacco wrote: Monday 04 May 2020 17:01 But how do i get the countervalue on a specific day?
With http://<domoticz_ip:domoticz_port>/json.htm?type=graph&sensor=counter&idx=<idx>&range=year
you get all days
So walk the result table and select the specific date.
Or if it is a once of date then do this in your browser, write down the counter values and make these constants in your script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: get a counter value of a specific date

Post by steef27 »

I've been running the script for some time now and I noticed that "sameDayLastYear" is not a year ago today, but a year ago tomorrow.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: get a counter value of a specific date

Post by waaren »

steef27 wrote: Thursday 29 April 2021 11:28 I've been running the script for some time now and I noticed that "sameDayLastYear" is not a year ago today, but a year ago tomorrow.
can you try again after changing

Code: Select all

        local function daysInLastYear() 
            local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
            return math.floor(os.difftime(os.time(), lastYear) / day )
        end
to

Code: Select all

        local function daysInLastYear() 
            local lastYear = os.time{day = dz.time.day, year = dz.time.year - 1, month = dz.time.month}
            return math.ceil(os.difftime(os.time(), lastYear) / day )
        end
?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests