Lightning and Domoticz

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!
freijn
Posts: 71
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Lightning and Domoticz

Postby freijn » Wednesday 19 April 2017 13:08

If you start the script from putty , end it with an & enperant which makes it keep running in the background while exiting Putty.

python script.py &

User avatar
Siewert308SW
Posts: 468
Joined: Monday 29 December 2014 16:47
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands - Zoutkamp (gn)
Contact:

Re: Lightning and Domoticz

Postby Siewert308SW » Thursday 20 April 2017 22:40

Maybe a dumb question or im in need of glasses. ;-)
The script works well and counter updates as it should be.
But my question is as i want the lighting strikes per hour, how do you guys retrieve the strikes per hour in python or bash so that the output can be used else where.
Setup:
1x RPi3 - PIco hv3.0A Plus / Domoticz / RFXtrx433E
1x RPi3 - PiHole / logging gas,elec
3x FI9803P Cam
2x Youless Elec/Gas
4x KD101 detectors
a lot of KaKu/CoCo stuff

Domoticz scripts: @ GitHub
pico_installer: @ GitHub
pico_status: @ GitHub

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

Re: Lightning and Domoticz

Postby phoenixblue » Thursday 20 April 2017 23:25

Just for my clearance, do you mean the info per hour inside Domoticz or do you mean the import file?

The import file itself is updated every 2 minutes server based from an central database.
scripts on github: Link

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

Re: Lightning and Domoticz

Postby freijn » Friday 21 April 2017 14:34

@Siewert
I see you have domoticz as well. Counting in a couter and then click on 'log' gives you a diagram per hr.

as per Phoenixblue please give us some details about your requirements

User avatar
Siewert308SW
Posts: 468
Joined: Monday 29 December 2014 16:47
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands - Zoutkamp (gn)
Contact:

Re: Lightning and Domoticz

Postby Siewert308SW » Friday 21 April 2017 17:14

freijn wrote:@Siewert
I see you have domoticz as well. Counting in a couter and then click on 'log' gives you a diagram per hr.

as per Phoenixblue please give us some details about your requirements


Phoenixblue knows what i mean as he pm me to clear the sky in our own language ;-)
I know the counter shown you a per hour diagram, but the main question is how to retrieve that so i get a usable output to use somewhere else.
I know you can get daily and monthly by a json but hourly isn't possible, or am i missing something.
Setup:
1x RPi3 - PIco hv3.0A Plus / Domoticz / RFXtrx433E
1x RPi3 - PiHole / logging gas,elec
3x FI9803P Cam
2x Youless Elec/Gas
4x KD101 detectors
a lot of KaKu/CoCo stuff

Domoticz scripts: @ GitHub
pico_installer: @ GitHub
pico_status: @ GitHub

tontze
Posts: 223
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby tontze » Sunday 23 April 2017 8:05

Hi !

I have script to make lightning.py to run as service.

You should copy this startup script to /etc/init.d folder named as "lightning"

copy lightning checker script lightning.py to /usr/local/bin

Code: Select all

#! /bin/sh
#
# /etc/init.d/lightning
#
### BEGIN INIT INFO
# Provides:          lightning
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: This daemon manages lightning detection : lightning
### END INIT INFO

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=pi

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
DAEMON=/usr/local/bin/lightning.py

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/lightning/lightning.pid

NAME=lightning

test -x $DAEMON_OPTS || exit 0

. /lib/lsb/init-functions


do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
                return 1
        fi
}

case "$1" in
  start)
     log_daemon_msg "Starting $NAME"
     do_start
     RET_STATUS=$?
     case "$RET_STATUS" in
        1)
           log_success_msg
           [ "$VERBOSE" != no ] && [ $RET_STATUS = 1 ] && log_warning_msg "Daemon was already running"
        ;;
        *)
           start-stop-daemon --start  --background --oknodo --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --exec $DAEMON  $DAEMON_OPTS
           log_end_msg $?
        ;;
     esac
     ;;
  stop)
     log_daemon_msg "Stopping $NAME"
     start-stop-daemon --stop --pidfile $PIDFILE --retry 10
     log_end_msg $?
   ;;
  force-reload|restart)
     $0 stop
     $0 start
   ;;
  status)
     status_of_proc -p $PIDFILE $DAEMON $NAME  && exit 0 || exit $?
   ;;
 *)
   echo "Usage: /etc/init.d/lightning {start|stop|restart|force-reload|status}"
   exit 1
  ;;
esac
exit 0


Do :

Code: Select all

sudo chmod 755 /usr/local/bin/lightning.py


Code: Select all

sudo chmod 755 /etc/init.d/lightning


And finally add it as service :

Code: Select all

sudo update-rc.d lightning defaults


Try it out :

Code: Select all

sudo /etc/init.d/lightning start

and

Code: Select all

sudo /etc/init.d/lightning stop
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

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

Re: Lightning and Domoticz

Postby phoenixblue » Monday 24 April 2017 20:57

Thanks for this solution!

small question, i get this error when i want to set it as server:

Code: Select all

[email protected]:~ $ sudo chmod 755 /usr/local/bin/lightning.py
[email protected]:~ $ sudo chmod 755 /etc/init.d/lightning
[email protected]:~ $ sudo update-rc.d lightning defaults
update-rc.d: error: initscript does not exist: /etc/init.d/lightning
[email protected]:~ $


The file is inside the folder so that must be ok.
scripts on github: Link

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

Re: Lightning and Domoticz

Postby phoenixblue » Friday 28 April 2017 20:51

Hi,

Small update, the script is now also able to check from the Domoticz settings page the GPS location so you don't have to insert it manual.

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 time
from datetime import datetime

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

#Location to import the lightning info
jsonUrl = "http://www.onweerdetectie.com/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
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.
   
scripts on github: Link

tontze
Posts: 223
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby tontze » Tuesday 02 May 2017 21:07

With the new script :

Code: Select all

@raspberrypi:~ $ python /usr/local/bin/lightning.py
  File "/usr/local/bin/lightning.py", line 90
    requests.get(
    ^
IndentationError: unexpected indent
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

tontze
Posts: 223
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby tontze » Tuesday 02 May 2017 21:30

phoenixblue wrote:Thanks for this solution!

small question, i get this error when i want to set it as server:

Code: Select all

[email protected]:~ $ sudo chmod 755 /usr/local/bin/lightning.py
[email protected]:~ $ sudo chmod 755 /etc/init.d/lightning
[email protected]:~ $ sudo update-rc.d lightning defaults
update-rc.d: error: initscript does not exist: /etc/init.d/lightning
[email protected]:~ $


The file is inside the folder so that must be ok.


Im puzzled .. Should work if you really have file "/etc/init.d/lightning" And you have supplied code inside it

-T
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

tontze
Posts: 223
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby tontze » Tuesday 02 May 2017 21:42

tontze wrote:With the new script :

Code: Select all

@raspberrypi:~ $ python /usr/local/bin/lightning.py
  File "/usr/local/bin/lightning.py", line 90
    requests.get(
    ^
IndentationError: unexpected indent


Ok, too much white spaces at the end of the script before new lines :)
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

dennis70
Posts: 4
Joined: Thursday 06 April 2017 20:10
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby dennis70 » Friday 05 May 2017 16:02

i'm getting the next problem, see below.

[email protected]:~/domoticz/scripts/python $ python onweer.py
File "onweer.py", line 90
requests.get(
^
IndentationError: unexpected indent

does anybody know what to do?

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

Re: Lightning and Domoticz

Postby phoenixblue » Friday 05 May 2017 18:23

Looks at the same problem as tontze, when you delete those white spaces is the problem then solved?

I will update the message with the script.
scripts on github: Link

tontze
Posts: 223
Joined: Thursday 12 January 2017 16:30
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby tontze » Friday 05 May 2017 18:33

phoenixblue wrote:Looks at the same problem as tontze, when you delete those white spaces is the problem then solved?

I will update the message with the script.


For me yes :)
-----------------------------------------
Rpi 3 / Razberry 2
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
ESP82xx

zenit
Posts: 4
Joined: Wednesday 25 March 2015 18:40
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Lightning and Domoticz

Postby zenit » Sunday 28 May 2017 10:30

Hi All,

First of all ,thanks for this script phoenixblue/Freijn. Today in my surrounding we had/have lots of strikes this morning. So i decided to try this script.
Running this script in putty from cli worked well. The only thing was that updating my virtual counter in Domoticz didn't.

With some trial and error fiddling I removed de quotes from the device idx.
So i put ---> deviceIdx = 191 <---- instead of ---> deviceIdx = "191" <---- in the script.


I can't explain why but, This worked !!! :) and my sensor got updated in Domoticz. Maybe I helped some people with this info.

edit:typo,small addition.

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

Re: Lightning and Domoticz

Postby GJKNL » Sunday 28 May 2017 16:15

Wow, lot of lightning
Attachments
2017-05-28_161331.png
2017-05-28_161331.png (15.87 KiB) Viewed 216 times

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

Re: Lightning and Domoticz

Postby phoenixblue » Sunday 28 May 2017 21:41

Do you use the last version of the script otherwise it's possible that it counts sometimes double ;-)
scripts on github: Link

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

Re: Lightning and Domoticz

Postby GJKNL » Sunday 28 May 2017 21:49

phoenixblue wrote:Do you use the last version of the script otherwise it's possible that it counts sometimes double ;-)

Looks like not and it looks more than double :D

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

Re: Lightning and Domoticz

Postby assenzuid » Monday 29 May 2017 13:22

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
Moved recently to Emmen.

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

Re: Lightning and Domoticz

Postby vanathi » Monday 29 May 2017 14:13

Anybody s there...?


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests