Page 1 of 1

how to update python script results to dummy device (temp, wind)

Posted: Thursday 29 December 2016 18:28
by koowee
Hi,

I have made python script, which get results from weather institute site. Now I would like update dummy device in Domoticz, but I'm totally lost. Can you help me on this, how should I do it? :)

When I run python script, I get results below (last column is needed value)

Code: Select all

pi@raspberrypi:~/domoticz/scripts$ python forecast_script.py
WindDirection, 2016-12-29T23:00:00Z, 246.0
WindSpeedMS, 2016-12-29T23:00:00Z, 2.15
WindGust, 2016-12-29T23:00:00Z, 5.32
Temperature, 2016-12-29T23:00:00Z, 1.3
DewPoint, 2016-12-29T23:00:00Z, 1.02

Re: how to update python script results to dummy device (temp, wind)

Posted: Friday 30 December 2016 15:51
by SweetPants

Re: how to update python script results to dummy device (temp, wind)

Posted: Saturday 31 December 2016 0:07
by Nautilus
I'm doing it with a bash script, an example for cloud cover:

Code: Select all

#!/bin/bash
#Retrieve sensor data
/usr/bin/python fmi_observations.py > /home/pi/domoticz/scripts/fmi_observations.tmp
#Find parameters to update
CloudCover=$(cat /home/pi/domoticz/scripts/fmi_observations.tmp | grep 'TotalCloudCover' | awk -F ',' '{print $2}')
#Update if value has changed
curl -s "http://url:port/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=$CloudCover"
(note, although the commented line says "Update if value has changed" I have not yet added the check for current value in Domoticz to determine whether to update)

Here I'm getting similar data as you are but I took away the extra space between the columns (if you keep it, you need to modify/replace the awk statement). Also, with grep it only works if "TotalCloudCover" is only once in the results (=> if you plan to fetch also forecast, once again modifications are needed). On your example the data is on third column so '{print $2} would ned to be '{print $3}.

In any case, basically this will fetch the the data with the python script and store it to a temp file (always writes over the previous). Then find the row matching to "TotalCloudCover" and store the second column (data) of that line as variable CloudCover. Curl fetches then the url which updates the value to Domoticz through the json api.

Several ways to add all the parameters here, the simplest one probably to just add more variables and curls :) Finally have it run with cron on desired intervals.

On the Finnish home automation fb group where you probably picked this up from, there is also an example how to do it with node red (available by default in case you used sd image to orginally install Domoticz). I used to do stuff with node red but for some reason it kept hogging the memory which lead to crashes and slowness, probably due to my own fault though :)

edit: my data is in this format:

Code: Select all

GeopHeight,112.94
Temperature,4.44
Pressure,1007.0
Humidity,88.98
WindDirection,257.67
WindSpeedMS,2.39
WindUMS,1.98
WindVMS,1.33
MaximumWind,3.67
WindGust,7.17
DewPoint,2.15
TotalCloudCover,100.0
WeatherSymbol3,3.0
LowCloudCover,92.4
MediumCloudCover,0.0
HighCloudCover,95.5
Precipitation1h,0.0
PrecipitationAmount,0.0
RadiationGlobalAccumulation,0.0
RadiationLWAccumulation,6416061.5
RadiationNetSurfaceLWAccumulation,-166203.08
RadiationNetSurfaceSWAccumulation,0.0
RadiationDiffuseAccumulation,0.0

Re: how to update python script results to dummy device (temp, wind)

Posted: Saturday 31 December 2016 10:19
by koowee
Thanks to both of you.

Yes I have read that wiki page, but it was still little unclear how I can use it in this (beginner with Domoticz). Last evening I used trial and error method and got this working in same python script, which get the data. Script stored needed data to variables and build JSON URL using those variables. I add this to cron and now it updates Domoticz dummy device (wind+temp+chill) values every 15 minutes..

Code: Select all

# Parsing URL
httpurl = 'http://192.168.1.111:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=' + WindDirection + 
';' +  WindDirectionabbr + ';' + WindSpeed + ';' + WindGust + ';' + Temperature + ';' + str(WindChill)

# Sending data to Domoticz
r = requests.get(httpurl)
Screenshot 2016-12-31 10.54.41.png
Screenshot 2016-12-31 10.54.41.png (29.13 KiB) Viewed 4637 times
Next I will try Nautilus bash script and also Node Red (I have installed Raspbian Jessie and Domoticz separately). Propably I have same problems with Node Red if I can get it to work at all :). And yes, this script is picked up from Finnish FB group

Re: how to update python script results to dummy device (temp, wind)

Posted: Saturday 31 December 2016 12:49
by Nautilus
koowee wrote:Thanks to both of you.

Yes I have read that wiki page, but it was still little unclear how I can use it in this (beginner with Domoticz). Last evening I used trial and error method and got this working in same python script, which get the data. Script stored needed data to variables and build JSON URL using those variables. I add this to cron and now it updates Domoticz dummy device (wind+temp+chill) values every 15 minutes..

Code: Select all

# Parsing URL
httpurl = 'http://192.168.1.111:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=' + WindDirection + 
';' +  WindDirectionabbr + ';' + WindSpeed + ';' + WindGust + ';' + Temperature + ';' + str(WindChill)

# Sending data to Domoticz
r = requests.get(httpurl)
Screenshot 2016-12-31 10.54.41.png

Next I will try Nautilus bash script and also Node Red (I have installed Raspbian Jessie and Domoticz separately). Propably I have same problems with Node Red if I can get it to work at all :). And yes, this script is picked up from Finnish FB group
Nice! Can you show how you did this "Script stored needed data to variables" within the python script?

Re: how to update python script results to dummy device (temp, wind)

Posted: Saturday 31 December 2016 17:25
by koowee
Ofcourse, for example it is just "WindDirection = items[3].text" after http request

Code: Select all

...

if r.status == 200:
    root = ET.fromstring(r.data)
    for items in root.findall('.//{http://xml.fmi.fi/schema/wfs/2.0}BsWfsElement'):
        print(items[2].text + ', ' + items[1].text + ', ' + items[3].text)
        WindDirection = items[3].text
And then I add this "WindDirection" and other variables to JSON URL

Code: Select all

httpurl = 'http://192.168.1.111:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=' + WindDirection + 
';' +  WindDirectionabbr + ';' + WindSpeed + ';' + WindGust + ';' + Temperature + ';' + str(WindChill)
And JSON URL like this (WindDirection is 272.0)

http://192.168.1.111:8080/json.htm?type ... 1.68;-5.87

Re: how to update python script results to dummy device (temp, wind)

Posted: Saturday 31 December 2016 23:57
by Nautilus
koowee wrote:Ofcourse, for example it is just "WindDirection = items[3].text" after http request

Code: Select all

...

if r.status == 200:
    root = ET.fromstring(r.data)
    for items in root.findall('.//{http://xml.fmi.fi/schema/wfs/2.0}BsWfsElement'):
        print(items[2].text + ', ' + items[1].text + ', ' + items[3].text)
        WindDirection = items[3].text
And then I add this "WindDirection" and other variables to JSON URL

Code: Select all

httpurl = 'http://192.168.1.111:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=' + WindDirection + 
';' +  WindDirectionabbr + ';' + WindSpeed + ';' + WindGust + ';' + Temperature + ';' + str(WindChill)
And JSON URL like this (WindDirection is 272.0)

http://192.168.1.111:8080/json.htm?type ... 1.68;-5.87
Ok, so you trust the parameters are all present and on same order as before? Yes, I guess they really should be so why not...:D

Re: how to update python script results to dummy device (temp, wind)

Posted: Sunday 01 January 2017 9:17
by koowee
Nautilus wrote:
koowee wrote:Ofcourse, for example it is just "WindDirection = items[3].text" after http request

Code: Select all

...

if r.status == 200:
    root = ET.fromstring(r.data)
    for items in root.findall('.//{http://xml.fmi.fi/schema/wfs/2.0}BsWfsElement'):
        print(items[2].text + ', ' + items[1].text + ', ' + items[3].text)
        WindDirection = items[3].text
And then I add this "WindDirection" and other variables to JSON URL

Code: Select all

httpurl = 'http://192.168.1.111:8080/json.htm?type=command&param=udevice&idx=98&nvalue=0&svalue=' + WindDirection + 
';' +  WindDirectionabbr + ';' + WindSpeed + ';' + WindGust + ';' + Temperature + ';' + str(WindChill)
And JSON URL like this (WindDirection is 272.0)

http://192.168.1.111:8080/json.htm?type ... 1.68;-5.87
Ok, so you trust the parameters are all present and on same order as before? Yes, I guess they really should be so why not...:D
Yes, you are right.. At this moment script rely on that, maybe I should try do some checks :D

I also get Node Red working yesterday

Re: how to update python script results to dummy device (temp, wind)

Posted: Tuesday 03 January 2017 14:27
by Nautilus
koowee wrote:
I also get Node Red working yesterday
Great! :) How'd you convert the wind chill by the way, is there some formula to calculate it? Json api does not seem to accept omitting the value although there is even a thread suggesting that Domoticz should be able to calculate it if not provided (edit: NetAtmo only). I hope I got the compass direction correctly parsed...:)

Re: how to update python script results to dummy device (temp, wind)

Posted: Tuesday 03 January 2017 18:10
by koowee
Nautilus wrote:Great! :) How'd you convert the wind chill by the way, is there some formula to calculate it? Json api does not seem to accept omitting the value although there is even a thread suggesting that Domoticz should be able to calculate it if not provided (edit: NetAtmo only). I hope I got the compass direction correctly parsed...:)

Yes, there is formula what I use to calculate it. I found this link https://fi.wikipedia.org/wiki/Pakkasen_purevuus, there is mentioned the formula what Ilmatieteenlaitos is using. Float is in the formula just to convert text to numbers.

Code: Select all

windchill = ( 13.12 + (0.6215 * float(temperature)) ) - ( 13.956 * (float(windspeed)**0.16) ) + ( 0.4867 * float(temperature) * (float(windspeed)**0.16) )
windchill = "%.2f" % windchill
I use this kind of code to parse wind direction.. It may be 180 degrees wrong, need to check :)

Code: Select all

if ( float(winddirection) >= 348.8 and float(winddirection) <=360 ) or ( float(winddirection) >= 0.0 and float(winddirection) <=11.2 ):
        direction = 'S'
elif float(winddirection) >=11.3 and float(winddirection) <= 33.7:
        direction = 'SSW'
elif float(winddirection) >=33.8 and float(winddirection) <= 56.2:
        direction = 'SW'
...

Re: how to update python script results to dummy device (temp, wind)

Posted: Tuesday 10 January 2017 8:31
by Nautilus
koowee wrote: Yes, there is formula what I use to calculate it. I found this link https://fi.wikipedia.org/wiki/Pakkasen_purevuus, there is mentioned the formula what Ilmatieteenlaitos is using. Float is in the formula just to convert text to numbers.

Code: Select all

windchill = ( 13.12 + (0.6215 * float(temperature)) ) - ( 13.956 * (float(windspeed)**0.16) ) + ( 0.4867 * float(temperature) * (float(windspeed)**0.16) )
windchill = "%.2f" % windchill
Thanks, I guess the direction was correct and now also the wind chill. Only thing missing is the rain counter as the PrecipitationAmount does not seem to be the daily cumulative amount that Domoticz is expecting (it can go from some value to 0 during the day which shows as -X [if current rain counter is X]). But maybe FMI does not provide the rain counter data in this way...