Turn on/off switches based on Weather

Moderator: leecollings

Post Reply
jackwan1
Posts: 41
Joined: Saturday 15 April 2017 20:04
Target OS: Linux
Domoticz version: 2020.2
Location: USA
Contact:

Turn on/off switches based on Weather

Post by jackwan1 »

I have a sonoff basic switch loaded with ESP Easy, which works fine using the timer in domoticz. My goal is trying to turn on the switch at a prescribed time only if certain weather conditions are met, such as if the wind is less than x Km/h and or temp is in a certain range (whatever it is in the weather forecast section of the Domoticz). Is this possible?

Thanks a million.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn on/off switches based on Weather

Post by waaren »

jackwan1 wrote: Saturday 24 October 2020 4:03 My goal is trying to turn on the switch at a prescribed time only if certain weather conditions are met, such as if the wind is less than x Km/h and or temp is in a certain range (whatever it is in the weather forecast section of the Domoticz). Is this possible?
Should be possible with a script. Domoticz retrieve the data used in the forecast from https://forecast.io/forecast?q=<lat,lon>

Weather forecasts available are current, hourly and daily. (hourly forecasts: 48, daily forecasts: 8)
Spoiler: show

Code: Select all

 hourly:
     summary: Rain starting tonight.
     data:
             1:
                     humidity: 1
                     ozone: 285.5
                     visibility: 16.093
                     summary: Mostly Cloudy
                     apparentTemperature: 11.01
                     precipType: rain
                     cloudCover: 0.85
                     windSpeed: 21.01
                     pressure: 1010.9
                     time: 1603515600
                     uvIndex: 0
                     windBearing: 217
                     windGust: 38.59
                     icon: partly-cloudy-night
                     precipIntensity: 0.0738
                     dewPoint: 10.95
                     precipProbability: 0.15
                     temperature: 11.01
             2:
                     humidity: 0.99
                     ozone: 284.9
                     visibility: 16.093
                     summary: Mostly Cloudy
                     apparentTemperature: 11.02
                     precipType: rain
                     cloudCover: 0.8
                     windSpeed: 22.83
                     pressure: 1010.4
                     time: 1603519200
                     uvIndex: 0
                     windBearing: 212
                     windGust: 39.86
                     icon: partly-cloudy-night
                     precipIntensity: 0.0718
                     dewPoint: 10.92
                     precipProbability: 0.12
                     temperature: 11.02
             3:
             ....
             49:
                     humidity: 0.98
                     ozone: 325.4
                     visibility: 16.093
                     summary: Partly Cloudy
                     apparentTemperature: 5.73
                     precipType: rain
                     cloudCover: 0.47
                     windSpeed: 21.11
                     pressure: 1000.3
                     time: 1603688400
                     uvIndex: 0
                     windBearing: 207
                     windGust: 45.36
                     icon: partly-cloudy-night
                     precipIntensity: 0.0076
                     dewPoint: 8.42
                     precipProbability: 0.08
                     temperature: 8.77

 daily:
     summary: Rain today through Friday.
     data:
             1:
                     windGustTime: 1603576800
                     apparentTemperatureLow: 12.03
                     temperatureMax: 15.86
                     apparentTemperatureLowTime: 1603605600
                     uvIndexTime: 1603538880
                     apparentTemperatureMinTime: 1603521660
                     precipType: rain
                     time: 1603490400
                     uvIndex: 2
                     temperatureLowTime: 1603605600
                     icon: rain
                     humidity: 0.92
                     apparentTemperatureMaxTime: 1603552800
                     precipIntensityMaxTime: 1603508340
                     apparentTemperatureMax: 15.58
                     ozone: 283.3
                     precipIntensityMax: 0.1131
                     temperatureMin: 10.72
                     moonPhase: 0.29
                     summary: Possible light rain in the morning and overnight.
                     temperatureMaxTime: 1603552800
                     pressure: 1008.8
                     apparentTemperatureMin: 10.99
                     sunriseTime: 1603520640
                     cloudCover: 0.88
                     apparentTemperatureHigh: 15.58
                     windSpeed: 26.87
                     windBearing: 206
                     visibility: 16.093
                     temperatureHigh: 15.86
                     apparentTemperatureHighTime: 1603552800
                     windGust: 67.48
                     temperatureHighTime: 1603552800
                     temperatureMinTime: 1603521660
                     dewPoint: 11.84
                     precipIntensity: 0.0368
                     sunsetTime: 1603557000
                     precipProbability: 0.51
                     temperatureLow: 11.76
             2:
                     windGustTime: 1603580760
                     apparentTemperatureLow: 5.41
                     temperatureMax: 14.72
                     apparentTemperatureLowTime: 1603692000
                     uvIndexTime: 1603624860
                     apparentTemperatureMinTime: 1603664940
                     precipType: rain
                     time: 1603576800
                     uvIndex: 1
                     temperatureLowTime: 1603692000
                     icon: rain
                     humidity: 0.89
                     apparentTemperatureMaxTime: 1603576800
                     precipIntensityMaxTime: 1603605780
                     apparentTemperatureMax: 14.44
                     ozone: 301.3
                     precipIntensityMax: 1.4301
                     temperatureMin: 9.5
                     moonPhase: 0.32
                     summary: Rain throughout the day.
                     temperatureMaxTime: 1603576800
                     pressure: 1001.6
                     apparentTemperatureMin: 7.27
                     sunriseTime: 1603607160
                     cloudCover: 0.93
                     apparentTemperatureHigh: 13.2
                     windSpeed: 24.3
                     windBearing: 207
                     visibility: 15.311
                     temperatureHigh: 13.48
                     apparentTemperatureHighTime: 1603634400
                     windGust: 68.88
                     temperatureHighTime: 1603634400
                     temperatureMinTime: 1603664520
                     dewPoint: 10.61
                     precipIntensity: 0.3633
                     sunsetTime: 1603643280
                     precipProbability: 0.97
                     temperatureLow: 8.21
            ....
             8:
                     windGustTime: 1604134440
                     apparentTemperatureLow: 5.93
                     temperatureMax: 15.79
                     apparentTemperatureLowTime: 1604199720
                     uvIndexTime: 1604144280
                     apparentTemperatureMinTime: 1604124000
                     precipType: rain
                     time: 1604098800
                     uvIndex: 1
                     temperatureLowTime: 1604199660
                     icon: wind
                     humidity: 0.86
                     apparentTemperatureMaxTime: 1604145240
                     precipIntensityMaxTime: 1604122440
                     apparentTemperatureMax: 15.51
                     ozone: 292.8
                     precipIntensityMax: 0.0602
                     temperatureMin: 11.03
                     moonPhase: 0.51
                     summary: Windy in the morning.
                     temperatureMaxTime: 1604145240
                     pressure: 1014.4
                     apparentTemperatureMin: 11.3
                     sunriseTime: 1604126220
                     cloudCover: 0.48
                     apparentTemperatureHigh: 15.51
                     windSpeed: 21.68
                     windBearing: 212
                     visibility: 16.093
                     temperatureHigh: 15.79
                     apparentTemperatureHighTime: 1604145240
                     windGust: 67.44
                     temperatureHighTime: 1604145240
                     temperatureMinTime: 1604124000
                     dewPoint: 10.5
                     precipIntensity: 0.0182
                     sunsetTime: 1604161020
                     precipProbability: 0.21
                     temperatureLow: 7.96
     icon: rain
 currently:
     humidity: 0.99
     ozone: 285
     visibility: 16.093
     summary: Mostly Cloudy
     apparentTemperature: 11.01
     precipType: rain
     cloudCover: 0.8
     windSpeed: 22.46
     pressure: 1010.5
     time: 1603518378
     uvIndex: 0
     windBearing: 214
     windGust: 39.72
     icon: partly-cloudy-night
     precipIntensity: 0.0732
     dewPoint: 10.92
     precipProbability: 0.13
     temperature: 11.01
Happy to help but need something more to work with.
  • what is the name of the switch that needs to be controlled?
  • what time(s) should the script run?
  • what are the (forecasted weather) criteria that need to be evaluated?
    • types?
    • how far in the future?
    • average or extremes?
  • what values do you want the switch to be Off/On?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jackwan1
Posts: 41
Joined: Saturday 15 April 2017 20:04
Target OS: Linux
Domoticz version: 2020.2
Location: USA
Contact:

Re: Turn on/off switches based on Weather

Post by jackwan1 »

Thank you and to answer your question @waaren

1. By name of the switch do you mean the name of the virtual switch? Its called sonoff7 idx 13 its always off with a weekly on time
2. Time, will be weekly, Friday, at 17:00 on and off 15 min. Later or Saturday the same time if the Friday weather is too windy
3. Weather condition is current Wind <2mph then switch on otherwise skip
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Turn on/off switches based on Weather

Post by waaren »

jackwan1 wrote: Saturday 24 October 2020 16:40 Thank you and to answer your question @waaren

1. By name of the switch do you mean the name of the virtual switch? Its called sonoff7 idx 13 its always off with a weekly on time
2. Time, will be weekly, Friday, at 17:00 on and off 15 min. Later or Saturday the same time if the Friday weather is too windy
3. Weather condition is current Wind <2mph then switch on otherwise skip
Can you try below script?
_____________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
_____________________________________________________________________________________________________________________________

Code: Select all

--[[
        This script collects weather forecast data from https://forecast.io/forecast?q=<lat,lon>&lang=en-us&units=auto // based on your location

        Please note that dzVents requires acces to domoticz at a sufficient level to retrieve the location settings
        including the latititude and longitude. It can only get these if the Local Networks (no username/password)
        include 127.0.0.1 (or  ::1 for systems using IPv6)
        Explained in the wiki at:  https://www.domoticz.com/wiki/DzVents:_next_generation_LUA_scripting#Using_dzVents_with_Domoticz

        original script posted:  https://www.domoticz.com/forum/viewtopic.php?f=11&t=34308&p=259306#p259306

        history
        20201024 Start coding - shared on forum
       
]]--

local scriptVar = 'ForecastIO'

return
{
    on = {
        timer =
        {
            'at 17:00 on fri,sat' -- change to time(s) you want this script to execute
        },
        
        devices =
        {
            'trigger' .. scriptVar, -- just for test. Can be ignored
        },
        
        httpResponses =
        {
            scriptVar,
        },
    },
    
    data =
    {
        actioned =
        {
            initial = false,
        },
    },
    
    logging =
    {
            level = domoticz.LOG_DEBUG, -- change to domoticz.ERROR when script works as designed
            marker = scriptVar,
    },

    execute = function(dz, item)
       -- ******** Your settings  below this line
       
       weeklySwitch = dz.devices('sonoff7') -- set to name of switch you want to control
       maxWindSpeed = 2 -- Check this as it seems quite low 
       
       -- ******** No changes required below this line

        local forecastURL = 'https://forecast.io/forecast?lang=en-us&units=us&q='
 
        local function sendURL()
            dz.openURL(
            {
                url = forecastURL  ..  dz.settings.location.latitude .. ',' .. dz.settings.location.longitude ,
                callback = scriptVar, -- see httpResponses above.
            })
        end
        
        local function processReturn(t)
            dz.log('current windSpeed is ' .. t.windSpeed, dz.LOG_DEBUG )
            if t.windSpeed <= maxWindSpeed then -- if friday and saturday too windy then no action at all. Is this correct?
                weeklySwitch.switchOn()
                weeklySwitch.switchOff().afterMin(15)
                dz.data.actioned = true
            end
        end
        
        -- main code
        if item.isHTTPResponse then
            if item.ok and item.isJSON then
                processReturn(item.json.currently)
            else
                dz.log('There was a problem with the response from ' .. scriptVar, dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
            end
        else -- get the data from forecastIO
            -- if dz.time.matchesRule('on fri') then 
            if dz.time.matchesRule('on fri') then 
                sendURL()
                dz.data.actioned = false
            elseif dz.data.actioned ~= true then -- no action was done on Friday
                sendURL()
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jackwan1
Posts: 41
Joined: Saturday 15 April 2017 20:04
Target OS: Linux
Domoticz version: 2020.2
Location: USA
Contact:

Re: Turn on/off switches based on Weather

Post by jackwan1 »

Thank you @waaren
I will try that
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest