Page 1 of 1

get value of virtual sensor in if statement

Posted: Thursday 01 August 2019 8:35
by sailmich
Rpi3, zStick, newest beta.
I would like to turn on a wall socket for my garden pump. But only when the todays rain rate of my personal weather station is lower than 0.5. I have a virtual sensor called "Weatherstation_Todaysrain" the sensor shows the todays rain rate. My code doesn't work seems that I don't get the value.
Spoiler: show
return {
on = {
timer = {'at 07:52','at 23:13 '
}
},
execute = function(domoticz, device)
if domoticz.devices('Weatherstation_Todaysrain').nValue <= 0.5 then

domoticz.devices('Wandstecker').switchOn().forMin(30)

end
end
}
When calling the status with json I got
Spoiler: show

ActTime 1564640893
AstrTwilightEnd "00:22"
AstrTwilightStart "02:33"
CivTwilightEnd "21:56"
CivTwilightStart "04:58"
DayLength "15:34"
NautTwilightEnd "22:54"
NautTwilightStart "04:00"
ServerTime "2019-08-01 08:28:13"
SunAtSouth "13:27"
Sunrise "05:40"
Sunset "21:14"
app_version "4.11062"
result
0
AddjMulti 1
AddjMulti2 1
AddjValue 0
AddjValue2 0
BatteryLevel 255
CustomImage 11
Data "4.2 mm"
Description ""
Favorite 0
HardwareID 3
HardwareName "Dummys"
HardwareType "Dummy (Does nothing, use for virtual switches only)"
HardwareTypeVal 15
HaveTimeout true
ID "00082174"
Image "Water"
LastUpdate "2019-07-31 23:23:18"
Name "Weatherstation_Todaysrain"
Notifications "false"
PlanID "0"
PlanIDs
0 0
Protected false
SensorType 1
SensorUnit "mm"
ShowNotifications true
SignalLevel "-"
SubType "Custom Sensor"
Timers "false"
Type "General"
TypeImg "Water"
Unit 1
Used 1
XOffset "0"
YOffset "0"
idx "174"
status "OK"
title "Devices"
I need the value behind Data.
Any help appreciated :P

Re: get value of virtual sensor in if statement

Posted: Thursday 01 August 2019 9:39
by waaren
sailmich wrote: Thursday 01 August 2019 8:35 I have a virtual sensor called "Weatherstation_Todaysrain" the sensor shows the todays rain rate. My code doesn't work seems that I don't get the value.
The value you are looking for is likely to be stored in the sValue field (= rawData table in dzVents). Small complication is that it could be stored as a combination of the value (4.2) and the unit (mm) so we have carve out the value. In Lua you can use the string.match function for that.

Can you try this ?

Code: Select all

return {
    on = 
    {
        timer = 
        {
            'at 07:52',
            'at 23:13', 
        }
    },

    execute = function(domoticz)
        local floatFormat =  '%d*%.*%d*'
        local rawRainToday = domoticz.devices('Weatherstation_Todaysrain').rawData[1] -- '4.2 mm'
        local rainToday = tonumber(string.match(rawRainToday, floatFormat)) -- 4.2
    
        if rainToday <= 0.5 then
            domoticz.devices('Wandstecker').switchOn().forMin(30)
        end
    end
}


Re: get value of virtual sensor in if statement  [Solved]

Posted: Thursday 01 August 2019 10:03
by sailmich
@waaren
Thank you very much! It worked as it should.