Page 1 of 2

Virtual sensor for rain

Posted: Sunday 03 June 2018 8:40
by packetloss
Afternoon!

I followed this page: https://www.domoticz.com/wiki/Virtual_w ... 29_Devices for creating a virtual device for the rain in mm that I get from Weather Underground. The page says "Mission completed, the virtual temperature sensor can now be used in events."

I do have a value for the rain:
2.png
2.png (2.49 KiB) Viewed 6315 times
but now I see in Blocky, this is not working yet :( .
3.png
3.png (7.9 KiB) Viewed 6315 times
Anybody has a solution for this? What I want to do is start my GR105 (*) device or not, on set times like the evening, depending if there was rain in the last 12 hours or not.

(*) https://www.domotique-store.fr/2416-thi ... e-tour.jpg

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 11:56
by gizmocuz
It's very unclear what you want to do... first of, WU is native supported in domoticz, you do not need a script for this
You want to control 'somethings' when there was more then 'xx' mm of rain ?

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 12:08
by packetloss
> It's very unclear what you want to do... first of, WU is native supported in domoticz, you do not need a script for this

correct, I know Weather Underground is supported, that's why I have the devices like temp, humidti, rain etc.

> You want to control 'somethings' when there was more then 'xx' mm of rain ?

yes. I want to enable / disable devices like the GR105 or any other thing supported in Domoticz, after like 8mm of rain. It seems that Blocky doesn't work with devices like weather measurement. Thats what I understand from "NOT WORKING YET".

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 12:28
by gizmocuz
Blockly works with temperature devices, not sure about rain or wind, it should not be that difficult to add, but time is my problem

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 12:48
by packetloss
gizmocuz wrote: Sunday 03 June 2018 12:28 Blockly works with temperature devices, not sure about rain or wind, it should not be that difficult to add, but time is my problem
Yes, when I remove my rain device (IDX256) and replace it with a temperature device, there's no warning of NOT WORKING YET.

Ok, I understand you're the maintainer / programmer of this part of Blocky in Domoticz?

If you can implement this in Blocky, yes please! In a simplified form, this is what I do with Lua:

Code: Select all

local idxr = 256
if devicechanged[sensorwu] then
        sWeatherFirst, sWeatherRain = otherdevices_svalues[sensorwu]:match("([^;]+);([^;]+)")
        sWeatherRain = tonumber(sWeatherRain)
        print('sWeatherRain is now' .. sWeatherRain)
        commandArray['UpdateDevice']=idxr .. '|0|' .. sWeatherRain      -- for updating Dummy devices e.g. '96|0|Hello World'
return commandArray
and this works: I see the correct value of IDX # 256 when I go to Setup, Events, Show current values. And this is Devices:
11.png
11.png (17.8 KiB) Viewed 6284 times
What is not working, is this (IDX256) item in Weather
12.png
12.png (4.79 KiB) Viewed 6284 times
but I don't mind that. I want to use it in Blocky please.

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 13:29
by gizmocuz
That is probably because you use the wrong amount of parameters for the device
See the Wiki/JSON what values a rain sensor should have (https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s)

But again, if you just add the normal WU hardware (no script what so every), and i could make it (not today i think) that in blockly you can use

if (myrain>10mm) then....

Then you could work with this ?

Since you are 'programming' in lua, you might want to have a look in the javascript/cpp code if you want it implemented fast

Re: Virtual sensor for rain

Posted: Sunday 03 June 2018 16:49
by packetloss
gizmocuz wrote: Sunday 03 June 2018 13:29 That is probably because you use the wrong amount of parameters for the device See the Wiki/JSON what values a rain sensor should have (https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s)
I will have a look at that.
gizmocuz wrote: Sunday 03 June 2018 13:29But again, if you just add the normal WU hardware (no script what so every),
I do that. Just so you know, this one is working perfect:
13.png
13.png (7.63 KiB) Viewed 6258 times
So I do have a working WU hardware setup. The only thing I want extra is an item/device with the rain in mm *only*.

Thats what I do, the normal WU hardware. The only addition (so not instead of) is what I read on https://www.domoticz.com/wiki/Virtual_w ... emperature
gizmocuz wrote: Sunday 03 June 2018 13:29 and i could make it (not today i think) that in blockly you can use if (myrain>10mm) then.... Then you could work with this ?
correct! Thank you.

Re: Virtual sensor for rain

Posted: Monday 04 June 2018 7:58
by gizmocuz
I had a look.... (quick look)

In the next beta i remove the 'NOT WORKING YET', as a lot (if not all) weather sensors are supported

In blockly, with the rain sensor, it checks if the "amount of rain in the last hour",

if (rain>10) means, if there was more then 10mm of rain in the last hour, then do something

if you want to work with the total rain for a day, in your lua script, you have to use this table:

otherdevices_rain

otherdevices_rain_lasthour

I'm no lua developer, but our wiki helps.

Another option is to use dzVents

Re: Virtual sensor for rain

Posted: Monday 04 June 2018 20:13
by packetloss
gizmocuz wrote: Monday 04 June 2018 7:58 I had a look.... (quick look) In the next beta i remove the 'NOT WORKING YET', as a lot (if not all) weather sensors are supported
In blockly, with the rain sensor, it checks if the "amount of rain in the last hour",
if (rain>10) means, if there was more then 10mm of rain in the last hour, then do something
if you want to work with the total rain for a day, in your lua script, you have to use this table
Ok, thank you! I keep an eye on the beta updates.
I'm not using Lua for my sprinklers, only Blocky. Do I understand correctly, in Blocky I can only use the last hour, not the last 12 or 24 hours?

Re: Virtual sensor for rain

Posted: Monday 04 June 2018 21:48
by packetloss
I installed the update ( Compile Date: 2018-06-04 08:19:42 Version: 3.9571) and indeed, the "NOT WORKING YET" is removed. Unfortunately, my Blocky is not working yet. Can you please have look, whats going wrong?

The script:
14.png
14.png (13.71 KiB) Viewed 6200 times
and the value:
15.png
15.png (2.42 KiB) Viewed 6200 times
and the syslog output:

Code: Select all

Jun  4 21:47:00 domoticz: EventSystem: Lua script error (Blockly), Name: sprinkler_1 => [string "result = 0; weekday = os.date('*t')['wday']; ..."]:1: attempt to compare nil with number

Re: Virtual sensor for rain

Posted: Monday 04 June 2018 22:04
by packetloss
O and by the way, this one is working;
16.png
16.png (10.21 KiB) Viewed 6200 times
So I conclude that the problem is with the WeatherData-Rain-IDX256 <= 1 item.

Re: Virtual sensor for rain

Posted: Monday 04 June 2018 22:41
by Simania
I'm looking for exactly the same.
If my rainsensor has a certain amount of rain (better to say a lack of it) for the past 24 hours, then turn on a Z-wave switch, which controls the valve for my sprinkler.
Lots of thanks in advance!

Re: Virtual sensor for rain

Posted: Tuesday 05 June 2018 7:36
by gizmocuz
@simania, the 'compare' value for the rain sensor is the amount of rain for the last hour already

@both, i would say, start simple, make a blockly that just 'prints' the value.

For example, create a dummy rain sensor, and a blockly like:

On the 'devices' page you see the index value of the rain sensor (idx) like 1103.
Rain1
Rain1
rain1.PNG (7.91 KiB) Viewed 6184 times
note that in the log message, weatherdevice[1103] = rain_rate and when you use raindevice[1103] it is total_rain of today

When you now update the virtual rain sensor with

Code: Select all

http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=1103&nvalue=0&svalue=2;1000
It should print in the log:
2018-06-05 07:26:09.292 Status: Rain from last hour is: 0.02
If we want to do something when there is more then 5mm of rain in the last hour we could make a blockly like:
Rain2
Rain2
Rain2.PNG (7.97 KiB) Viewed 6184 times
and update the virtual sensor with:

Code: Select all

http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=1103&nvalue=0&svalue=510;1081
the first part (before the ; is the rain_rate in 0.01mm, 510 = 5.1mm, the last part is the counter (always increasing) in mm
so if the first example (2;1000) was more then 5 minutes ago, you should now have two values in the database (log) after 5 minutes, 1000 and 1081, 1081-1000=81mm of rain today

How to use the total rain today in an action is for part 2

Re: Virtual sensor for rain

Posted: Tuesday 05 June 2018 8:05
by gizmocuz
Using total rain for today in an action:

For this we are going to use dzVents, it is in the same editor as Blockly, but then choose dzVents

We are going to (Always!!) start simple, then build the final script, so lets start with printing the values:

Code: Select all

return {
	on = {
		devices = {
			'DumRain'
		}
	},
	execute = function(domoticz, device)
	    --[[
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		domoticz.log('rain_rate: ' .. device.rainRate, domoticz.LOG_INFO)
		domoticz.log('total_rain: ' .. device.rain, domoticz.LOG_INFO)
		]]--
	end
}
When we now execute the json query:

Code: Select all

http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=1103&nvalue=0&svalue=510;1081
it will output in the log:
2018-06-05 07:48:07.545 Status: dzVents: Info: Handling events for: "DumRain", value: "511;1081"
2018-06-05 07:48:07.545 Status: dzVents: Info: ------ Start internal script: TestTotalRain: Device: "DumRain (Dummy)", Index: 1103
2018-06-05 07:48:07.546 Status: dzVents: Info: Device DumRain was changed
2018-06-05 07:48:07.547 Status: dzVents: Info: rain_rate: 5.1100001335144
2018-06-05 07:48:07.547 Status: dzVents: Info: total_rain: 81
2018-06-05 07:48:07.547 Status: dzVents: Info: ------ Finished TestTotalRain
Perfect! all the values we need... now lets switch on a light when there is to much rainfall today (>80mm)
For this i added a dummy switch called 'MyDummySwitch'

Here is the new script:

Code: Select all

return {
	on = {
		devices = {
			'DumRain'
		}
	},
	execute = function(domoticz, device)

		local my_light = domoticz.devices('MyDummySwitch')

		if (device.rain>80) then
            domoticz.log('more then 80mm of rain today!!', domoticz.LOG_INFO)
	        my_light.switchOn()
	    else
	        my_light.switchOff()
		end
	end
}

Now of course, on every rain change the light will be switched on/off, and probably you would not want this, so there are several options to prevent this, so lets do a simple one:

Code: Select all

return {
	on = {
		devices = {
			'DumRain'
		}
	},
	execute = function(domoticz, device)

		local my_light = domoticz.devices('MyDummySwitch')
        	local bTurnOn = false
		if (device.rain>80) then
	            domoticz.log('more then 80mm of rain today!!', domoticz.LOG_INFO)
        	    bTurnOn = true;
	        end
        	if (my_light.bState ~= bTurnOn) then
		            if (bTurnOn == true) then
		                domoticz.log('switching light ON', domoticz.LOG_INFO)
			         my_light.switchOn()
			     else
		                domoticz.log('switching light OFF', domoticz.LOG_INFO)
			        my_light.switchOff()
			     end
		end
	end
}
In the installation folder of domoticz there is a folder 'dzVents' with documentation, and also a scripts/dzVents/examples

Re: Virtual sensor for rain

Posted: Tuesday 05 June 2018 21:39
by Simania
@gizmocuz wow what a detailed and quick response! I really appreciate this!
I will try both methods to see what is best fit and will let you know of the outcome.
Once again, your time is much appreciated.

Re: Virtual sensor for rain

Posted: Tuesday 12 June 2018 17:14
by Simania
Gizmocus,

The first example works perfectly, but the second, with DzVents not.
If I execute the json query, in my example my ip is 192.168.178.98 and the id of the 'DumRain' device is 162:
http://192.168.178.98:8080/json.htm?typ ... e=580;2024

I get the following output, the name of the event is 'Sproeien':
2018-06-12 17:01:12.665 Status: dzVents: Info: Handling events for: "DumRain", value: "510;1081"
2018-06-12 17:01:12.665 Status: dzVents: Info: ------ Start internal script: Sproeien: Device: "DumRain (WeatherData)", Index: 162
2018-06-12 17:01:12.665 Status: dzVents: Info: ------ Finished Sproeien

Somehow no values show up, where as in the first example evverything works like it should, see my ouput:
2018-06-12 16:55:49.339 Status: Too much rain this last hour!! (5.1)

Any thought?
Here is the event I copied from this thread:

Code: Select all

return {
	on = {
		devices = {
			'DumRain'
		}
	},
	execute = function(domoticz, device)
	    --[[
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		domoticz.log('rain_rate: ' .. device.rainRate, domoticz.LOG_INFO)
		domoticz.log('total_rain: ' .. device.rain, domoticz.LOG_INFO)
		]]--
	end
}
Regards,
Simania

Re: Virtual sensor for rain

Posted: Tuesday 12 June 2018 17:48
by waaren
the --[[ means the start of a multiline comment
the ]] means the end of a multiline comment
the -- means the start of a single line comment

so effectively everything between --[[
and
]]--
including the entire line after ]]-- is not interpreted as code by Lua / dzVents.

This should work

Code: Select all

return {
	on = {
		devices = {
			'DumRain'
		}
	},
	execute = function(domoticz, device)

		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
		domoticz.log('rain_rate: ' .. device.rainRate, domoticz.LOG_INFO)
		domoticz.log('total_rain: ' .. device.rain, domoticz.LOG_INFO)

	end
}

Re: Virtual sensor for rain

Posted: Wednesday 13 June 2018 10:10
by Simania
Thanks waaren, I'm pretty new to this dzVents coding stuff.
It works now btw.

Re: Virtual sensor for rain

Posted: Wednesday 13 June 2018 15:12
by Simania
Okey I used the following code in dzVents together with my Netatmo rainsensor:

Code: Select all

return {
   on = {
      timer = { 'every minute' },
   },
	execute = function(domoticz, timer)
	local netatmo_rain = domoticz.devices(23)	
		domoticz.log('rain_rate: ' .. netatmo_rain.rainRate, domoticz.LOG_INFO)
		domoticz.log('total_rain: ' .. netatmo_rain.rain, domoticz.LOG_INFO)
    end
}
And it gives met the following output in the log:

2018-06-13 15:07:00.253 Status: dzVents: Info: ------ Start internal script: Netatmo:, trigger: every minute
2018-06-13 15:07:00.276 Status: dzVents: Info: rain_rate: 0
2018-06-13 15:07:00.276 Status: dzVents: Info: total_rain: 0.10000000149012
2018-06-13 15:07:00.276 Status: dzVents: Info: ------ Finished Netatmo

I find it kind of strange that I get a trillion digit number for the total_rain. Is it possible to use it like this with my Netatmo rainsensor, or is this number just something different?

Regards in advance!

Re: Virtual sensor for rain

Posted: Thursday 14 June 2018 7:09
by gizmocuz
@Simania, this is a float representation, it is safe to use it, it is 0.1