Send notification after switch been off for 2h

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Ivanhoe1
Posts: 20
Joined: Monday 19 December 2016 20:42
Target OS: Windows
Domoticz version:
Contact:

Send notification after switch been off for 2h

Post by Ivanhoe1 »

Hi,
Cant figure out how to do what seems be a easy thing. I have an Nexa transmitter that sends on/off to domiticz when my robotic lawnmover leaves and return for charging. I want domoticz to send me an notification when the robot been away for 2 hours. How could I do this? In other word, when therese been Off recieved from the switch for mote then 2h i will get the notification.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Send notification after switch been off for 2h

Post by waaren »

Ivanhoe1 wrote: Tuesday 11 June 2019 20:05 Hi,
Cant figure out how to do what seems be a easy thing. I have an Nexa transmitter that sends on/off to domoticz when my robotic lawnmower leaves and return for charging. I want domoticz to send me an notification when the robot been away for 2 hours. How could I do this? In other words, when there has been no Off received from the switch for more then 2 hours i will get the notification.
Unfortunately there is no native domoticz timer to help here. The script below uses the afterNNN method of dzVents to emulate this.

When not yet familiar with dzVents please start with reading Get started Before implementing. Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not 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

scriptVar = "yourSwitchDelay" 
lawnmowerSwitch = "lawnmowerActive"

return {
    on  = {     devices = { lawnmowerSwitch }, -- change to 'name' or ID of your lawnMower active switch
                httpResponses   =  { scriptVar}},
                
    logging =   {   level     =   domoticz.LOG_DEBUG,           
                    marker    =   scriptVar    },
                
    execute = function(dz, item)
        local  delay  =  7200               -- delay in seconds Adjust to your needs
        local  jsonString   = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=Waiting%20is%20over"

        if item.isHTTPResponse then
            if dz.devices(lawnmowerSwitch).active and lawnmowerSwitch.lastUpdate.secondsAgo > ( delay - 100 ) then
                dz.notify('Lawnmower','Lawnmower away for '.. lawnmowerSwitch.lastUpdate.secondsAgo ..' seconds',dz.PRIORITY_NORMAL)
            end
        elseif item.active then -- lawnmowerSwitch switched to On
           dz.openURL  ({ url      = jsonString,  
                          method   = "GET", 
                          callback =  scriptVar }).afterSec(delay)             -- to come back after delay seconds 
        end                                      
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ivanhoe1
Posts: 20
Joined: Monday 19 December 2016 20:42
Target OS: Windows
Domoticz version:
Contact:

Re: Send notification after switch been off for 2h

Post by Ivanhoe1 »

Thanks alot waaren for the help! :D
Ivanhoe1
Posts: 20
Joined: Monday 19 December 2016 20:42
Target OS: Windows
Domoticz version:
Contact:

Re: Send notification after switch been off for 2h

Post by Ivanhoe1 »

Tried the script but ran in to a problem with "attempt to index field 'lastUpdate' (a nil value) ". Log below.
Should the uservarible be an integer?


2019-06-18 22:13:01.395 Status: User: Admin initiated a switch command (244/Lawnmoverswitch/Off)
2019-06-18 22:13:01.537 Status: dzVents: Info: LawnmoverGone: ------ Start internal script: Robot4: Device: "Lawnmoverswitch (RFXCOM)", Index: 244
2019-06-18 22:13:01.537 Status: dzVents: Info: LawnmoverGone: ------ Finished Robot4
2019-06-18 22:13:05.981 (RFXCOM) Temp + Humidity (Kallvind temp)
2019-06-18 22:13:09.264 (RFXCOM) Lighting 2 (Lawnmoverswitch)
2019-06-18 22:13:09.256 Status: User: Admin initiated a switch command (244/Lawnmoverswitch/On)
2019-06-18 22:13:09.408 Status: dzVents: Info: LawnmoverGone: ------ Start internal script: Robot4: Device: "Lawnmoverswitch (RFXCOM)", Index: 244
2019-06-18 22:13:09.408 Status: dzVents: Debug: LawnmoverGone: OpenURL: url = http://127.0.0.1:8088/json.htm?type=com ... 0is%20over
2019-06-18 22:13:09.408 Status: dzVents: Debug: LawnmoverGone: OpenURL: method = GET
2019-06-18 22:13:09.408 Status: dzVents: Debug: LawnmoverGone: OpenURL: post data = nil
2019-06-18 22:13:09.408 Status: dzVents: Debug: LawnmoverGone: OpenURL: headers = nil
2019-06-18 22:13:09.408 Status: dzVents: Debug: LawnmoverGone: OpenURL: callback = LawnmoverGone
2019-06-18 22:13:09.409 Status: dzVents: Info: LawnmoverGone: ------ Finished Robot4
2019-06-18 22:13:09.409 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-06-18 22:13:14.421 Status: Waiting is over
2019-06-18 22:13:14.539 Status: dzVents: Info: LawnmoverGone: ------ Start internal script: Robot4: HTTPResponse: "LawnmoverGone"
2019-06-18 22:13:14.581 Status: dzVents: Debug: LawnmoverGone: Processing device-adapter for Lawnmoverswitch: Switch device adapter
2019-06-18 22:13:14.581 Status: dzVents: Error (2.4.19): LawnmoverGone: An error occured when calling event handler Robot4
2019-06-18 22:13:14.581 Status: dzVents: Error (2.4.19): LawnmoverGone: ...pi/domoticz/scripts/dzVents/generated_scripts/Robot4.lua:16: attempt to index field 'lastUpdate' (a nil value)
2019-06-18 22:13:14.581 Status: dzVents: Info: LawnmoverGone: ------ Finished Robot4
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Send notification after switch been off for 2h

Post by waaren »

Ivanhoe1 wrote: Tuesday 18 June 2019 22:19 Tried the script but ran in to a problem with "attempt to index field 'lastUpdate' (a nil value) ". Log below.
Should the uservarible be an integer?
No I made a couple of mistakes in copy / pasting the script to the forum. Probably a bit too late or too much wine at that time of night :D and some confusion about the devicename lawnmower vs lawnmover. I now followed you and used the string 'lawnmover'
Can you please try again with this ?

Code: Select all

scriptVar = "yourSwitchDelay" 
lawnmoverSwitch = "lawnmoverActive"

return {
    on  = {     devices = { lawnmoverSwitch }, -- change to 'name' or ID of your lawnMower active switch
                httpResponses   =  { scriptVar}},
                
    logging =   {   level     =   domoticz.LOG_DEBUG,           
                    marker    =   scriptVar    },
                
    execute = function(dz, item)
        local  delay  =  7200               -- delay in seconds Adjust to your needs
        local  jsonString   = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=addlogmessage&message=Waiting%20is%20over"

        if item.isHTTPResponse then
            mySwitch = dz.devices(lawnmoverSwitch)
            if mySwitch.active and mySwitch.lastUpdate.secondsAgo > ( delay - 100 ) then
                dz.notify('Lawnmover','Lawnmover away for '.. mySwitch.lastUpdate.secondsAgo ..' seconds',dz.PRIORITY_NORMAL)
            end
        elseif item.active then -- lawnmoverSwitch switched to On
           dz.openURL  ({ url      = jsonString,  
                          method   = "GET", 
                          callback =  scriptVar }).afterSec(delay)             -- to come back after delay seconds 
        end                                      
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ivanhoe1
Posts: 20
Joined: Monday 19 December 2016 20:42
Target OS: Windows
Domoticz version:
Contact:

Re: Send notification after switch been off for 2h

Post by Ivanhoe1 »

Works like a charm! Thanks! :D
Now you deserve another bottle of wine. ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest