Check all device is Off  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Check all device is Off

Post by djdevil »

Hello, I have an automation in mind but I am unable to write it. I ask for help!
the script should start by activating a virtual key.
It should check that all the lights and shutters are in an off state. If a device is on a different state from the one on, it sends me a notification with the device name and its status.
while if everything is off send a message that everything is ok.
You could facilitate the script by adding the devices to query with the addition of its idx. thank you
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check all device is Off

Post by waaren »

djdevil wrote: Tuesday 04 February 2020 11:59 the script should start by activating a virtual key.
It should check that all the lights and shutters are in an off state. If a device is on a different state from the one on, it sends me a notification with the device name and its status.
Could look like something below. ( This version can check for On and Off states but can easily extended with other state-checks by adding tables and add the new table names to devicesToCheck and new required state strings to requiredStates )

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'checkStates', -- change to name of (virtual) activation switch
        }
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'check States',
    },

    execute = function(dz)
        local offDevicesToCheck = { 83, 'Alarm clock', 63, 'RGBWWTest'} -- name or id of devices to check for Off state
        local onDevicesToCheck = { 70, 69, 'RGBTest'} -- name or id of devices to check for On state
        
        --- No changes required below this line
        
        local devicesToCheck = { offDevicesToCheck, onDevicesToCheck}
        local allDeviceStatesOK = true 
        local requiredStates = { 'Off', 'On' }
        
        for checkTableID, stateTable in ipairs(devicesToCheck) do
            requiredState = requiredStates[checkTableID]
                
            for _, deviceIdentifier in ipairs(stateTable) do
        
                local device = dz.devices(deviceIdentifier)
                local state = device.state
                local name = device.name
                if state ~= requiredState then 
                    allDeviceStatesOK = false 
                    dz.log('Device ' .. name .. ' has wrong state (' .. state .. ')', dz.LOG_DEBUG)        
                    dz.notify('StateChecker', 'Device ' .. name .. ' has wrong state (' .. state .. ')', dz.PRIORITY_NORMAL)
                end
        
            end
        end
    
        if allDeviceStatesOK then
            dz.log('Devices are all in the required state', dz.LOG_DEBUG)        
            dz.notify('StateChecker', 'Devices are all in the required state' , dz.PRIORITY_NORMAL)
        end
    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
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

thank you so much for your work and for the help, in case I have the blinds that report the percentage as well as the lights as I can verify?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check all device is Off

Post by waaren »

djdevil wrote: Tuesday 04 February 2020 14:04 In case I have the blinds that report the percentage as well as the lights as I can verify?
What is the state you want to check ? What do you get as notification or see in the log when checking for On or Off state ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

A practical example would be that if I close the roller shutter and it locks and remains at 50% or I have dimmable lights that if they do not go out, their status is set to 30% 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check all device is Off

Post by waaren »

djdevil wrote: Tuesday 04 February 2020 14:19 A practical example would be that if I close the roller shutter and it locks and remains at 50% or I have dimmable lights that if they do not go out, their status is set to 30% 
Sorry but I stil don't get it.
Should the script notify you when the level of the shutter is 50% or should it notify when it is not equal to 50 %
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

It should be noted that the roller shutter has not been closed and that it is 50% the same as the light has never been turned off and 30% water is found
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check all device is Off

Post by waaren »

djdevil wrote: Tuesday 04 February 2020 14:53 It should be noted that the roller shutter has not been closed and that it is 50% the same as the light has never been turned off and 30% water is found
Probably a language thing and with a risk that I am repeating myself:
The script looks for a certain state in a device. If the state found is different then what you defined as required it notifies you.
So what is the state you require ?

Is it Off, Closed or something else ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

Ok then my mistake I understood tonight I will go home and try the script and let you know
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

waaren I studied the code! I understood that everything works perfectly. Notifications arrive perfectly!

Code: Select all

os.execute('/home/pi/domoticz/alexa_remote_control.sh -d "Alexa Studio" -e speak:"Attenzione  ' .. name .. ' è acceso"')
Now I use this code to announce to alexa the devices on or closed, but you only have one, what should I do?

This all code

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Controllo Casa', -- change to name of (virtual) activation switch
        }
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'check States',
    },

    execute = function(dz)
       local offDevicesToCheck = { 151, 66,127,} -- name or id of devices to check for Off state
          
      local onDevicesToCheck = {40 } -- name of blind check if close
        
        --- No changes required below this line
        
        local devicesToCheck = { offDevicesToCheck, onDevicesToCheck}
       local allDeviceStatesOK = true
     local requiredStates = { 'Off', 'Closed' }
        
        for checkTableID, stateTable in ipairs(devicesToCheck) do
            requiredState = requiredStates[checkTableID]
                
            for _, deviceIdentifier in ipairs(stateTable) do
        
                local device = dz.devices(deviceIdentifier)
                local state = device.state
                local name = device.name
                if state ~= requiredState then 
                    allDeviceStatesOK = false 
                    dz.log('Device ' .. name .. ' has wrong state (' .. state .. ')', dz.LOG_DEBUG)        
                    dz.notify('StateChecker', 'Device ' .. name .. ' has wrong state (' .. state .. ')', dz.PRIORITY_NORMAL)
                    os.execute('/home/pi/domoticz/alexa_remote_control.sh -d "Alexa Studio" -e speak:"Attenzione  ' .. name .. ' è acceso"') -- Speak Alexa code
                end
        
            end
        end
    
        if allDeviceStatesOK then
            dz.log('Devices are all in the required state', dz.LOG_DEBUG)        
            dz.notify('StateChecker', 'Devices are all in the required state' , dz.PRIORITY_NORMAL)
        end
    end
}
Alexa should announce the names of powered on or open devices
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check all device is Off

Post by waaren »

djdevil wrote: Tuesday 04 February 2020 21:40 Now I use this code to announce to alexa the devices on or closed, but you only have one, what should I do?
Alexa should announce the names of powered on or open devices
Sorry I don't understand what you mean. Please explain in more detail.

In general it is not a good idea to use external commands (like os.execute) in a loop. It potentially can block the entire event system.
My suggestion is to create a string in the loop and send that to the /home/pi/domoticz/alexa_remote_control.sh script when the loop finished.

the dzVents could look like

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Controllo Casa', -- change to name of (virtual) activation switch
        }
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'check States',
    },

    execute = function(dz)
        local offDevicesToCheck = { 151, 66,127,} -- name or id of devices to check for Off state
        local onDevicesToCheck = {40 } -- name of blind check if close
        local requiredStates = { 'Off', 'Closed' }
        
        --- No changes required below this line
        local alexaString = {}
        local devicesToCheck = { offDevicesToCheck, onDevicesToCheck}
        local allDeviceStatesOK = true
        
        for checkTableID, stateTable in ipairs(devicesToCheck) do
            requiredState = requiredStates[checkTableID]
                
            for _, deviceIdentifier in ipairs(stateTable) do
        
                local device = dz.devices(deviceIdentifier)
                local state = device.state
                local name = device.name
                if state ~= requiredState then 
                    allDeviceStatesOK = false 
                    dz.log('Device ' .. name .. ' has wrong state (' .. state .. ')', dz.LOG_DEBUG)        
                    dz.notify('StateChecker', 'Device ' .. name .. ' has wrong state (' .. state .. ')', dz.PRIORITY_NORMAL)
                    table.insert(alexaString, name)
                end
        
            end
        end
    
        if allDeviceStatesOK then
            dz.log('Devices are all in the required state', dz.LOG_DEBUG)        
            dz.notify('StateChecker', 'Devices are all in the required state' , dz.PRIORITY_NORMAL)
        else
            -- Speak Alexa code (& is to process command in background)
            os.execute('/home/pi/domoticz/alexa_remote_control.sh -d "Alexa Studio" -e speak:"Attenzione  '  .. table.concat(alexaString, ' and ') .. ' è acceso"  &') 
        end
    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
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off  [Solved]

Post by djdevil »

waaren wrote: Wednesday 05 February 2020 1:35
djdevil wrote: Tuesday 04 February 2020 21:40 Now I use this code to announce to alexa the devices on or closed, but you only have one, what should I do?
Alexa should announce the names of powered on or open devices
Sorry I don't understand what you mean. Please explain in more detail.

Sorry for my English!

With your last modification on the code I can do what I thought.
In practice, when a virtual button is activated, there is a control of open or on devices, if all goes well alexa announces the message that everything is closed and off or announces the names of the devices in an error state!

Perfect Thank you very much I hope it will serve everyone.

If you want you can change the title of the post more appropriately. Thank you 8-)
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Check all device is Off

Post by djdevil »

hello waaren I perfectly use the code written by you long ago, now I need to make a change. in practice, in summer I never completely close the shutters and therefore my 7 shutters are all positioned with a different percentage. how can i get it to check the percentage level? instead if open or closed?


Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Controllo Casa', -- change to name of (virtual) activation switch
        }
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'check States',
    },

    execute = function(dz)
        local offDevicesToCheck = { 151, 66,127,} -- name or id of devices to check for Off state
        local onDevicesToCheck = {40 } -- name of blind check if close
        local requiredStates = { 'Off', 'Closed' }
        
        --- No changes required below this line
        local alexaString = {}
        local devicesToCheck = { offDevicesToCheck, onDevicesToCheck}
        local allDeviceStatesOK = true
        
        for checkTableID, stateTable in ipairs(devicesToCheck) do
            requiredState = requiredStates[checkTableID]
                
            for _, deviceIdentifier in ipairs(stateTable) do
        
                local device = dz.devices(deviceIdentifier)
                local state = device.state
                local name = device.name
                if state ~= requiredState then 
                    allDeviceStatesOK = false 
                    dz.log('Device ' .. name .. ' has wrong state (' .. state .. ')', dz.LOG_DEBUG)        
                    dz.notify('StateChecker', 'Device ' .. name .. ' has wrong state (' .. state .. ')', dz.PRIORITY_NORMAL)
                    table.insert(alexaString, name)
                end
        
            end
        end
    
        if allDeviceStatesOK then
            dz.log('Devices are all in the required state', dz.LOG_DEBUG)        
            dz.notify('StateChecker', 'Devices are all in the required state' , dz.PRIORITY_NORMAL)
        else
            -- Speak Alexa code (& is to process command in background)
            os.execute('/home/pi/domoticz/alexa_remote_control.sh -d "Alexa Studio" -e speak:"Attenzione  '  .. table.concat(alexaString, ' and ') .. ' è acceso"  &') 
        end
    end
}
[/quote]
User avatar
waltervl
Posts: 5724
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Check all device is Off

Post by waltervl »

Blind percentage position can be get with .level (or .lastlevel)
See for more info the dzVents wiki.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests