Real-time solar data : azimuth, Altitude, Lux sensor...

Post Reply
jmleglise
Posts: 193
Joined: Tuesday 13 January 2015 0:27
Target OS: Raspberry Pi
Domoticz version: V3.5113
Location: FRANCE
Contact:

Real-time solar data : azimuth, Altitude, Lux sensor...

Post by jmleglise » Tuesday 19 January 2016 23:23

This script is documented in the wiki : http://www.domoticz.com/wiki/Real-time_ ... _sensor...

Edit 19 March 2016 - V1.4 - Take the last version of this script in the wiki. http://www.domoticz.com/wiki/Real-time_ ... structions

Edit 3 sept 2017 - V2 - The script is now translated in dzevent framework http://www.domoticz.com/forum/viewtopic ... 59&t=19220

------
Hi everyone,

This LUA script calculates in real-time a lot of usefull solar data without any hardware sensor. The result is very satisfactory. I hope you will enjoy it.

Data :
Azimuth : Angle between the Sun and the north, in degree. (north vector and the perpendicular projection of the sun down onto the horizon)
Altitude : Angle of the Sun with the horizon, in degree.
Lux : radiation of the sun with real time cloud layer
and many more

The calculation is based on the theoretical radiations of the sun according to its location in the sky, calculated according to the time and the number of day in the year, plus the real-time cloud layer and the real-time weather (pressure) :
- Geographical address
- Altitude of the geographical point
- Atmospheric pressure of the place / from api.wunderground.com
- The cloudiness in Octa (1/8) / from ogimet.com

-- What is it For ? ------------------------------------------------------
Manage exactly roller blinds and venetian blind. both to optimise the heating and to prevent dazzle.
Manage your light according to the Luminosity
Use civil twilight dawn and dusk (-6 deg altitude) instead of sunrise and sunset.
Some calculation about photovoltaic production ?
Automate the heating of swimming pool
Personally I use a roller blind to wake up instead of an alarm clock, with a progressive opening of the roller blind to prevent dazzle.

-- History ------------------------------------------------------
I have improved the huge work of Sébastien Joly (on an another automation system) that Neutrino has migrated to Domoticz.

I deeply encourage you to visit the very interesting, original post in French : http://www.domotique-info.fr/2015/09/ou ... ecenter-2/
And the others documentations and source of the formulae :
http://www.plevenon-meteo.info/techniqu ... ement.html
http://herve.silve.pagesperso-orange.fr/solaire.htm
https://en.wikipedia.org/wiki/Azimuth
https://en.wikipedia.org/wiki/Dawn
There was even already a first implementation of these formulae here http://www.domoticz.com/forum/viewtopic.php?f=23&t=6793, but I choose to complete the other implementation.
Last edited by jmleglise on Sunday 03 September 2017 21:30, edited 5 times in total.
Raspberry Pi2 + RFXTRX433E + KMTronic USB relay card
Blinds Somfy RTS, Blinds Bubendorff, Portal Somfy Evolvia 400, IP Video Doorbell ATZ-DBV01P, chacon IO, Oregon THN132ES, THN122N, Chinese PIR sensor PT2262, Bluetooth low energy dongle, iBeacon

User avatar
Westcott
Posts: 548
Joined: Tuesday 09 December 2014 18:04
Target OS: Raspberry Pi
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by Westcott » Wednesday 20 January 2016 20:57

Hi Jmleglise,

Thanks for posting, it looks fascinating.
But, how do you use it with Domoticz?
It fails by complaining about 'local'.
However, I found the file JSON.lua after a bit of Googling.
Zwave - Sigma Z+ stick, Fibaro 2x FGSD002, FGMS001
Horstmann - 2x HRT4, 2x C-stat-11, 4x ASR-ZW, TKB 2x TZ56, 2x TZ68E
Max-EQ Cube, Valve, Neo 2x door
RFXtrx433E, RFlink - 1byone driveway, doorbell, Oregon and BangGood temp
Wifi - YeeLight RGB, 3x ESP8266

jmleglise
Posts: 193
Joined: Tuesday 13 January 2015 0:27
Target OS: Raspberry Pi
Domoticz version: V3.5113
Location: FRANCE
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by jmleglise » Wednesday 20 January 2016 23:44

Hi westcott

I have created a page in the wiki for the library json.lua here : http://www.domoticz.com/wiki/Lua_-_json.lua
This is a commun library, used in several other scripts of this forum. In fact, you need it as soon as you want to request the api of Domoticz.

And for your question : Did you get an error on line 28 ? => of course you need to set up your own latitude in line 28 as well as set up all parameters.

I don't know your skill in lua, but for this kind of script, you have to invest a little time and read a least once every line and comments.
Raspberry Pi2 + RFXTRX433E + KMTronic USB relay card
Blinds Somfy RTS, Blinds Bubendorff, Portal Somfy Evolvia 400, IP Video Doorbell ATZ-DBV01P, chacon IO, Oregon THN132ES, THN122N, Chinese PIR sensor PT2262, Bluetooth low energy dongle, iBeacon

styxxen
Posts: 3
Joined: Thursday 21 January 2016 11:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by styxxen » Thursday 21 January 2016 11:24

Hey,
I implemented your script yesterday, and I must say that it works really well. Good Job!
I have a question though, the API calls are running through the roof,and I am already up to 199 API calls to Weather Underground today (time now is 10:15)
So how can I edit your script to not go over the 500 calls/day limit?

This is actually my first attempt at LUA-scripts, so I took the easy way and chose to do it through the "Events" system as a LUA script, and chose "Time" as script type.
The script now runs every minute, which I guess is fine, as long as the API calls are not performed every minute.
So do you have any tips on what to change in your script on line 52 to not have it making http request every minute?

time = os.date("*t")
if ((time.min % 1)==0) then -- remember the wundergroud limitation

styxxen
Posts: 3
Joined: Thursday 21 January 2016 11:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by styxxen » Thursday 21 January 2016 11:29

OK, I think I can answer my own question ;)

time = os.date("*t")
if ((time.min % 5)==0) then -- remember the wundergroud limitation

That will make the script run every 5 minutes, which solves the API limit-issue

User avatar
Westcott
Posts: 548
Joined: Tuesday 09 December 2014 18:04
Target OS: Raspberry Pi
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by Westcott » Thursday 21 January 2016 13:31

Well, I'm getting further.
Any idea how I know which 'City' to use?
I live right out in the country, not near a city.
However, not even 'London' works!

UPDATE

Changing 'FR' to 'GB' in the call to 'curl http://api.wunderground.com/api/' helped a bit!
Now I can get 'London'
But how to get my own PWS?
For the moment, I've set it to a nearby place, and run the call successfully in the browser.
But running the Lua now gives -
Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_weather.lua:77: attempt to index field 'current_observation' (a nil value)
apparently in -
local latitude = jsonLocation.current_observation.display_location.latitude
but this seems unlikely,

Debugging continues...

BTW I'm a retired .Net and Unix programmer.
Zwave - Sigma Z+ stick, Fibaro 2x FGSD002, FGMS001
Horstmann - 2x HRT4, 2x C-stat-11, 4x ASR-ZW, TKB 2x TZ56, 2x TZ68E
Max-EQ Cube, Valve, Neo 2x door
RFXtrx433E, RFlink - 1byone driveway, doorbell, Oregon and BangGood temp
Wifi - YeeLight RGB, 3x ESP8266

styxxen
Posts: 3
Joined: Thursday 21 January 2016 11:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by styxxen » Thursday 21 January 2016 16:33

Hi,
You need to Edit this row:

local config=assert(io.popen('curl http://api.wunderground.com/api/'..wuAP ... ty..'.json')) -- customize here !!

Paste in you APIkey where it says ..wuAPIkey..
Change /FR/ to /GB/ (or whatever countrycode WU uses for UK.
Paste in London where it says ..city..


Sent from my iPhone using Tapatalk

jmleglise
Posts: 193
Joined: Tuesday 13 January 2015 0:27
Target OS: Raspberry Pi
Domoticz version: V3.5113
Location: FRANCE
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by jmleglise » Thursday 21 January 2016 22:41

Both for the problem of API limitation, and the problem of living in the middle of nowhere 8-) : The City and the wunderground API are only used to get your local Pressure. (search the "relativePressure" variable to understand). This has a low impact on the formulae (while the cloud layer has a big one for example).

1/ You don't need to use Weather underground nor the city if you already have a barometer sensor (value in hPa). Use it instead and save some API Calls :
- you may already use the virtual Hardware type "Weather underground" ? (If so, remember, you already have an APi key , look in the Setup / Hardware page of Domoticz).
- Or perhaps do you have an hardware Temp sensor outside with Pressure ?

2/ you can simply remove City and wunderground and not use the Pressure by setting "relativePressure" in the script to an average value like 1024.

3/ or you can search the Wunderground doc (http://api.wunderground.com/weather/api/d/docs) to find if you can use it with Lat. and Long. without the name of a City. I don't know.
Raspberry Pi2 + RFXTRX433E + KMTronic USB relay card
Blinds Somfy RTS, Blinds Bubendorff, Portal Somfy Evolvia 400, IP Video Doorbell ATZ-DBV01P, chacon IO, Oregon THN132ES, THN122N, Chinese PIR sensor PT2262, Bluetooth low energy dongle, iBeacon

User avatar
Westcott
Posts: 548
Joined: Tuesday 09 December 2014 18:04
Target OS: Raspberry Pi
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by Westcott » Friday 22 January 2016 19:04

Thanks for the detailed reply!
I'll go for option 1 - I have my own Wunderground PWS ( IENGLAND873 ) with my API key in the script.
Zwave - Sigma Z+ stick, Fibaro 2x FGSD002, FGMS001
Horstmann - 2x HRT4, 2x C-stat-11, 4x ASR-ZW, TKB 2x TZ56, 2x TZ68E
Max-EQ Cube, Valve, Neo 2x door
RFXtrx433E, RFlink - 1byone driveway, doorbell, Oregon and BangGood temp
Wifi - YeeLight RGB, 3x ESP8266

stefanvn
Posts: 5
Joined: Saturday 06 February 2016 11:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by stefanvn » Saturday 06 February 2016 11:11

Thanks for the script, this works great!
I also wanted to use this script to switch the lights in and around the house, based on the amount of light. However, if I try to retrieve the Lux-value in a separate LUA script with the "otherdevices_svalues command, I'm constantly getting an error.

The code I'm using:

Code: Select all

commandArray = {}
  Luxvalue = otherdevices_svalues['Lux']
  return commandarray
The error message when I run the script in terminal:

lua: script_time_Duskswitchonlux.lua:3: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
script_time_Duskswitchonlux.lua:3: in main chunk
[C]: ?

Can you share with me how you have setup your scripts, based on the Lux value?

Thanks in advance for your reply.

jmleglise
Posts: 193
Joined: Tuesday 13 January 2015 0:27
Target OS: Raspberry Pi
Domoticz version: V3.5113
Location: FRANCE
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by jmleglise » Saturday 06 February 2016 12:25

Lux is for me a virtual device of type Lux and subtype Lux, named 'Lux". svalues is a string. So to use it you must convert it to a number. like this :

if tonumber(otherdevices_svalues['Lux'])>1000 then


Does it help ?
Raspberry Pi2 + RFXTRX433E + KMTronic USB relay card
Blinds Somfy RTS, Blinds Bubendorff, Portal Somfy Evolvia 400, IP Video Doorbell ATZ-DBV01P, chacon IO, Oregon THN132ES, THN122N, Chinese PIR sensor PT2262, Bluetooth low energy dongle, iBeacon

stefanvn
Posts: 5
Joined: Saturday 06 February 2016 11:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by stefanvn » Saturday 06 February 2016 18:22

Hi Jimlegise,

Thanks for your reply. I tried your suggestion in a small script:

Code: Select all

ommandarray={}
if tonumber(otherdevices_svalues['Lux'])>1000 then print("on")
end
return commandarray
Again, I'm getting an error message:

lua: luxswitch.lua:2: attempt to index global 'otherdevices_svalues' (a nil value)
stack traceback:
luxswitch.lua:2: in main chunk
[C]: ?

Here is my Lux device:
Lux.png
Lux.png (33.78 KiB) Viewed 9372 times
Perhaps I'm missing something. I'm not a coding expert, sorry.

User avatar
Westcott
Posts: 548
Joined: Tuesday 09 December 2014 18:04
Target OS: Raspberry Pi
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by Westcott » Saturday 06 February 2016 18:46

What happens if you try -
print(otherdevices_svalues['Lux'])
Also check its value in Setup->More options->Events->Show Current States
Is it the only device called Lux?
Zwave - Sigma Z+ stick, Fibaro 2x FGSD002, FGMS001
Horstmann - 2x HRT4, 2x C-stat-11, 4x ASR-ZW, TKB 2x TZ56, 2x TZ68E
Max-EQ Cube, Valve, Neo 2x door
RFXtrx433E, RFlink - 1byone driveway, doorbell, Oregon and BangGood temp
Wifi - YeeLight RGB, 3x ESP8266

stefanvn
Posts: 5
Joined: Saturday 06 February 2016 11:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by stefanvn » Sunday 07 February 2016 12:41

Still not working....I'm getting a bit frustrated. Seems to me that I'm missing something obvious or something.

What I did sofar:

* Check value in Setup->More options->Events->Show Current States: Value is present.
* Check whether it is the only device called "Lux": it is.
* print(otherdevices_svalues['Lux']): same error message (attempt to index global 'otherdevices_svalues' (a nil value)).
* Removed Lux device and re-created a new dummy Lux device: Device gets updated with Luxvalue, but print(otherdevices_svalues['Lux']) still gives the same error message.

Seems to me that it shouldn't be that hard to retrieve such a value, but apparently it is too difficult for me ;-)

I would love to get some more hints and tips, in order to get this working.

alexsh1
Posts: 275
Joined: Wednesday 30 September 2015 11:50
Target OS: Raspberry Pi
Domoticz version: v3.8975
Location: United Kingdom
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by alexsh1 » Sunday 07 February 2016 16:10

I have setup everything and it all works just fine.
However, my only concern is that the azimuth and altitude are measured in degrees and I have km (I have used virtual visibility). I browsed through all the virtual sensors and was not able to find one corresponding to degrees and minutes.

stefanvn
Posts: 5
Joined: Saturday 06 February 2016 11:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by stefanvn » Sunday 07 February 2016 21:17

Hi Alexsh1,

Thanks for your reply. Please note that the script that jmleglise provided is working perfectly! It is just that I can't seem to get other scripts going to switch lights on and off based on the Lux value calculated in the script. Hence my request for help in this topic.

alexsh1
Posts: 275
Joined: Wednesday 30 September 2015 11:50
Target OS: Raspberry Pi
Domoticz version: v3.8975
Location: United Kingdom
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by alexsh1 » Monday 08 February 2016 12:12

stefanvn wrote:Hi Alexsh1,

Thanks for your reply. Please note that the script that jmleglise provided is working perfectly! It is just that I can't seem to get other scripts going to switch lights on and off based on the Lux value calculated in the script. Hence my request for help in this topic.
Hi,

I never said that the script is not working. All I am saying is that I have the sun azimuth/altitude displayed in km and it should be in degrees. I was not to able a virtual sensor displaying degrees.

Cheers
Alex

paulD
Posts: 18
Joined: Wednesday 20 January 2016 15:46
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by paulD » Monday 08 February 2016 12:56

Hi Alex,

You should vote for this suggestion :
http://www.domoticz.com/forum/viewtopic ... 31&t=10078 to encourage developpers to create a free counter (degrees and everything else).

latis
Posts: 14
Joined: Thursday 31 October 2013 17:42
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by latis » Wednesday 10 February 2016 1:51

Hi,
I got an error message with this script.
Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_SolarSensor.lua:64: attempt to index field 'current_observation' (a nil value)

Why do I get this?

//Latis

jmleglise
Posts: 193
Joined: Tuesday 13 January 2015 0:27
Target OS: Raspberry Pi
Domoticz version: V3.5113
Location: FRANCE
Contact:

Re: Real-time solar data : azimuth, Altitude, Lux sensor...

Post by jmleglise » Friday 12 February 2016 0:02

Hi,

What is your line 64 ?

And did you read all the script at least once to made all the adaptation mentioned in the comments ? for example, as you are on RPi, you must change this line :
--json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
Raspberry Pi2 + RFXTRX433E + KMTronic USB relay card
Blinds Somfy RTS, Blinds Bubendorff, Portal Somfy Evolvia 400, IP Video Doorbell ATZ-DBV01P, chacon IO, Oregon THN132ES, THN122N, Chinese PIR sensor PT2262, Bluetooth low energy dongle, iBeacon

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests