Page 1 of 1

Check all device is Off

Posted: Tuesday 04 February 2020 11:59
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

Re: Check all device is Off

Posted: Tuesday 04 February 2020 13:20
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
}

Re: Check all device is Off

Posted: Tuesday 04 February 2020 14:04
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?

Re: Check all device is Off

Posted: Tuesday 04 February 2020 14:10
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 ?

Re: Check all device is Off

Posted: Tuesday 04 February 2020 14:19
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% 

Re: Check all device is Off

Posted: Tuesday 04 February 2020 14:34
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 %

Re: Check all device is Off

Posted: Tuesday 04 February 2020 14:53
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

Re: Check all device is Off

Posted: Tuesday 04 February 2020 15:08
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 ?

Re: Check all device is Off

Posted: Tuesday 04 February 2020 15:11
by djdevil
Ok then my mistake I understood tonight I will go home and try the script and let you know

Re: Check all device is Off

Posted: Tuesday 04 February 2020 21:40
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

Re: Check all device is Off

Posted: Wednesday 05 February 2020 1:35
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
}

Re: Check all device is Off  [Solved]

Posted: Wednesday 05 February 2020 12:46
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-)

Re: Check all device is Off

Posted: Wednesday 23 June 2021 20:13
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]

Re: Check all device is Off

Posted: Wednesday 23 June 2021 22:39
by waltervl
Blind percentage position can be get with .level (or .lastlevel)
See for more info the dzVents wiki.