Lightning and Domoticz

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
elmortero
Posts: 237
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Lightning and Domoticz

Post by elmortero » Wednesday 30 May 2018 12:00

Hi,

Is there a way to parse parameters to http://www.onweeractueel.nl/domoticz_bo.json ?
I wrote a script in dzvents for this but sometimes the json result is too large (which returns "lua script execution exceeded maximum number of lines")

What would be great, if possible, is to send a parameter with max/min LAT and LON so that the number of results gets lower.

For those interested in my dzvents version:

Code: Select all

 
 return {
	on = {
		timer = { 'every 15 minutes' },
		httpResponses = { 'blitz' } -- matches callback string below
	},
        data = {
            last = { initial = 0 }
        },
	
	execute = function(domoticz, triggerItem)
	   local lightning = domoticz.devices('Bliksemteller')
	   local preLight = tonumber(lightning.rawData[1])

		distance = function(lat1, lng1, lat2, lng2)
		radius = 6371
		dLat = (lat2 - lat1) * math.pi / 180
		dLng = (lng2 - lng1) * math.pi / 180
		lat1 = lat1 * math.pi / 180
		lat2 = lat2 * math.pi / 180
		val = math.sin(dLat / 2) * math.sin(dLat / 2) + math.sin(dLng / 2) * math.sin(dLng / 2) * math.cos(lat1) * math.cos(lat2)
		ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1 - val))
		return radius * ang
		end
		
		local latHome = 00.000000  --replace with your own coordenates
		local lngHome = 00.000000  --replace with your own coordenates
		local distanceRange = 20    --change to the maximum distance you want for filtering (in KMs)
		local last = tonumber(domoticz.data.last)
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://www.onweeractueel.nl/domoticz_bo.json',
				method = 'GET',
				callback = 'blitz'
			})
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
			local value = 0
			local ignored = 0
			tl = #response.json
			tc = 1
			repeat
			local times = tonumber(response.json[tc][1])
			local lat = tonumber(response.json[tc][2])
			local lng = tonumber(response.json[tc][3])
			local distanceBetween = distance(latHome, lngHome, lat, lng)
			 if (distanceBetween <= distanceRange) then
				if (times > last) then
				 value = value + 1
				else
				 value = 0
				end
			  domoticz.data.last = times
			 end
			tc = tc + 1
			until tc > tl
			print('Blitz Value = '..value)
			if value ~= preLight then
			lightning.updateCustomSensor(value)
	        end
			else
				print('**blitz failed to fetch info')
			end
		end
	end
}
 

FrankVZ
Posts: 35
Joined: Sunday 27 May 2018 12:01
Target OS: -
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by FrankVZ » Friday 01 June 2018 16:06

I think we're gonna die.
Look at the results of the lightning strikes with the data and script as mentioned before and get no errors as far as I know.

The amount of strikes (running script every 10 mins for a range of 15 km):
DateTime Series 1
31-05-2018 15:00 0,00
31-05-2018 16:00 4,00
31-05-2018 17:00 18446744073709500000,00
31-05-2018 18:00 0,00
31-05-2018 19:00 0,00
31-05-2018 20:00 0,00
31-05-2018 21:00 0,00
31-05-2018 22:00 0,00
31-05-2018 23:00 0,00
01-06-2018 00:00 0,00
01-06-2018 01:00 0,00
01-06-2018 02:00 0,00
01-06-2018 03:00 0,00
01-06-2018 04:00 0,00
01-06-2018 05:00 0,00
01-06-2018 06:00 0,00
01-06-2018 07:00 0,00
01-06-2018 08:00 0,00
01-06-2018 09:00 0,00
01-06-2018 10:00 0,00
01-06-2018 11:00 0,00
01-06-2018 12:00 0,00
01-06-2018 13:00 0,00
01-06-2018 14:00 1,00
01-06-2018 15:00 18446744073709500000,00


The same script (I gave it another name), and running at a lower refresh rate (2x/hr for 30 km)
31-05-2018 15:00 0,00
31-05-2018 16:00 37,00
31-05-2018 17:00 18446744073709500000,00
31-05-2018 18:00 0,00
31-05-2018 19:00 0,00
31-05-2018 20:00 0,00
31-05-2018 21:00 0,00
31-05-2018 22:00 0,00
31-05-2018 23:00 0,00
01-06-2018 00:00 0,00
01-06-2018 01:00 0,00
01-06-2018 02:00 0,00
01-06-2018 03:00 0,00
01-06-2018 04:00 0,00
01-06-2018 05:00 0,00
01-06-2018 06:00 0,00
01-06-2018 07:00 0,00
01-06-2018 08:00 0,00
01-06-2018 09:00 0,00
01-06-2018 10:00 0,00
01-06-2018 11:00 0,00
01-06-2018 12:00 0,00
01-06-2018 13:00 0,00
01-06-2018 14:00 0,00
01-06-2018 15:00 0,00


Well, are we going to survive? :)

It looks like the misreading occurs, with the first reading after a successful/correct reading.

Someone knows what causes these strange amounts?

User avatar
EdwinK
Posts: 1379
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Lightning and Domoticz

Post by EdwinK » Saturday 02 June 2018 20:54

EdwinK wrote:
Tuesday 29 May 2018 20:20
Getting the script from Phoenixblue's git page made it work again.

Hmm.. Not really. Still doesn't update the counter :(

Tried with creating another counter, but still no updating :(
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

FrankVZ
Posts: 35
Joined: Sunday 27 May 2018 12:01
Target OS: -
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by FrankVZ » Thursday 07 June 2018 22:38

Well, a small update: in my case it is updating, but on regular basis the huge error (18 * 10^18 number)

The script starts in my case by the synology task manager (running the python script from there). It has a big advantage: if Domoticz is down (crashed last two days at midnight for some reason) I instantly get notified by the synology that is still brave doing what it has to do.
So that is nice,

but the huge number error is still strange.
But today I also find out, while testing another counter, the same error occurs.
I tested the json command in order to set a new value into the counter with
http://<myip>:<myport>/json.htm?type=command&param=udevice&idx=<mycounter>&svalue=200

It just should put the number 200 into the counter, but got 18*18^10 instead.

So it wouldn't surprise me if the behaviour /bug is coming from the counter function within Domoticz it selves, instead of the script created by Phoenixblue or the sources it uses (the API).
It is quite buggy anyway, because the graph only shows the last 10 hours, but the header says 24hrs :)
Counter errors.jpg
Counter errors.jpg (63.02 KiB) Viewed 1096 times
Question is of course: how to solve this issue?
Al lot of people should experience counter errors in that case?
Or is it the interpreter between json <-> DZ where is goes wrong?

assenzuid
Posts: 166
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Lightning and Domoticz

Post by assenzuid » Thursday 09 August 2018 16:49

Today I noticed the script is not working.
I use the script at page 3 of this topic.

Code: Select all

Traceback (most recent call last):
  File "lightning.py", line 76, in <module>
    for pos in data:
TypeError: 'NoneType' object is not iterable

User avatar
phoenixblue
Posts: 82
Joined: Friday 25 November 2016 12:20
Target OS: Raspberry Pi
Domoticz version: 3.7392
Contact:

Re: Lightning and Domoticz

Post by phoenixblue » Thursday 09 August 2018 16:58

Hi,
This is correct, there are some import problems from the main server in Germany.
Unfortunately this part must be solved from external.

When the import is back working I will let it know.


Verstuurd vanaf mijn EML-L29 met Tapatalk

scripts on github: Link

SweetPants
Posts: 2349
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.10196
Location: The Netherlands
Contact:

Re: Lightning and Domoticz

Post by SweetPants » Friday 10 August 2018 9:19

I have data again since aug 9 16:00, and some lightning strikes where discovered.
Ubuntu 16.04.5 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

Thelion
Posts: 46
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Lightning and Domoticz

Post by Thelion » Friday 17 August 2018 15:08

elmortero wrote:
Wednesday 30 May 2018 12:00
Hi,

For those interested in my dzvents version:
I really like your dzVents script. I think it will be more efficient than an external script.

I haven't got it working yet. I get the following error in the log:

Code: Select all

Error opening url: https://www.onweeractueel.nl/domoticz_bo.json
Status: dzVents: **blitz failed to fetch info
I also get the error "lua script execution exceeded maximum number of lines" you mentioned sometimes. Is the rootcuase of these two errors the same? Have you found a solution to the number of lines issue?

elmortero
Posts: 237
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Lightning and Domoticz

Post by elmortero » Friday 17 August 2018 23:28

No solution yet.
In this thread viewtopic.php?f=59&t=24304 it was mentioned that they are working on it.

User avatar
phoenixblue
Posts: 82
Joined: Friday 25 November 2016 12:20
Target OS: Raspberry Pi
Domoticz version: 3.7392
Contact:

Re: Lightning and Domoticz

Post by phoenixblue » Saturday 18 August 2018 9:18

Hi,
The download issues are solved so the script must be back working.


Verstuurd vanaf mijn EML-L29 met Tapatalk

scripts on github: Link

User avatar
EdwinK
Posts: 1379
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Lightning and Domoticz

Post by EdwinK » Saturday 18 August 2018 9:32

Going to try this then. *waiting for some lightning to occur* :)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

User avatar
phoenixblue
Posts: 82
Joined: Friday 25 November 2016 12:20
Target OS: Raspberry Pi
Domoticz version: 3.7392
Contact:

Re: Lightning and Domoticz

Post by phoenixblue » Saturday 18 August 2018 9:42

You can change the gps coordinate to example Majorca they having at this moment some lightning.
Or you can change the radius to +10k ;-)
scripts on github: Link

User avatar
EdwinK
Posts: 1379
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Lightning and Domoticz

Post by EdwinK » Friday 24 August 2018 23:43

sudo python3 lightning.py
Found 0 matches -- Fri Aug 24 23:42:54 2018
115 old matches were ignored -- Fri Aug 24 23:42:54 2018
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

knisa
Posts: 2
Joined: Sunday 26 August 2018 15:39
Target OS: Windows
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by knisa » Sunday 26 August 2018 15:46

mrfusion wrote:
Friday 02 June 2017 23:58
Hi and thanks for this nice script however I'm facing some strange errors:

$ python lightning.py
Traceback (most recent call last):
File "lightning.py", line 73, in <module>
data = json.loads(z.content)
File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'


I'm running Ubuntu 16.04.2 LTS and python 3.5.2

At first I thought it was a package issue but I have all needed ones.
Maybe you created it with older python/json version and some commands changes.

Any idea? (took last version you posted)
Hi,

My problem is same.
Windows 7, Domoticz v4.9700, python 3.5.4
I try "data = json.loads(z.content.decode('utf-8'))" line, bit it make error.

Please help me. Thanks!

User avatar
EdwinK
Posts: 1379
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Lightning and Domoticz

Post by EdwinK » Sunday 26 August 2018 19:38

What error do you get when you change the script? It's working for me.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

knisa
Posts: 2
Joined: Sunday 26 August 2018 15:39
Target OS: Windows
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by knisa » Sunday 26 August 2018 21:09

Dear EdwinK,

I use this script:
phoenixblue wrote:
Friday 07 April 2017 12:06
Hi,
Small update to not send double strikes to the counter in Domoticz when it' s quiet with the lightning.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import requests
import json
import time
from datetime import datetime

latHome = xx.xxxx
lngHome = xx.xxxx
distanceRange = 15  # in km
jsonUrl = "http://www.onweerdetectie.com/domoticz_bo.json"

server = "http://localhost"
port = 8080
deviceIdx = "1"


def distance(lat1, lng1, lat2, lng2):
    radius = 6371

    dLat = (lat2 - lat1) * math.pi / 180
    dLng = (lng2 - lng1) * math.pi / 180

    lat1 = lat1 * math.pi / 180
    lat2 = lat2 * math.pi / 180

    val = math.sin(dLat / 2) * math.sin(dLat / 2) + math.sin(dLng / 2) * \
        math.sin(dLng / 2) * math.cos(lat1) * math.cos(lat2)
    ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1 - val))
    return radius * ang

last = 0
while True:
    z = requests.get(jsonUrl)
    data = json.loads(z.content)
    value = 0
    ignored = 0
    for pos in data:
        time_, lat, lng = pos
        distanceBetween = distance(latHome, lngHome, lat, lng)
        if (distanceBetween <= distanceRange):
            if (time_ > last):
                value += 1
            else:
                ignored += 1

    last = time_

    print ("%d inslagen gevonden -- %s" % (value, datetime.strftime(datetime.now(), "%c")))
    print ("%d oude inslagen worden genegeerd -- %s" % (ignored, datetime.strftime(datetime.now(), "%c")))
    requests.get(
        "%s:%d/json.htm?type=command&param=udevice&idx=%s&svalue=%d" %
        (server, port, deviceIdx, value))

    time.sleep(120)  # wacht 120 seconden
