Page 1 of 1

nil value for solarnoon

Posted: Sunday 09 March 2025 17:54
by jberinga
I have this simple script to get the solarnoon time:
Spoiler: show
return {
on = {
timer = {
'every minute' -- Adjust as needed (e.g., 'every 5 minutes', 'at 10:00')
},
devices = { 'Test' }
},
execute = function(domoticz, timer)

if
domoticz.devices('Test').state == 'On'
then
-- Example: log the solarnoon time.
local solarnoonTime = domoticz.utils.solarNoon()
domoticz.log("Solar noon time(unix): " .. solarnoonTime, domoticz.LOG_INFO)
-- To convert that unix time to a readable time.
domoticz.log("Solar noon time(readable): " .. os.date("%Y-%m-%d %H:%M:%S", solarnoonTime), domoticz.LOG_INFO)
end
end
}
But in the log I get this error:
Error: dzVents: ...userdata/scripts/dzVents/generated_scripts/SolarNoon.lua:14: attempt to call a nil value (field 'solarNoon')

What am I doing wrong here?

The goal is to update a text device with the solarnoon time, but that also fails with the nil value error....

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 19:34
by HvdW
Here is another message from CoPilot.

To calculate solar noon in Dzvents, you can use the following Lua script. Solar noon is the time of day when the sun is at its highest point in the sky. This script calculates the solar noon based on your geographical location (latitude and longitude).

Here's an example of how you can do this:

Code: Select all

-- Function to calculate solar noon
function calculate_solar_noon(latitude, longitude)
    local now = os.time()
    local day_of_year = os.date("%j", now)
    local timezone_offset = os.date("%z", now) / 100

    -- Calculate the approximate time of solar noon
    local solar_noon_approx = 12 - (longitude / 15) - timezone_offset

    -- Calculate the equation of time (EoT)
    local B = (360 / 365) * (day_of_year - 81)
    local EoT = 9.87 * math.sin(math.rad(2 * B)) - 7.53 * math.cos(math.rad(B)) - 1.5 * math.sin(math.rad(B))

    -- Calculate the solar noon
    local solar_noon = solar_noon_approx + (EoT / 60)

    return solar_noon
end

-- Example usage
local latitude = 52.0 -- Replace with your latitude
local longitude = 5.0 -- Replace with your longitude
local solar_noon = calculate_solar_noon(latitude, longitude)

print("Solar noon: " .. solar_noon .. " hours")
In this example:

latitude and longitude are your geographical coordinates.

The script calculates the day of the year and the timezone offset.

It then calculates the approximate time of solar noon and the equation of time (EoT).

Finally, it calculates the solar noon and prints the result.

Replace the latitude and longitude values with your actual coordinates, and the script will calculate the solar noon for your location.

Feel free to adjust the script to suit your needs, and let me know if you need further assistance!

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 19:42
by waltervl
HvdW wrote: Sunday 09 March 2025 19:34 Here is another message from CoPilot.
......
This is NOT helping the topic starter....
This user wants to use a standard dzvents function, gets an error and you tell him to do something complety different with the help of AI crap programming functions......

Better help him finding what is going wrong...

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 19:56
by waltervl
jberinga wrote: Sunday 09 March 2025 17:54 I have this simple script to get the solarnoon time:

But in the log I get this error:
Error: dzVents: ...userdata/scripts/dzVents/generated_scripts/SolarNoon.lua:14: attempt to call a nil value (field 'solarNoon')

What am I doing wrong here?

The goal is to update a text device with the solarnoon time, but that also fails with the nil value error....
Where did you read there is a domoticz.utils.solarNoon() function? As it seems not to be there.
There is a function

Code: Select all

domoticz.time.solarnoonInMinutes()
: Number. Minutes from midnight until solarnoon (based on the location in your settings)

Then you have to convert the minutes to hours:min to get the solarnoon time.
This can be done by eg

Code: Select all

local hours = floor(minutes/60)
local minutes = floor(mod(minutes,60))

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 21:23
by HvdW
waltervl wrote: Sunday 09 March 2025 19:42 This is NOT helping the topic starter....
This user wants to use a standard dzvents function, gets an error and you tell him to do something complety different with the help of AI crap programming functions......

Better help him finding what is going wrong...
Maybe you want people to use pen and paper instead of a calculator.

Code: Select all

domoticz.time.solarnoonInMinutes()
does not need the (), it can be omitted.

Anyway....

Code: Select all

return {
    on = {
        timer = { 'every 1 minutes' },  -- Voer het script elke minuut uit
    },
    logging = {
        level = domoticz.LOG_INFO,     -- Log niveau instellen op INFO
        marker = 'Solar Noon Logger'   -- Marker voor de log
    },
    execute = function(domoticz, triggeredItem)
        -- Bereken de solar noon in minuten sinds middernacht
        local solarNoonInMinutes = domoticz.time.solarnoonInMinutes

        -- Log de uitkomst
        domoticz.log('Solar Noon in minuten sinds middernacht: ' .. tostring(solarNoonInMinutes), domoticz.LOG_INFO)
    end
}
This one fits the frame I hope.

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 21:25
by jberinga
Well... I wasn't using Copilot.... but Gemini.... ;)

I am very skeptical about using AI and also for creating scripts, but I wanted to give it a try especially after I didn't get things working without AI.
Gemini was so convinced that solarNoon was a subset of domoticz.utils, the only conclusion I came to was that it was something I did wrong...

Thank you both for the suggestions, I will give it a try!

For those who are interested, this is the (incorrect) reaction of Gemini when I asked for a list of domoticz.utils:
Spoiler: show
The `domoticz.utils` table in dzVents provides a collection of utility functions that can be helpful in your Domoticz scripting. Here's a list of commonly used functions:

* **`domoticz.utils.openURL(url)`**:
* Opens a URL. This can be used to send HTTP requests to external services.
* **`domoticz.utils.httpGET(url)`**:
* Performs an HTTP GET request and returns the response.
* **`domoticz.utils.httpPOST(url, data)`**:
* Performs an HTTP POST request with the provided data.
* **`domoticz.utils.toJSON(table)`**:
* Converts a Lua table to a JSON string.
* **`domoticz.utils.fromJSON(jsonString)`**:
* Parses a JSON string and returns a Lua table.
* **`domoticz.utils.base64Encode(string)`**:
* Encodes a string using Base64.
* **`domoticz.utils.base64Decode(string)`**:
* Decodes a Base64-encoded string.
* **`domoticz.utils.solarNoon(latitude, longitude)`**:
* Calculates the solar noon time (Unix timestamp) for the given latitude and longitude.
* **`domoticz.utils.sunrise(latitude, longitude, date)`**:
* Calculates the sunrise time (Unix timestamp) for the given latitude, longitude, and optional date. If date is omitted, it uses the current date.
* **`domoticz.utils.sunset(latitude, longitude, date)`**:
* Calculates the sunset time (Unix timestamp) for the given latitude, longitude, and optional date. If date is omitted, it uses the current date.
* **`domoticz.utils.daylength(latitude, longitude, date)`**:
* Calculates the day length (in seconds) for the given latitude, longitude, and optional date. If date is omitted, it uses the current date.
* **`domoticz.utils.moonPhase()`**:
* Returns the current moon phase.
* **`domoticz.utils.moonRise(latitude,longitude,date)`**:
* Returns the moon rise time.
* **`domoticz.utils.moonSet(latitude,longitude,date)`**:
* Returns the moon set time.

**Important Considerations:**

* The availability and behavior of these functions might vary slightly depending on your Domoticz version.
* When working with solar calculations, ensure that your Domoticz location settings (latitude and longitude) are accurate.
* When using http requests, be mindful of the security implications of sending and receiving data over the network.
* Always consult the Domoticz Wiki, and forums for the most up to date information.
Or I misunderstood this reaction

Re: nil value for solarnoon

Posted: Sunday 09 March 2025 21:30
by HvdW
jberinga wrote: Sunday 09 March 2025 21:25 Well... I wasn't using Copilot.... but Gemini.... ;)
Don't trust.
Test and ask along.
AI is just another tool that makes a lot of mistakes.
It can be very helpfull though.

Re: nil value for solarnoon

Posted: Monday 10 March 2025 18:04
by waltervl
For dzvents just use the wiki and search this forum, loads of examples here.

Dont trust any AI on dzvents as it is a niche and there is not a lot of good data to train the AI.
Just pure Lua is fine to ask to AI as it is far more common.

Re: nil value for solarnoon

Posted: Sunday 23 March 2025 14:38
by jberinga
Thanks again for your help!

Finally found time to get it working:

Code: Select all

return {
    on = {
        timer = { 'at sunrise' },  -- Voer het script bij zonsopgang uit
    },
    logging = {
        level = domoticz.LOG_INFO,     -- Log niveau instellen op INFO
        marker = 'Solar Noon Logger'   -- Marker voor de log
    },
    execute = function(domoticz, triggeredItem)
        -- Bereken de solar noon in minuten sinds middernacht > naar HH:MM
        local solarNoonInMinutes = domoticz.time.solarnoonInMinutes
        local solarHour = math.floor(solarNoonInMinutes/60)
        local solarMinute = math.floor(solarNoonInMinutes-(solarHour*60))
        --Update tekstveld met de uitkomst
        domoticz.devices('SolarNoon').updateText(tostring(solarHour) .. ':' .. tostring(solarMinute))
        -- Log de uitkomst
        domoticz.log('Solar Noon in minuten sinds middernacht: ' .. tostring(solarNoonInMinutes), domoticz.LOG_INFO)
        domoticz.log('Solar Noon is: ' .. tostring(solarHour) .. ':' .. tostring(solarMinute), domoticz.LOG_INFO)
    end
}

Re: nil value for solarnoon

Posted: Sunday 23 March 2025 15:33
by habahabahaba
a little bit optimized:

Code: Select all


        local solarNoonInMinutes = domoticz.time.solarnoonInMinutes
        local solarNoonTime = domoticz.time.timestampToDate(domoticz.time.dateToTimestamp(os.date("%Y-%m-%d 00:00")) + solarNoonInMinutes * 60, '%Y-%m-%d time')
        domoticz.log('todaySolarNoonTime: '.. solarNoonTime, domoticz.LOG_INFO)


Re: nil value for solarnoon

Posted: Sunday 06 April 2025 16:56
by solarboy
I am trying to get a time value for sunset or time until sunset but I can't find anything documented.

"domoticz.time.sunset" and "domoticz.time.sunsetInMinutes" do not work.

Is there anywhere this is written down ? (I did check the dzVents wiki but found nothing)

sunsetInMinutes gave this error.
Error: dzVents: ...icz/scripts/dzVents/generated_scripts/Solar Excess 2.lua:83: bad argument #2 to 'difftime' (number expected, got string)

Re: nil value for solarnoon

Posted: Sunday 06 April 2025 17:52
by jvdz
Show the whole script so we can check what the reason might be as sunriseInMinutes & sunsetInMinutes functions do exist.

Re: nil value for solarnoon

Posted: Sunday 06 April 2025 20:13
by solarboy
jvdz wrote: Sunday 06 April 2025 17:52 Show the whole script so we can check what the reason might be as sunriseInMinutes & sunsetInMinutes functions do exist.
Apologies, discovered a typo, all good now.