Lightning and Domoticz

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
User avatar
EdwinK
Posts: 1409
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 29 May 2017 19:23

Same problem as Assenzuid.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

tezzlicious
Posts: 20
Joined: Saturday 09 April 2016 13:47
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Lightning and Domoticz

Post by tezzlicious » Monday 29 May 2017 21:39

assenzuid wrote:Just implementing this nice script.

But I have set it to use quit,

Code: Select all

# time.sleep(120) # When use script in loop use this part.
quit(5) # when use script one's a time use this part.
But it runs continuous when start it from the command line with

Code: Select all

python lightning.py
Found 0 matches -- Mon May 29 13:20:07 2017
0 old matches were ignored -- Mon May 29 13:20:07 2017
Found 0 matches -- Mon May 29 13:20:08 2017
0 old matches were ignored -- Mon May 29 13:20:08 2017
Found 0 matches -- Mon May 29 13:20:08 2017
0 old matches were ignored -- Mon May 29 13:20:08 2017
Found 0 matches -- Mon May 29 13:20:08 2017
0 old matches were ignored -- Mon May 29 13:20:08 2017
Found 0 matches -- Mon May 29 13:20:08 2017
0 old matches were ignored -- Mon May 29 13:20:08 2017

Be careful to get the indentation right. Take a close look at the while loop. Simply removing all the spaces causes to skip the sleep or quit.

assenzuid
Posts: 185
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 » Monday 29 May 2017 22:03

Ok, I have play with it and it now works.

Code: Select all

# 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.
and also configured with time.sleep

Code: Select all

# 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.
Found 0 matches -- Mon May 29 22:02:36 2017
0 old matches were ignored -- Mon May 29 22:02:36 2017
Found 0 matches -- Mon May 29 22:04:38 2017
0 old matches were ignored -- Mon May 29 22:04:38 2017

assenzuid
Posts: 185
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 » Monday 29 May 2017 22:11

What's the best way to get an notification by email, but not at every strike, but one time and if later the same day new lightning is detected again a notification.

assenzuid
Posts: 185
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 » Monday 29 May 2017 22:12

EdKo66 wrote:Same problem as Assenzuid.

I Solved it to modify it so.

Code: Select all

# 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.

User avatar
EdwinK
Posts: 1409
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 29 May 2017 22:15

Going to try this. Thanks.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

User avatar
EdwinK
Posts: 1409
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 29 May 2017 23:12

Running this manually works, but when I want to run this through 'Task Scheduler' on the Synology, I get an 'interrupted (5)' error message.

This is what is in the box to execute.

python /var/scripts/blikseminslag.py

(edit)Nevermind. Got it working.
Last edited by EdwinK on Tuesday 30 May 2017 11:24, edited 1 time in total.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

vanathi
Posts: 8
Joined: Monday 29 May 2017 12:49
Target OS: Linux
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by vanathi » Tuesday 30 May 2017 6:15

there anybody

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

Re: Lightning and Domoticz

Post by phoenixblue » Tuesday 30 May 2017 9:22

Little update, because there are an lot of issues with double lighting strikes and some memory issues because it's running in the memory.

It's now possible to run the script once an time (for example every 2 min) from an cronejob so the script is download the strike info and store the last value in an tmp file.
When you restart the script it will check first the tmp file for the last strike time and compare that with the new info.
So the script is not sending double strik info into Domoticz and you don't have to run the script in the python memory.

Code: Select all

#!/usr/bin/env python
# -*- 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 = "1"

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

# GPS location and distance to calculate
latHome = xx.xxxxxx
lngHome = xx.xxxxxx
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 = 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

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))
Last edited by phoenixblue on Monday 05 June 2017 21:12, edited 1 time in total.
scripts on github: Link

mrfusion
Posts: 4
Joined: Friday 02 June 2017 23:49
Target OS: Linux
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by mrfusion » 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)

GJKNL
Posts: 30
Joined: Monday 31 October 2016 10:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by GJKNL » Saturday 03 June 2017 11:14

GJKNL wrote:Wow, lot of lightning
I think I know why this was going wrong.
I also made a script with a radius of 50km, but I did not change the name of the tmp file. So both scripts used the same tmp file

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

Re: Lightning and Domoticz

Post by phoenixblue » Saturday 03 June 2017 14:09

@mrfusion, strange error.
I will try it to run it on an same environment as you did.
scripts on github: Link

mrfusion
Posts: 4
Joined: Friday 02 June 2017 23:49
Target OS: Linux
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by mrfusion » Saturday 03 June 2017 14:29

@phoenixblue:
Could be python version and the way of handling urls/requests.
It seems not so uncommon error from what I "googled"

It is like the output of json query is not received in the expected format.

mrfusion
Posts: 4
Joined: Friday 02 June 2017 23:49
Target OS: Linux
Domoticz version:
Contact:

Re: Lightning and Domoticz

Post by mrfusion » Saturday 03 June 2017 14:36

@ phoenix blue: found the trick:

I edited the line 73 this way and it is working:
data = json.loads(z.content.decode('utf-8'))

instead of
data = json.loads(z.content)

So I guess that on my system python was not expecting the good format by default.

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

Re: Lightning and Domoticz

Post by phoenixblue » Monday 05 June 2017 21:11

@mrfusion, thanks for the feedback.
A pity that Python used more ways based on the different versions.

Small update from this side, because i'm migrating the server to an other url I have an new location for the json file.
[http://www.onweeractueel.nl/domoticz_bo.json]
I have already changed in the last script, so when you reed this updat change it in yours ;-)

The old one i will run it parallel for an few months.
scripts on github: Link

User avatar
Derik
Posts: 2195
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 » Tuesday 06 June 2017 8:40

Dear...
I did run th eold script.. [ sometimes hahah]
The new i did install... with a crontab
Only the script is not running.
Do i have the settings for this good?
ScreenShot009.jpg
ScreenShot009.jpg (26.28 KiB) Viewed 3105 times
Or where can i go wrong?
And i do have a new odtiod board..[ Xu4 fast fast fast ]
Do i need perhaps install some software?
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

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

Re: Lightning and Domoticz

Post by freijn » Tuesday 13 June 2017 8:29

Derik,

Are you able to run the onweer.py from the commandline ?

>python onweer.py

What is the output of that?

User avatar
EdwinK
Posts: 1409
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 22 June 2017 17:07

Strange. I'm running this script, and it goes from 7 strikes to 1 strike, and then up again.

this is from the log:
2017-06-22 15:30:00 0
2017-06-22 15:35:00 1
2017-06-22 15:40:00 0
2017-06-22 15:45:00 0
2017-06-22 15:50:00 0
2017-06-22 15:55:00 0
2017-06-22 16:00:00 1
2017-06-22 16:05:00 0
2017-06-22 16:10:00 0
2017-06-22 16:15:00 0
2017-06-22 16:20:00 0
2017-06-22 16:25:00 0
2017-06-22 16:30:00 0
2017-06-22 16:35:00 0
2017-06-22 16:40:00 0
2017-06-22 16:45:00 1
2017-06-22 16:50:00 0
2017-06-22 16:55:00 4
2017-06-22 17:00:00 0
2017-06-22 17:05:00 3
Not that I;m going to loose any sleep over it, just wondering.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | RFxcom | Dashticz V2

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 » Saturday 15 July 2017 14:57

Curiosity question....

Here I am located near Chicago and have been testing the AS3935 Lightning sensor break out board on one RPi using Python. It works well.

I am also using custom mapping from lightning dot org except there are no real local stations here.

Concurrently also revisiting old Hobby Boards 1-wire lightning sensor which is just a dual 1-wire counter. The 1-wire network is connected via a USB-Serial 9097 hub. Next also testing an EMP sensor which is just a pulse detection sensor on a GPIO pin on the RPi.

Is it possible to combine both to one Python script so generate lightning sensor variables?
lightningsensor.jpg
lightningsensor.jpg (378.48 KiB) Viewed 2630 times
Concurrently testing this lightning sensor counting pulses on GPIO pin for local lightning counts
emp.jpg
emp.jpg (172.05 KiB) Viewed 2628 times
and old Hobby boards dual wire sensor for distant lightning counts
hblightningsensor.jpg
hblightningsensor.jpg (47.23 KiB) Viewed 2628 times
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)

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

Re: Lightning and Domoticz

Post by freijn » Saturday 15 July 2017 23:53

Pete,

Not quite sure I do understand the question, but if you do get the both sensors connected to the Raspberry you can ( or coures) write a script to read them.
From your post I just do not understand what you would like to combine?
Just a graph ? or like to make calculations ?
Combining both imputs to Domoticz ???

Sorry,
Frank

Post Reply

Who is online

Users browsing this forum: Joep123 and 1 guest