@ropske, when you have an url of the json location we can insert it in the script so we have more sources to use.
I have same problem like mrfusion.

If I change this line to "data = json.loads(z.content.decode('utf-8'))" the error message is the next:
error1.jpg

Thanks your help!
Attachments
error1.jpg
error1.jpg (48.85 KiB) Viewed 462 times

User avatar
EdwinK
Posts: 1379
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Lightning and Domoticz

Post by EdwinK » Sunday 26 August 2018 21:18

First, I believe this

Code: Select all

jsonUrl = "http://www.onweerdetectie.com/domoticz_bo.json"
should be:

Code: Select all

jsonUrl = "http://www.onweeractueel.nl/domoticz_bo.json"
I'm not familiar with running domoticz on Windows systems, can't help you there.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

SweetPants
Posts: 2349
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.10196
Location: The Netherlands
Contact:

Re: Lightning and Domoticz

Post by SweetPants » Monday 27 August 2018 12:30

I think you are using the 'old' script, try this one

Code: Select all

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#####################################################
# Following libraries in python are needed : json, yaml,math,time,requests
# This script will load an external json file that contact for europe the lightning information and will send it to Domoticz when it's
# insde the distance range
# Many thanks to Ignacio for helping ;-).
#
# How to use:
# 1) made an dummy counter inside Domoticz and set the idx into this script "deviceIdx"
# 2) You cen set the GPS location where to calculate with manual otherwise the script try to receive this info from the settings page inside Domoticz
# 3) Set the distance range in km where to calculate with.
# 4) Run this script on the way you like, as service or cronjob, etc.
#
# Accuracy is quite good and is sometimes lower than 1 kilometer. This is equal to commercial stroke detection companies. But you can not use this info for protection of life and property!
#
#####################################################

import math
import requests
import json
import os
from datetime import datetime

# Domoticz server settings
server = "http://localhost"
port = 8080
deviceIdx = "124"

# Location to import the lightning info
jsonUrl = "https://www.onweeractueel.nl/domoticz_bo.json"

# GPS location and distance to calculate
latHome = 0.0
lngHome = 0.0
distanceRange = 15  # Distance in km

# Try to get GPS location from domoticz
try:
    data = json.loads(
        requests.get(
            "%s:%d/json.htm?type=settings" %
            (server, port)).content)
    latHome = float(data['Location']['Latitude'])
    lngHome = float(data['Location']['Longitude'])
except:
    pass
    
# Location distance calculation

def distance(lat1, lng1, lat2, lng2):
    radius = 6371

    dLat = (lat2 - lat1) * math.pi / 180
    dLng = (lng2 - lng1) * math.pi / 180

    lat1 = lat1 * math.pi / 180
    lat2 = lat2 * math.pi / 180

    val = math.sin(dLat / 2) * math.sin(dLat / 2) + math.sin(dLng / 2) * \
        math.sin(dLng / 2) * math.cos(lat1) * math.cos(lat2)
    ang = 2 * math.atan2(math.sqrt(val), math.sqrt(1 - val))
    return radius * ang

last = 0
if os.path.exists("/tmp/last_lightning.txt"):
    f = open("/tmp/last_lightning.txt")
    last = int(f.read())
    f.close()

z = requests.get(jsonUrl)

data = z.json() # Python3 way
value = 0
ignored = 0
for pos in data:
    time_, lat, lng = pos
    distanceBetween = distance(latHome, lngHome, lat, lng)
    if (distanceBetween <= distanceRange):
        if (time_ > last):
            value += 1
        else:
            ignored += 1

f = open("/tmp/last_lightning.txt", "w")
f.write(str(time_))
f.close()

print ("Found %d matches -- %s" %
       (value, datetime.strftime(datetime.now(), "%c")))
print ("%d old matches were ignored -- %s" %
       (ignored, datetime.strftime(datetime.now(), "%c")))

requests.get(
    "%s:%d/json.htm?type=command&param=udevice&idx=%s&svalue=%d" %
    (server, port, deviceIdx, value))
Ubuntu 16.04.5 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

SweetPants
Posts: 2349
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.10196
Location: The Netherlands
Contact:

Re: Lightning and Domoticz

Post by SweetPants » Friday 26 October 2018 18:44

Ubuntu 16.04.5 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

User avatar
Derik
Posts: 2183
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Lightning and Domoticz

Post by Derik » Friday 26 October 2018 21:24

Also not working here any more..
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest