Page 2 of 3

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 27 September 2024 16:27
by Kedi
You could replace the getWindDirectionString function with this solution, found it somewhere on the Internet.

Code: Select all

-- Directions is 1 of 16 Compose Rose Angles
local CompassRose16Table = {  [0] = "N", [1] = "NNE", [2] = "NE", [3] = "ENE",
                              [4] = "E", [5] = "ESE" , [6] = "SE", [7] = "SSE",
                              [8] = "S", [9] = "SSW", [10] = "SW", [11] = "WSW",
                              [12] = "W", [13] = "WNW", [14] = "NW", [15] = "NNW",
                              [16] = "N"
                            }
function directionText(directionDeg)
    return CompassRose16Table[math.floor((directionDeg/22.5) +0.5)]
end
And instead of all the idx numbers you could also use the device name like:

Code: Select all

local wl_alarm_idx = 'Weather Alarm'    --idx of the custom Alert device for Weather alarm.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 27 September 2024 16:55
by janpep
Of course there are always other ways to get things done. :-). The first you found, or shorter, or more readable and clear, or supposed to be faster.

For the wind directions I have a centrally placed function in global_data, because it is accessed from multiple scripts and works properly according to the clear definition I found.
For accessing devices I strongly prefer to work with the idx and not with the names!
The idx does not change. That's why I once revised all my scripts and got rid of the names.
If you use that, you are free to rename devices without having to check all your scripts again.

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 11:19
by JanPedaal
@janpep, many thanks for your work on this script! Could you share your global_data function for the wind directions?

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 12:13
by janpep
The script has a reference to _h.getDirectionfromDegree in global_data
Compared to what Kedi showed, this is more code, but for me easier to understand and working fine for a long time, so I keep it this way.

Code: Select all

		------------------------
		-- Used in dt-OpenMeteo, t-Weerlive, t-Airplanes,
		getDirectionfromDegree = function( degrees )
			-- To calculate the degrees.
			local directionString = ''
			-- When string is not in Enlish, the icon does not appear in wind device!
			if degrees >= 0 and degrees < 11.25 then directionString = 'N' end
			if degrees >= 11.25 and degrees < 33.75 then directionString = 'NNE' end
			if degrees >= 33.75 and degrees < 56.25 then directionString = 'NE' end
			if degrees >= 56.25 and degrees < 78.75 then directionString = 'ENE' end
			if degrees >= 78.75 and degrees < 101.25 then directionString = 'E' end
			if degrees >= 101.25 and degrees < 123.75 then directionString = 'ESE' end
			if degrees >= 123.75 and degrees < 146.25 then directionString = 'SE' end
			if degrees >= 146.25 and degrees < 168.75 then directionString = 'SSE' end
			if degrees >= 168.75 and degrees < 191.25 then directionString = 'S' end
			if degrees >= 191.25 and degrees < 213.75 then directionString = 'SSW' end
			if degrees >= 213.75 and degrees < 236.25 then directionString = 'SW' end
			if degrees >= 236.25 and degrees < 258.75 then directionString = 'WSW' end
			if degrees >= 258.75 and degrees < 281.25 then directionString = 'W' end
			if degrees >= 281.25 and degrees < 303.75 then directionString = 'WNW' end
			if degrees >= 303.75 and degrees < 326.25 then directionString = 'NW' end
			if degrees >= 326.25 and degrees < 348.75  then directionString = 'NNW' end
			if degrees >= 348.75 and degrees <= 360 then directionString = 'N' end
			return directionString
		end,
The script has also a reference to _h.trimLRspaces in global_data.

Code: Select all

		------------------------
		-- Used in t-Weerlive,
	   trimLRspaces = function( s )
			return ( s:gsub( "^%s*(.-)%s*$", "%1" ) )
		end,
		------------------------
PS. In global_data these functions are within the helpers part

Code: Select all

	-- Global helper functions
	helpers = {
	--
	}
	

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 14:24
by JanPedaal
Thanks, I managed to get most of the script working. I still have issues addressing the global data, but most of my problems disappeared when I added

Code: Select all

local dz = domoticz
to the script. Not sure if this was left out on purpose, but I need it to fill most of the devices with info.

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 16:20
by Kedi
janpep wrote: Monday 30 September 2024 12:13

Code: Select all

		------------------------
		-- Used in dt-OpenMeteo, t-Weerlive, t-Airplanes,
		getDirectionfromDegree = function( degrees )
			-- To calculate the degrees.
			local directionString = ''
			-- When string is not in Enlish, the icon does not appear in wind device!
			if degrees >= 0 and degrees < 11.25 then directionString = 'N' end
			if degrees >= 11.25 and degrees < 33.75 then directionString = 'NNE' end
			if degrees >= 33.75 and degrees < 56.25 then directionString = 'NE' end
			if degrees >= 56.25 and degrees < 78.75 then directionString = 'ENE' end
			if degrees >= 78.75 and degrees < 101.25 then directionString = 'E' end
			if degrees >= 101.25 and degrees < 123.75 then directionString = 'ESE' end
			if degrees >= 123.75 and degrees < 146.25 then directionString = 'SE' end
			if degrees >= 146.25 and degrees < 168.75 then directionString = 'SSE' end
			if degrees >= 168.75 and degrees < 191.25 then directionString = 'S' end
			if degrees >= 191.25 and degrees < 213.75 then directionString = 'SSW' end
			if degrees >= 213.75 and degrees < 236.25 then directionString = 'SW' end
			if degrees >= 236.25 and degrees < 258.75 then directionString = 'WSW' end
			if degrees >= 258.75 and degrees < 281.25 then directionString = 'W' end
			if degrees >= 281.25 and degrees < 303.75 then directionString = 'WNW' end
			if degrees >= 303.75 and degrees < 326.25 then directionString = 'NW' end
			if degrees >= 326.25 and degrees < 348.75  then directionString = 'NNW' end
			if degrees >= 348.75 and degrees <= 360 then directionString = 'N' end
			return directionString
		end,
I understand that you want to use this because it is easier to understand.
Form a programmers point of view you could optimize when you use 'elseif' instead of the 'if' nr. 2 - 17

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 16:27
by janpep
Kedi wrote: Monday 30 September 2024 16:20 Form a programmers point of view you could optimize when you use 'elseif' instead of the 'if' nr. 2 - 17
You are right.

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 16:33
by janpep
JanPedaal wrote: Monday 30 September 2024 14:24 Thanks, I managed to get most of the script working. I still have issues addressing the global data
- I do not understand about the dz, because dz is set in line 69 for domoticz, so it should not be needed to set it again.
- In line 71 _h is set to point to the dz.helpers in global_data, in wich you placed the functions.
In the mentioned functions itself dz or domoticz is not used, so I do not know at what point you still have a problem.
Not sure if this was left out on purpose, but I need it to fill most of the devices with info.
What do you mean by "left out" and "I need to fill most of the devices with info"?
May be I do not understand it right, but when the devices you want are created, and you have set the correponding IDXes in line 32-44, and other settings in line 46-50, the content should fill when the scipt runs.

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 30 September 2024 17:19
by janpep
janpep wrote: Monday 30 September 2024 16:27 You are right.
So here is the modified function.

Code: Select all

		------------------------
		-- Used in dt-OpenMeteo, t-Weerlive, t-Airplanes,
		getDirectionfromDegree = function( degrees )
			-- To calculate the degrees.
			local directionString = ''
			-- When string is not in Enlish, the icon does not appear!
			if degrees >= 0 and degrees < 11.25 then directionString = 'N' 
            elseif degrees >= 11.25 and degrees < 33.75 then directionString = 'NNE'
            elseif degrees >= 33.75 and degrees < 56.25 then directionString = 'NE'
            elseif degrees >= 56.25 and degrees < 78.75 then directionString = 'ENE'
            elseif degrees >= 78.75 and degrees < 101.25 then directionString = 'E'
            elseif degrees >= 101.25 and degrees < 123.75 then directionString = 'ESE'
            elseif degrees >= 123.75 and degrees < 146.25 then directionString = 'SE'
            elseif degrees >= 146.25 and degrees < 168.75 then directionString = 'SSE'
            elseif degrees >= 168.75 and degrees < 191.25 then directionString = 'S'
            elseif degrees >= 191.25 and degrees < 213.75 then directionString = 'SSW'
            elseif degrees >= 213.75 and degrees < 236.25 then directionString = 'SW'
            elseif degrees >= 236.25 and degrees < 258.75 then directionString = 'WSW'
            elseif degrees >= 258.75 and degrees < 281.25 then directionString = 'W'
            elseif degrees >= 281.25 and degrees < 303.75 then directionString = 'WNW'
            elseif degrees >= 303.75 and degrees < 326.25 then directionString = 'NW'
            elseif degrees >= 326.25 and degrees < 348.75  then directionString = 'NNW'
            elseif degrees >= 348.75 and degrees <= 360 then directionString = 'N' end
			return directionString
		end,

Re: Configurable 'KNMI Weerlive' script

Posted: Tuesday 01 October 2024 15:01
by JanPedaal
janpep wrote: Monday 30 September 2024 16:33 - I do not understand about the dz, because dz is set in line 69 for domoticz, so it should not be needed to set it again.
- In line 71 _h is set to point to the dz.helpers in global_data, in wich you placed the functions.
In the mentioned functions itself dz or domoticz is not used, so I do not know at what point you still have a problem.
I copied the script from this forum, and "local dz = domoticz" wasn't present. Maybe you declare that somewhere else? I assume you don't mean the "execute = function()" line? You use "dz" there as an input, which means it is declared somewhere earlier (global script perhaps?).

I had a problem with the global_data script, which prevented the global functions (helpers) to work. But I've solved that now (global_data was defined as a lua script instead of a dzVents script) and the _h is working now.
What do you mean by "left out" and "I need to fill most of the devices with info"?
May be I do not understand it right, but when the devices you want are created, and you have set the correponding IDXes in line 32-44, and other settings in line 46-50, the content should fill when the scipt runs.
If I do not add "local dz=domoticz" before the execute block, the script won't run correctly.

Anyhow, everything is now working as it should :) Thanks for your help.

Re: Configurable 'KNMI Weerlive' script

Posted: Tuesday 01 October 2024 16:44
by janpep
JanPedaal wrote: Tuesday 01 October 2024 15:01 If I do not add "local dz=domoticz" before the execute block, the script won't run correctly.

Anyhow, everything is now working as it should :) Thanks for your help.
Yes, I was pointing to the execute line.
As you can see I nowhere declare a local dz = domoticz. This domoticz is passed as the first argument to dz in the execute line and used from there.
As explained in the documentation here
Anyhow, happy for you that you got it working now.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 8:55
by Kedi
For some reason those icons cannot be read by Domoticz.
I get an error. I saw that there is a zero bytes file in the zipfiles.
Also the other files are 50x50 pixels.
https://www.domoticz.com/wiki/Custom_ic ... binterface

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 9:17
by janpep
Kedi wrote: Friday 04 October 2024 8:55 For some reason those icons cannot be read by Domoticz.
Strange, that you get an error.
As far as I remember they are created at https://www.domoticz.com/icon-creator/. I use them myself.
I will have a look at it later and let you know.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 12:32
by janpep
janpep wrote: Friday 04 October 2024 9:17 I will have a look at it later and let you know.
Just looked at it and did a quick test.
1. I did not count the pixels and filesize of the files in the ZIP that is created by the mentioned icon generator, but I can confirm now that it generates the icons 50 x 50 pixels. Nevertheless that does not seem a problem, as it is working fine here.
2. I can confirm that the ON and OFF images that are created have content, as can be seen in the posted example. The other image is indeed 0 kb. I did not notice before. See that this is the case for all of them. I quess this file is not used in the current situation, because I am not aware that miss anything.
3. I thougbht that may be I imported these files in previous version and something has changed meanwhile.
So for testing I added one of the files for a second time after remaning. This goes without a problem.

My preliminary conclusion is that the 50x50 pixels and the mentioned empty png file are not a problem. At least not for me.

What should perhaps be mentioned is that I had a problem uploading icon files several times. It turned out that I had to do a refresh beforehand. However, this is unrelated to the files in question.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 12:44
by Kedi
I am working with beta.
And I kust checked with my docker test system.
It does not load, and the error is about the 0 bytes file is too small.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 13:05
by janpep
OK. Then it seems that something has changed in this area in your newer beta version.

I remember that I was a bit confused by the fact that you need and ON and OFF file. Because for our text devices that makes no sense at all! :-) So I put the same file twice in the generator. Then found out that it also generates this thirth file (that now seems to be 0 kb). I do not know why it is needed, nor where it is used for.
I did see where the content from icons.txt went and renamed that auto generated text.
Perhaps you can replace the 0 kb file and see what happens then.

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 15:55
by Kedi
The small icons are used in the floor/roomplans

Re: Configurable 'KNMI Weerlive' script

Posted: Friday 04 October 2024 16:22
by janpep
Kedi wrote: Friday 04 October 2024 15:55 The small icons are used in the floor/roomplans
OK. Thanks. That explains why I did not miss anything, because I do not use this.
Obvious that there is something wrong in the icon generator that produced these 0 kb files.
May be I can try to regenerate these "floor icons", but it's not my hobby to endlessly work with pictures. That's usually bad for my mood. :-)

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 07 October 2024 11:44
by janpep
@Kedi,
I recreated the small 16x16 icons in a batch with IrfanView 64.
Then I replaced the ZIP file in the second post of this topic, with the new files.
NB. I did not touch all the 48 pixel files, because this just seems to work (at least here).
I would like to hear if this works in your beta version.

Re: Configurable 'KNMI Weerlive' script

Posted: Monday 07 October 2024 18:39
by Kedi
Yes, those can be imported.
But I only use icons with transparent background, for obvious reason.
I have to convert all those icons when I have some spare time.
B.t.w. the first I imported was 'bewolkt' and that one was 50x50 pixels.
It works but not the right format.