forEach attribute in .dump to file

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

Moderator: leecollings

Post Reply
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

forEach attribute in .dump to file

Post by DomoticaRob »

I want to make a textfile with all the attributes of a device.
I can print a .dump to the log with:

Code: Select all

return {
	on =    {
		devices =   {
			        103
	            	},
    	    },

	execute = function(domoticz, Rob1)

    if (Rob1.dump() ~= nil) then
        print('Dump: '..Rob1.dump())
    end

end 

}
I want to iterate through Rob1 and write every item to a textfile.

I know that it must be possible with

forEach

and

file = io.open()

and so on. But, I don't get it working.
I have struggled through a lot of topics and wiki's. I still don't get it.

Please help,

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

Re: forEach attribute in .dump to file

Post by waaren »

DomoticaRob wrote:I want to make a textfile with all the attributes of a device.
I can print a .dump to the log with:

Code: Select all

return {
	on =    {
		devices =   {
			        103
	            	},
    	    },

	execute = function(domoticz, Rob1)

    if (Rob1.dump() ~= nil) then
        print('Dump: '..Rob1.dump())
    end

end 

}
I want to iterate through Rob1 and write every item to a textfile.

I know that it must be possible with

forEach

and

file = io.open()

and so on. But, I don't get it working.
I have struggled through a lot of topics and wiki's. I still don't get it.

Please help,

Rob
If you configure domoticz to log to a file you can copy that file and keep the relevant lines.

Verstuurd vanaf mijn ONEPLUS A6003 met Tapatalk

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

Hey Waaren,

Thanks for your reply.
That is a way. But I want to do it with 'iteratie' and 'file.write' and so on. It is not only for this purpose, but also as learning moment for other situations.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by waaren »

DomoticaRob wrote: Saturday 15 December 2018 22:15 Hey Waaren,

Thanks for your reply.
That is a way. But I want to do it with 'iteratie' and 'file.write' and so on. It is not only for this purpose, but also as learning moment for other situations.
In that case you can use the code below. The dumpTable function used here is almost equal to what dzVents uses internally for the dump command. This and other Lua code snippets can be found in the dzVents/runtime directory

Code: Select all

return {
	on =    { devices =   { 103 }},

     execute = function(dz, item )
        
        local fileName  = "dump" .. item.name .. ".out_" .. os.date("%H%M%S" )
        local file      = io.open(fileName, "a")
     
        local function dumpTable(t, level)
            for attr, value in pairs(t) do
                if (type(value) ~= 'function') then
                    if (type(value) == 'table') then
                        io.write(level .. attr .. ':\n')
                        print(level .. attr )
                        dumpTable(value, level .. '    ')
                    else
                        io.write(level .. attr .. ': ' .. tostring(value) .. "\n")
                        print(level .. attr .. ': ' .. tostring(value) )
                    end
                else
                    io.write(level .. attr .. '()\n')
                    print(level .. attr .. '()')
                end
            end
        end
        
        io.output(file)
        io.write("-- Start of attributes dump " .. item.name .. "\n")
        dumpTable(item,"> ")
        io.write("-- End of attributes dump " .. item.name .. " \n")
        io.close(file)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

Super, super, super!

Thank you very much.

I do understand the code.

There is one last issue:

In FileZilla I can't copy the files to my Local site (Nas).

I have tried to change the file attributes. Still no downloading.

I wanted to place the screendumps from FileZilla in this reply, but in the tab Attachments I don't get 'upload file' anymore. Has that to do with the planned relocatoin of the Domoticz forum server?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by waaren »

DomoticaRob wrote: Sunday 16 December 2018 13:36 In FileZilla I can't copy the files to my Local site (Nas).
I have tried to change the file attributes. Still no downloading.
I wanted to place the screendumps from FileZilla in this reply, but in the tab Attachments I don't get 'upload file' anymore. Has that to do with the planned relocatoin of the Domoticz forum server?
Sorry no experience with fileZilla. Better to open a new topic for that problem?
For filecopy between RPI's and synology I use rsync and between RPI's and desktop WinSCP
I still see the button Add files but also the error "Sorry, the board attachment quota has been reached." on the yellow triangle with the exclamation mark
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

I see that for these files the owner is: 'root root'.
Other files have as owner: 'pi pi'. These files I can download.

How/where can I change that?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by waaren »

On my system the owner of these files is the user executing the domoticz binary.
To change this give the command

Code: Select all

sudo chown pi:pi fileName
on the OS level
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

Hey Waaren,

It seems that the code below here has a strange sorting result. If I run the script a few times for the same devide I get every time the same result, but every time in a different sorting order, probably no order at all?
Is there a way in the script that I can sort it?

Code: Select all

return {
	on =    { devices =   { 103 }},

     execute = function(dz, item )
        
        local fileName  = "dump" .. item.name .. ".out_" .. os.date("%H%M%S" )
        local file      = io.open(fileName, "a")
     
        local function dumpTable(t, level)
            for attr, value in pairs(t) do
                if (type(value) ~= 'function') then
                    if (type(value) == 'table') then
                        io.write(level .. attr .. ':\n')
                        print(level .. attr )
                        dumpTable(value, level .. '    ')
                    else
                        io.write(level .. attr .. ': ' .. tostring(value) .. "\n")
                        print(level .. attr .. ': ' .. tostring(value) )
                    end
                else
                    io.write(level .. attr .. '()\n')
                    print(level .. attr .. '()')
                end
            end
        end
        
        io.output(file)
        io.write("-- Start of attributes dump " .. item.name .. "\n")
        dumpTable(item,"> ")
        io.write("-- End of attributes dump " .. item.name .. " \n")
        io.close(file)
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by waaren »

DomoticaRob wrote: Wednesday 19 December 2018 20:10 It seems that the code below here has a strange sorting result. If I run the script a few times for the same device I get every time the same result, but every time in a different sorting order, probably no order at all?
Is there a way in the script that I can sort it?
Lua tables / arrays are unsorted. There is always a way to sort but for the attributes of a device ( effectively a multilevel table ) it is too complicated for my Lua skills.
If you only want to alphabetically sort the file you could use the sort function on the os level. A call to os.execute("sort -b " .. file .. " > " .. file .. "_sorted" ) would do the trick and produce something like below.
Spoiler: show

Code: Select all

>         1: 0
>     1: 0
>     1: Switch device adapter
> active: true
> _adapters:
> armAway()
> armHome()
>     baseType: device
> baseType: device
>     batteryLevel: 255
> bState: true
> cancelQueuedCommands()
>     changed: true
> changed: true
> close()
>     compare()
>     current:
>     data:
> _data:
>         day: 19
>     day: 19
>     dayAbbrOfWeek: wed
>     daysAgo: 0
>     dDate: 1545247583
> decreaseBrightness()
>     description: MaxOnTime:60
> description: MaxOnTime:60
> deviceId: 011FE0EE
>     deviceID: 011FE0EE
> deviceSubType: AC
>     deviceType: Light/Switch
> deviceType: Light/Switch
> dimTo()
> disarm()
> dump()
>         fileExists()
>         fromJSON()
>     getISO()
> hardwareId: 5
>         hardwareID: 5
> hardwareID: 5
>         hardwareName: RFLink
> hardwareName: RFLink
>         hardwareType: RFLink Gateway USB
> hardwareType: RFLink Gateway USB
>         hardwareTypeValue: 46
> hardwareTypeValue: 46
>         hour: 20
>     hour: 20
>     hoursAgo: 0
>         icon: lightbulb
> icon: lightbulb
>     id: 535
> id: 535
> idx: 535
> increaseBrightness()
> isDevice: true
>         isdst: false
>     isdst: false
> isGroup: false
> isHTTPResponse: false
> isScene: false
> isSecurity: false
> isTimer: false
>     isToday: true
>     isUTC: false
> isVariable: false
> kodiExecuteAddOn()
> kodiPause()
> kodiPlay()
> kodiPlayFavorites()
> kodiPlayPlaylist()
> kodiSetVolume()
> kodiStop()
> kodiSwitchOff()
>     lastLevel: 0
> lastLevel: 0
> lastUpdate:
>     lastUpdate: 2018-12-19 20:26:23
> level: 0
>         levelVal: 0
> levelVal: 0
>         log()
>         LOG_DEBUG: 4
>         LOG_ERROR: 1
>         LOG_FORCE: 0.5
>         LOG_INFO: 3
>         LOG_MODULE_EXEC_INFO: 2
>     matchesRule()
>         maxDimLevel: 100
> maxDimLevel: 100
>     milliseconds: 0
>     milliSeconds: 0
>     millisecondsAgo: 2053
>         min: 26
>     min: 26
>     minutes: 26
>     minutesAgo: 0
>         month: 12
>     month: 12
>     msAgo: 2053
>         _nValue: 1
> _nValue: 1
> nValue: 1
> onkyoEISCPCommand()
> open()
>         osExecute()
> pause()
> play()
> playFavorites()
>         print()
>         protected: false
> protected: false
>     raw: 2018-12-19 20:26:23
>     rawData:
> rawData:
>     rawDate: 2018-12-19
>     rawTime: 20:26:23
>         rgbToHSB()
>     ruleIsAfterCivilTwilightEnd()
>     ruleIsAfterCivilTwilightStart()
>     ruleIsAfterSunrise()
>     ruleIsAfterSunset()
>     ruleIsAtCivilDayTime()
>     ruleIsAtCivilNight()
>     ruleIsAtCivilTwilightEnd()
>     ruleIsAtCivilTwilightStart()
>     ruleIsAtDayTime()
>     ruleIsAtNight()
>     ruleIsAtSunrise()
>     ruleIsAtSunset()
>     ruleIsBeforeCivilTwilightEnd()
>     ruleIsBeforeCivilTwilightStart()
>     ruleIsBeforeSunrise()
>     ruleIsBeforeSunset()
>     ruleIsInWeek()
>     ruleIsOnDate()
>     ruleIsOnDay()
>     ruleMatchesBetweenRange()
>     ruleMatchesHourSpecification()
>     ruleMatchesMinuteSpecification()
>     ruleMatchesTime()
>     ruleMatchesTimeRange()
>     sec: 23
>         sec: 25
>     seconds: 23
>     secondsAgo: 2
>     secondsSinceMidnight: 73583
> setDiscoMode()
> setHotWater()
> setKelvin()
> setNightMode()
> setRGB()
> setState()
> setVolume()
> setWhiteMode()
>     signalLevel: 12
> signalLevel: 12
> startPlaylist()
>         _state: On
> _state: On
> state: On
> stop()
>     subType: AC
> switchOff()
> switchOn()
> switchSelector()
>     switchType: On/Off
> switchType: On/Off
>     switchTypeValue: 0
> switchTypeValue: 0
>     timedOut: false
> timedOut: false
> toggleSwitch()
>         toJSON()
>         unit: 12
> unit: 12
> update()
> updateAirQuality()
> updateAlertSensor()
> updateBarometer()
> updateCounter()
> updateCustomSensor()
> updateDistance()
> updateElectricity()
> updateGas()
> updateHumidity()
> updateLux()
> updateMode()
> updateP1()
> updatePercentage()
> updatePressure()
> updateRadiation()
> updateRain()
> updateSetPoint()
> updateSoilMoisture()
> updateSoundLevel()
> updateTempBaro()
> updateTemperature()
> updateTempHum()
> updateTempHumBaro()
> updateText()
> updateUV()
> updateVisibility()
> updateVoltage()
> updateWaterflow()
> updateWeight()
> updateWetness()
> updateWind()
> updateYouless()
>         urlEncode()
>         usedByCamera: false
> usedByCamera: false
>     utils:
>         wday: 4
>     wday: 4
>     week: 51
>         yday: 353
>     yday: 353
>         year: 2018
>     year: 2018

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

Can I just place:

Code: Select all

os.execute("sort -b " .. fileName .. " > " .. fileName.. "_sorted")
at the end of my script, or must it be something like:

Code: Select all

local result = os.execute("sort -b " .. fileName .. " > " .. fileName.. "_sorted")
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by waaren »

DomoticaRob wrote: Wednesday 19 December 2018 22:23 Can I just place:

Code: Select all

os.execute("sort -b " .. fileName .. " > " .. fileName.. "_sorted")
at the end of my script, or must it be something like:

Code: Select all

local result = os.execute("sort -b " .. fileName .. " > " .. fileName.. "_sorted")
Both will work. If you don't want to do something with the result then it makes not much sense to store the result in this var.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: forEach attribute in .dump to file

Post by DomoticaRob »

Thanks Waaren,

It works. The only problem is that the attributes and the tables will be mixed up.
I'm gonna make a macro in Excel to do this job for me.
Finally I want to have a Excel file with all my devices with all the attributes.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest