Lightning and Domoticz

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
Toulon7559
Posts: 478
Joined: Sunday 23 February 2014 18:56
Target OS: Raspberry Pi
Domoticz version: latest
Location: Hengelo(Ov)/NL
Contact:

Re: Lightning and Domoticz

Post by Toulon7559 » Sunday 16 July 2017 12:31

A different solution to detection of lightning, but perhaps interesting as information:
this article in PiWeekly describes a setup with local read-out by a specific sensor in combination with a Raspberry.
Set1 = RPI-B+RFXCom433+S0PCM+Linksprite-shield for BMP180/DS18B20/RS485+DDS238-1ZN
Set2 = RPI-3+RFLinkGTW+ESP8266s+PWS_WS7000
Common = 3*PVLogger+PWS_TFA_Nexus+KAKUs
=> Energy & Data Management based on Time and on PV&Consumption&Meteo

User avatar
PeteC
Posts: 6
Joined: Friday 07 July 2017 13:39
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: Time
Contact:

Re: Lightning and Domoticz

Post by PeteC » Sunday 16 July 2017 22:12

Thank you Frank and Toulon7559!

Well with the EMP lightning it is just a pulse count to GPIO for an alert type device. It is not as sensitive to lightning as the other two devices. So primarily it would be some sort of an alert device (IE: utilize X10, UPB, Insteon, Z-Wave and Zigbee here plus AD stuff from the Leviton HAI OmniPro 2 alarm panel combo.

The Hobby boards lightning sensor with two counters would just count total lightning and lightning rate per minute. It goes from zero to almost 10,000 lightning strikes when a storm approaches.

The Lightning sensor breakout board is new and uses two / three GPIO pins (well one is an IRQ) with it's patented lightning sensor chip. This one reads lighting strikes and guesstimates distances. Very much different than the first two. Toulon7559...did utilize that article above and have the twitter script working (that said I am not really a tweeter here and rarely utilize any social sites).

I have been testing the EMP and Breakout Lightning sensor board with python scripts and they work well.

Concurrently yesterday on the same RPi2 that is running Domoticz installed Apache 2 and php now doing a php script that writes an xml text file plus installed Mosquitto on the same box.

BTW Toulon7559...parents here would send me to Hyeres and Toulon in the summers to stay with relatives many many years ago. I had a great great aunt that lived in Hyeres in town where you could see that old castle. I was a kid then and would sneak over there to play. Went and stayed with Aunt there in Toulon a few years back. Enjoyed the view from her home up in the hills over looking the town.

Worked in the Airline industry and most of the Internationale stuff worked with SITA/Equant there in Nice as on the international side of the airline industry they managed all of the local and international transport. Satellite to ground to airline stuff worked with Arinc on mostly.
Automator
Hardware: ARM, Intel, AMD based Linux boxes
OS: now all Linux (and a bit of Wintel)
Automation: X10, Inteon, UPB, Zigbee and ZWave - HAI Leviton OmniPro 2 panel (security and automation)

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 » Thursday 20 July 2017 17:42

Since I didn't get any lightning data back, I decided to run the script from command line

Code: Select all

/var/scripts/ python onweer.py

 File "onweer.py", line 60, in <module>
    for pos in data:
TypeError: 'NoneType' object is not iterable
What happend?
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

freijn
Posts: 392
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Lightning and Domoticz

Post by freijn » Friday 21 July 2017 11:26

Ed,

Can you paste the full script here exactly as you use is?
The latest source I see does not have any code on line 60.

cheers,

Frank

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 21 July 2017 18:52

O, it was on line 72, but I removed the remarks things

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#####################################################
# 
#####################################################

import math
import requests
import json
import time
from datetime import datetime

# Domoticz server settings
server = "http://192.168.0.10"
port = 8084
deviceIdx = "178"

#Location to import the lightning info
jsonUrl = "http://www.onweerdetectie.com/domoticz_bo.json" 

# GPS location and distance to calculate
latHome = 51.860067
lngHome = 4.412692
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
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_

# terminal print
    print ("Found %d matches -- %s" %
           (value, datetime.strftime(datetime.now(), "%c")))
    print ("%d old matches were ignored -- %s" %
           (ignored, datetime.strftime(datetime.now(), "%c")))
         
# Send info to domoticz
    requests.get(
    "%s:%d/json.htm?type=command&param=udevice&idx=%s&svalue=%d" %
    (server, port, deviceIdx, value))

    time.sleep(120) # When use script in loop use this part.
    #quit(5) # when use script one's a time use this part..
    
    
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

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 » Sunday 23 July 2017 20:52

After a fresh install of Domoticz on a RPi3 i get this error.

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
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 23 July 2017 21:45

Same here, although not on a Pi
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

freijn
Posts: 392
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Lightning and Domoticz

Post by freijn » Monday 24 July 2017 9:52

Guys

you are not using the latest script.
Please use the script from page 3 of this post where
the Url
http://www.onweeractueel.nl/domoticz_bo.json

is being used. The post of phoenixblue

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 » Monday 24 July 2017 10:35

Thanks. I thought I had that one, but looks it wasn't. N
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

Abbadon
Posts: 56
Joined: Thursday 01 October 2015 8:25
Target OS: Raspberry Pi
Domoticz version: beta
Location: Poland, Wrocław
Contact:

Re: Lightning and Domoticz

Post by Abbadon » Tuesday 25 July 2017 9:27

Thank you.

bertbigb
Posts: 137
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Re: Lightning and Domoticz

Post by bertbigb » Thursday 27 July 2017 16:43

Hi All,

I would like to see if the thunderstorm is approaching, overhead or departing.
I want to change the script that it is going to tell me for several distances, how many strikes there were.
I think to make the change for counting the strikes in steps of 5 km, up to 50 km (so call the script with 10 different distances) and then storing the counts.
Does anyone of you know how to create a new sensor type for this?
So the sensor type should be able to display for lets say in steps of 5 km away how many strikes has been counted, and this over time.
So it should look like something as:
lightning.jpg
lightning.jpg (8.51 KiB) Viewed 2823 times

time horizontal, distance vertical and content the amount of strikes

I hope some of you can guide me how to achieve this.

Thanks in advance
Best regards Bert

Synology DS1515+ - DSM 6.1
Raspberry PI2 - B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon

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 27 July 2017 21:20

freijn wrote:Guys

you are not using the latest script.
Please use the script from page 3 of this post where
the Url
http://www.onweeractueel.nl/domoticz_bo.json

is being used. The post of phoenixblue
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 20 August 2017 19:50

Must have been one hell of a thunderstorm last night
Screen Shot 2017-08-20 at 19.44.01.png
Wow...Lightnings
Screen Shot 2017-08-20 at 19.44.01.png (33.2 KiB) Viewed 2547 times
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 » Sunday 20 August 2017 20:43

Wow, did you have take an picture of that storm ;-)
I will take an look into the db what's happend.

(more uses with the same issue)
scripts on github: Link

freijn
Posts: 392
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Lightning and Domoticz

Post by freijn » Sunday 20 August 2017 22:03

It must be 18 I assume? What are the others???

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 20 August 2017 22:06

There were only 2, if I counted correctely
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

zicht
Posts: 219
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 3.8xxx
Location: NL
Contact:

Re: Lightning and Domoticz

Post by zicht » Saturday 02 September 2017 12:04

phoenixblue wrote:
Tuesday 04 April 2017 21:58
Hi,

Based on the script from @freijn there is an new version that can check based on GPS locations if there is any lightning strike.
It's also run as an python script that will check every 2 minutes if there is any strike.
Thanks for this script !
It inspired me to make a similar script based on this source in Lua
The script counts the strikes in different distances for the last 10 minutes and updates 2 text devices.
(2km 5km 10km 25km 50km 250km)

Output like in text device like "2017-09-01 20:30:10 Bliksem : closest 20.6km 20:26:07 -> 50km:0x 25km:1x 10km:0x 5km:0x 2km:0x"

For the interested here are my code parts , feel free to reuse

Function for getting lightning :

Code: Select all

function onweer(Lt,Lng)
	local tijd={} local latit={} local longit={}
	local distclosest = 5000 local dist250=0 local dist50=0 local dist25A=0 local dist25=0 local dist10=0 local dist5=0  local dist2=0
	local time2 = "" local time5 = "" local time10 = "" local time25 = "" local time50 = "" local time250 = "" local timeclosest = "" 
	local i=1  t = os.date('*t')  tnr=(os.time(t))
 	url="http://www.onweeractueel.nl/domoticz_bo.json"
	--
	content = os.capture("Curl "..url)
	y=string.len(content)
	content=string.sub(content,3,y)
	while string.len(content)>10 do 
		-- tijd 
		x=string.find(content,",") tijd[i]=string.sub(content,1,x-1) tijd[i] = tijd[i]:gsub('%W',' ') tijd[i] = tijd[i]:match("^%s*(.-)%s*$") tijd[i] = tijd[i]:gsub("%s+", " ") content=string.sub(content,x+1,y) 
		-- latitude		
		x=string.find(content,",") latit[i]=string.sub(content,1,x-1) latit[i] = latit[i]:gsub('%W',' ') latit[i] = latit[i]:match("^%s*(.-)%s*$") latit[i] = latit[i]:gsub("%s+", " ") latit[i] = latit[i]:gsub("% ", ".") content=string.sub(content,x+1,y)
		-- longitude
		x=string.find(content,",") if x~=nil then longit[i]=string.sub(content,1,x-1) content=string.sub(content,x+1,y) else longit[i]=string.sub(content,1,y-1) content="" end
		longit[i] = longit[i]:gsub('%W',' ') longit[i] = longit[i]:match("^%s*(.-)%s*$") longit[i] = longit[i]:gsub("%s+", " ") longit[i] = longit[i]:gsub("% ", ".")
		a=(tnr-tonumber(tijd[i]))
		-- Thuis : volgende alleen voor laatste 10 min
		if a<600 then 
			-- afstand
			dist=round(geo_distance(Lt, Lng, latit[i], longit[i]),1)
			if dist == nil then dist = 4000 end
			if dist<distclosest then distclosest = dist  timeclosest=(os.date("%X", tijd[i])) end
			-- binnen 25km 
			if dist<25 then dist25A=dist25A+1 end
			-- aantal per afstand
			if dist<250 and dist>=50 then dist250=dist250+1 time250=string.sub(os.date("%X", tijd[i]),1,5) end
			if dist<50 and dist>=25 then dist50=dist50+1 time50=string.sub(os.date("%X", tijd[i]),1,5) end
			if dist<25 and dist>=10 then dist25=dist25+1 time25=string.sub(os.date("%X", tijd[i]),1,5) end
			if dist<10 and dist>=5 then  dist10=dist10+1 time10=string.sub(os.date("%X", tijd[i]),1,5) end
			if dist<5 and dist>=2 then dist5=dist5+1 time5=string.sub(os.date("%X", tijd[i]),1,5) end
			if dist<2 then dist2=dist2+1 time2=(os.date("%X", tijd[i])) end
			--
			i=i+1
		end
	end
	str="closest "..distclosest.."km ".. timeclosest .. " -> 250km:"..dist250.."x 50km:"..dist50.."x 25km:"..dist25.."x 10km:"..dist10.."x "..time10.."  5km:"..dist5.."x "..time5.."  2km:"..dist2.."x "..time2
	str2="closest "..distclosest.."km ".. timeclosest .. " -> 50km:"..dist50.."x 25km:"..dist25.."x 10km:"..dist10.."x "..time10.."  5km:"..dist5.."x "..time5.."  2km:"..dist2.."x "..time2
	

	return i,dist25A,str,str2
end
function os.capture

Code: Select all

function os.capture(cmd, raw)				-- os.command uitvoeren en resultaat daarvan lezen in string
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  if raw then return s end
  s = string.gsub(s, '^%s+', '')
  s = string.gsub(s, '%s+$', '')
  s = string.gsub(s, '[\n\r]+', ' ')
  --

  return s
end
Call from timescript in lua

Code: Select all

--------------------------------------------------------------------------------
-- Laad standaard functies om aan te roepen
--------------------------------------------------------------------------------
z1 = os.clock()
package.path = package.path .. ';' .. 'C:/PROGRA~2/domoticz/scripts/lua/functions.lua;'   --***
local my = require ("functions")   ---***

commandArray = {}
time = os.date("*t")
--
if (time.min % 10)==0 or tonumber(uservariables["bliksem"])>1 then
	local Lt = 51.xxxxxx local Lng = 5.xxxxxxx  
  	j,dist25A,str,str2=onweer(Lt,Lng)
	if dist25A>0 then 
			if otherdevices['Thuis'] == 'On' then sendtext(str.." TH") end
			print("Bliksem : "..str)
			commandArray['UpdateDevice'] = '636'..'|0|'.."Bliksem : "..str2
			commandArray[#commandArray + 1] = {['UpdateDevice'] = '667'..'|0|'.."Bliksem : "..str2}
	end
	if tonumber(uservariables["bliksem"]) ~= dist25A then commandArray['Variable:bliksem']= ""..dist25A end
end

return commandArray		
As you can see i have all functions in file named functions.lua that i include in my time script, but you can put all in the same time script( then remove the lines package path and local my marked with ***)
The base function is called once in 10 minutes normally
I update 2 text devices 636 and 667 when i am at home. SendText is a telegram notification system.
I use a uservariable "bliksem" to make it run every minute when a lighting stike is within 25 km is detected.

As i am not a coder at all the code is quick an dirty but functional, i am sure there are better ways (like json, but could not get it to work that way)

Again thanks for the idea and code, could not have created this without your example !
Rpi & Win x64. Using : cam's,Nest,RFXCom,Kaku,LaCrosse,RFY,IsAlive,Dummy,Wether Underground, standard Lua, Curl on windows,Domoticz Android App & Tasker,

"The only source of knowledge is experience (A.Einstein)"

freijn
Posts: 392
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Lightning and Domoticz

Post by freijn » Wednesday 06 September 2017 9:16

Nice work Zicht !

I like the lua version.. Need to examine some code of you as it looks very interesting :-)

Together we can make creative and nice stuff :-)

SweetPants
Posts: 2350
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 » Wednesday 06 September 2017 19:16

The URL http://www.onweeractueel.nl/domoticz_bo.json returns null only. this is since 06-09-2017 16:00
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)

DutchHans
Posts: 340
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: Lightning and Domoticz

Post by DutchHans » Wednesday 06 September 2017 22:18

Confirmed..
Regards, Hans

Edit:
Working again

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests