is it possible to trigger scripts by device's state?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

is it possible to trigger scripts by device's state?

Post by rkarolek »

Is it possible to run script in specified time range, but only if some devices are off?
For example id like to check if lights are off after lets say 8pm,
if not id like to try switch them off and check in 5min,
if yes id like not to start script any more.

I hope you can understand my problem :)
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: is it possible to trigger scripts by device's state?

Post by emme »

you can trigger by state change in a specific time window using:

Code: Select all

on = {
    devices = { 'mylight' = { 'between 20:00 and 20:05' }
but to answer you question I assume it could be like:

Code: Select all

on = {
    timer= { 'every 5 minutes between 20.00 and 23:59' }
    }
   execute = function(domoticz, security)
      domoticz.devices('mylight').switchOff().checkFirst()
   end
   }
this will check at 20:00, 20:05, 20:10 etc etc and switch off your light only if needed
ciao
M
The most dangerous phrase in any language is:
"We always done this way"
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: is it possible to trigger scripts by device's state?

Post by rkarolek »

yes, i know that, but script is triger and in case many scripts can make some mesh in network.
would be nice if script will triger for example between 8 and 10pm but only if specify device value is on.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: is it possible to trigger scripts by device's state?

Post by waaren »

rkarolek wrote: Wednesday 03 October 2018 13:24 Is it possible to run script in specified time range, but only if some devices are off?
For example id like to check if lights are off after lets say 8pm,
if not id like to try switch them off and check in 5min,
if yes id like not to start script any more.

I hope you can understand my problem :)
My preference would be to use the approach @emme used but if you don't want to execute your script, a dzVents solution could be

Code: Select all

-- active when function returns true (  )

local myTimerange = "at 15:33-15:55"       -- your Timeslot
local myLightIDX  = 535            	-- Your light's idx

return {
    active = function(domoticz)                                -- This function will be evaluated every minute by the dzVents system
                myLight = domoticz.devices(myLightIDX)  
                return myLight.state == "On"
            end,

    on     = {  timer   = { myTimerange .. " every 5 minutes" }},
            
    execute = function(dz)
       myLight.switchOff()
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: is it possible to trigger scripts by device's state?

Post by emme »

rkarolek wrote: Wednesday 03 October 2018 15:28 yes, i know that, but script is triger and in case many scripts can make some mesh in network.
would be nice if script will triger for example between 8 and 10pm but only if specify device value is on.
script like the above does not mesh....
the .checkFirst() function switch the lights off ONLY if needed, but... I assume you want a timer within that window... so
if lights is switched on between 8pm and 10pm then switch it off after 5 minutes... right?

ok.. if so:

Code: Select all

on = {
    devices = { 
        'mylight' = { 'between 20:00 and 22:00' }
    }
   execute = function(domoticz, myLight)
        domoticz.devices('mylight').cancelQueuedCommands()
        if myLight.state == 'On' then
            domoticz.devices('mylight').switchOff().afterMin(5)
        end
   end
   }
the script is triggered ONLY between 8 and 10 pm and WHEN the myLight device changes its state (On/Off)
if on.. empty the queued messages and apply an Off Command delayed 5 mins

so... it'n 9pm...
Manual Switch On ==> queued 5 min before Off
Manual Switch Off ==> Delayed Off Command Canceled

Manual Switch On ==> queued 5 min before Off
after 5 mins ==> Auto Off
The most dangerous phrase in any language is:
"We always done this way"
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: is it possible to trigger scripts by device's state?

Post by rkarolek »

ty emme for your help, but its not exactly what i need.
i use domoticz to control lights in my aquarium, during last holidays i had problem with conection between my nodes.
I set lights switch and lights off in specific time (two lamps, so two on and two off times), coz my problem with mysensor networks lights not on or what worse not off. Now i try secure for next time. at the moment i triger script every minute and check lights by .checkFirst(), but i though that it can be made in other (maybe simple) way.
I hope no more problem with network but..... its just for secure (end for knowing somethin new :))

i try analyze waaren's code, but im new in dzvents, so its a bit strange for me, have to read more about it :)
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: is it possible to trigger scripts by device's state?

Post by emme »

I think (from a domoticz point of view) there are no many other ways to handle it with less script/traffic.

You said you're working on MySensors, I had it too and I discontinued it due to communication issue (nrf24 are not so brilliant as I supposed) and even using the new model did not fix my comm problems.

It could be that your problem is not on domoticz, but on mysensors....
do you have the possibility to use a different framework (like Esp8266 and WiFi)?
The most dangerous phrase in any language is:
"We always done this way"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: is it possible to trigger scripts by device's state?

Post by waaren »

rkarolek wrote: Wednesday 03 October 2018 17:06 i try analyze waaren's code, but im new in dzvents, so its a bit strange for me, have to read more about it :)
Let me try to help. Probably the confusing part is

Code: Select all

active = function(domoticz)                                -- This function will be evaluated every minute by the dzVents system
                myLight = domoticz.devices(myLightIDX)  
                return myLight.state == "On"
           end,
dzVents first looks at "active" to determine if the script should be executed or not. Whatever you assign to "active" ; as long it is not nil or false, will be considered as true. You can do this explicit by entering

Code: Select all

active = true
but here we do it implicit and based on the evaluation of the state of your light. The function will return true if the light is on and false otherwise. In order to be able to do the evaluation of the light's state, we have to pass the domoticz object to the function. I we don't do that, the function will not have access to information of all the domoticz devices.
This is not as bad as it sounds because Lua tables (and here the domoticz object is a Lua table) and functions, userdata and threads are reference types (sort of pointer). So we don't cause a lot of overhead because we only pass that reference to the function.

Hope this clarifies.
Last edited by waaren on Wednesday 03 October 2018 17:31, edited 1 time in total.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rkarolek
Posts: 33
Joined: Friday 31 August 2018 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: is it possible to trigger scripts by device's state?

Post by rkarolek »

@emme, you right, i have some esp-01 with easyesp working as wirelesss sensor and no problem with them.
already bought ESP2866 wemos and if will have another problem with mysensor network ill switch to ESP, but now seem all works fine (after upgrades all nodes to 2.3 and added 100u capacitor on NRF24)

@waaren, tyvm it looks great, i didnt know about active section, (missed it in manual?), so have to train it a bit :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest