Get Domoticz Uptime in Lua or via Json?

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Get Domoticz Uptime in Lua or via Json?

Post by ben53252642 »

Is it possible to get Domoticz uptime using Lua or via the Json interface?

I'm wanting to create a Lua script with an if condition that Domoticz must have been up for 10 minutes before it will run.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by ben53252642 »

Solved by guessing! :mrgreen:

Code: Select all

/json.htm?type=command&param=getuptime

Code: Select all

{
   "days" : 1,
   "hours" : 7,
   "minutes" : 36,
   "seconds" : 45,
   "status" : "OK",
   "title" : "GetUptime"
}
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by ben53252642 »

Example Lua Script to get Domoticz uptime in minutes.

In the below example, in additional to the Motion Sensor changing to "On", Domoticz uptime must be over 10 minutes.

Code: Select all

-- Domoticz Configuration
username="USERNAME"
password="PASSWORD"
server="192.168.0.5"

commandArray = {}

-- Get Domoticz Uptime in Minutes
json = (loadfile "/root/domoticz/scripts/lua/JSON.lua")()
command='curl -s -k "https://'..username..':'..password..'@'..server..'/json.htm?type=command&param=getuptime"'
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
output = json:decode(result)
domoticzuptime = ((output.days * 1440) + (output.hours * 60) + (output.minutes))

if (devicechanged["Motion Sensor"] == 'On' and domoticzuptime > 10) then
commandArray['Main Bedroom'] = 'On'
end

return commandArray
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by waaren »

ben53252642 wrote: Wednesday 19 September 2018 1:48 Is it possible to get Domoticz uptime using Lua or via the Json interface?

I'm wanting to create a Lua script with an if condition that Domoticz must have been up for 10 minutes before it will run.
in dzVents you can get the required info by

Code: Select all

domoticz.startTime.minutesAgo
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
HansieNL
Posts: 964
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by HansieNL »

I like the idea of having a button that shows how long Domoticz is up so I can use it in Dashticz.
I did find after trial and error some code that works... :o :shock:

Code: Select all

-- Domoticz Configuration
server="192.168.2.14"
port="8081"
switch="Uptime"

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local config=assert(io.popen('curl "http://'..server..':'..port..'/json.htm?type=command&param=getuptime"'))

local uptime = config:read('*all')
config:close()
local jsonGetUptime = json:decode(uptime)

days = jsonGetUptime.days
hours = jsonGetUptime.hours
minutes = jsonGetUptime.minutes
dzuptime = days.. " dagen " ..hours.. " uur " ..minutes.. " minuten"

commandArray = {}

  print('(Uptime) '.. dzuptime)
  table.insert(commandArray, { ['UpdateDevice'] = otherdevices_idx[switch] .. '|0|' .. dzuptime })

return commandArray
Image
Because I'm no programmer there can be a better solution. Be so kind to let me know.
Blah blah blah
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by waaren »

HansieNL wrote: Wednesday 19 September 2018 17:13 I like the idea of having a button that shows how long Domoticz is up so I can use it in Dashticz.
..
Because I'm no programmer there can be a better solution. Be so kind to let me know.
a dzVents solution

Code: Select all

return { 
    on = {   timer  =   {"every minute"}},  
                
    execute = function(dz)
        local uptimeTextDevice = dz.devices("Domoticz uptime")
        
        local minutes = dz.startTime.minutesAgo%60
        local days    = dz.startTime.daysAgo
        local hours   = dz.startTime.hoursAgo%24
        
        uptimeTextDevice.updateText(days .. " dagen, " .. hours .. " uur en " .. minutes .. " minuten"  )
    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
HansieNL
Posts: 964
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by HansieNL »

@waaren: Thanks!
Blah blah blah
Heelderpeel
Posts: 15
Joined: Sunday 26 April 2015 21:56
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Nederland
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by Heelderpeel »

Hello, I've been messing around with the script above, but can't really figure it out, maybe someone who has an idea what else I need to change.

What I want is 1 text device where all 3 my domoticz servers can be seen. I imagine the layout as follows.

Domoticz uptime (device name)
Master 2 days 2 hours 22 minutes (result)
Slave 1 2 days 2 hours 22 minutes (result)
Slave 2 2 days 2 hours 22 minutes (result)

Code: Select all

-- Domoticz Configuration
server1="192.168.1.100"
port1="8080"
server2="192.168.1.101"
port2="8080"
server3="192.168.1.102"
port3="8080"
switch="Domoticz uptime"

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local config=assert(io.popen('curl "http://'..server1..':'..port1..'/json.htm?type=command&param=getuptime"'))
local config=assert(io.popen('curl "http://'..server2..':'..port2..'/json.htm?type=command&param=getuptime"'))
local config=assert(io.popen('curl "http://'..server3..':'..port3..'/json.htm?type=command&param=getuptime"'))

local uptime = config:read('*all')
config:close()
local jsonGetUptime = json:decode(uptime)

days = jsonGetUptime.days
hours = jsonGetUptime.hours
minutes = jsonGetUptime.minutes
dzuptime = days.. " dagen " ..hours.. " uur " ..minutes.. " minuten"

commandArray = {}
 local m = os.date('%M')
 if (m % 5 == 0) then --Intervaltijd
  print('(Uptime) '.. dzuptime)
  print('(Uptime) '.. dzuptime)
  print('(Uptime) '.. dzuptime)
  table.insert(commandArray, { ['UpdateDevice'] = otherdevices_idx[switch] .. '|0|' .. dzuptime })
 end
return commandArray
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by waaren »

Heelderpeel wrote: Friday 12 June 2020 21:06 Hello, I've been messing around with the script above, but can't really figure it out, maybe someone who has an idea what else I need to change.
What I want is 1 text device where all 3 my domoticz servers can be seen. I imagine the layout as follows.
If you want to stick to classic Lua it could look like something as below.
Will probably fail if one of the systems or domoticz service is down.

Code: Select all

-- Domoticz Configuration

local uptimeString = ''
local uptimeSensor = 'Uptime'
local systems = 
        { 
            { 'master','192.168.1.100', '8080' },
            { 'slave1','192.168.1.101', '8080' },
            { 'slave2','192.168.1.102', '8080' },
        }    
            
local json = (loadfile 'scripts/lua/JSON.lua')()

for _, system in ipairs(systems) do
    local curl = "curl 'http://" .. system[2] .. ":"  .. system[3] .."/json.htm?type=command&param=getuptime'"
    -- print(curl)
    
    local config=assert(io.popen(curl))
    local uptime = config:read('*all')
    config:close()
    local jsonGetUptime = json:decode(uptime)

    days = jsonGetUptime.days
    hours = jsonGetUptime.hours
    minutes = jsonGetUptime.minutes
    uptimeString = uptimeString .. system[1] .. ' uptime: '  .. days.. ' dagen ' ..hours.. ' uur ' ..minutes.. ' minuten' .. '\n' 
end

uptimeString = uptimeString:sub(1, -2) -- remove trailing \n
-- print('(Uptime) '.. uptimeString)

commandArray = { ['UpdateDevice'] = otherdevices_idx[uptimeSensor] .. '|0|' .. uptimeString } 
return commandArray

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Heelderpeel
Posts: 15
Joined: Sunday 26 April 2015 21:56
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Nederland
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by Heelderpeel »

Who, that's fast, thank you very much for that. :) :) :)
This is basically for what I was looking for.
But you ask if I want to hold on to Lua, no not necessarily.
I found this and it worked for me.

If you say there is a better solution, yes please let me know.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by waaren »

Heelderpeel wrote: Saturday 13 June 2020 0:05 But you ask if I want to hold on to Lua, no not necessarily.
If you say there is a better solution, yes please let me know.
The potential issue with an approach like this with curl, is that if you don't get a reply because a system is down or domoticz is not responding that other scripts are no longer executed. Because the domoticz event system is single threaded such a situation will block all other event scripts.

dzVents offers multiple alternatives for this.

- To get the information from multiple systems as is needed to solve your request, you could have the systems all send there uptime based on the dzVents script I posted in this thread earlier via an openURL command (which is async and therefore cannot block the event system) to the central system. They would have the choice to send it to a domoticz uservar or to trigger a customEvent. In both cases a dzVents script on the central system would be triggered to collect the information and present it in the virtual text Sensor.
- Another way would be to update a textSensor on all systems and have dzVents on the master system to collect the data from these sensors; again via an async openURL command.

These dzVents based alternatives would require a bit more coding but with the advantage of added resilience against outages or other unforeseen problems.

btw. dzVents scripts are 100% Lua
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Heelderpeel
Posts: 15
Joined: Sunday 26 April 2015 21:56
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Nederland
Contact:

Re: Get Domoticz Uptime in Lua or via Json?

Post by Heelderpeel »

Okay, I initially tried your script on the slave and then wanted to make it visible with shared devices on the master but that didn't work, it didn't show up on the master. :(
Then I saw this script.
Anyway I am not a programmer and so I have to rely on google, ctrl-c and ctrl-v :roll: :D
So I really don't know how to do this and I don't want to bother people with that.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest