Repeat on and off commands every 10 minutes Topic is solved

Moderator: leecollings

Post Reply
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Repeat on and off commands every 10 minutes

Post by mpx2 »

Hello

I have a simple AC whitch which uses 433,92 MHz and uses one way communication. I need a LUA script which will repeat on or off command every 10 minutes untill on or off is pressed.

- A switch in Domoticz is switched on => repeat on command every 10 minutes untill the switch in Domoticz is turned off.
- The switch in Domoticz is switched off => repeat off command every 10 minutes untill the switch in Domoticz is turned on.

This would solve problems like
- there's a transmission error and the first command doesn't come through
- power goes down and the actual AC switch will turn off and won't remember the last state.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands every 10 minutes

Post by waaren »

mpx2 wrote: Saturday 30 November 2019 14:32 I have a simple AC switch which uses 433,92 MHz and uses one way communication. I need a LUA script which will repeat on or off command every 10 minutes until on or off is pressed.
Using dzVents it would look like below.

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 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

return 
{
    on = 
    {
        timer = 
        {
            'every 10 minutes',
        },
    },

    execute = function(dz)
        weakDevices = 
        {
            'device-1', -- Change to name of your device(s) 
            'device-2',
            'device-3',
        }
        
        for _, name in ipairs(weakDevices) do
            local device = dz.devices(name)
            dz.log('Resending  ' .. device.state .. ' state to ' .. name, dz.LOG_INFO)
            if device.state == 'Off' then 
                device.switchOff()
            else
                device.switchOn()
            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
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Repeat on and off commands every 10 minutes

Post by mpx2 »

waaren wrote: Saturday 30 November 2019 17:48
mpx2 wrote: Saturday 30 November 2019 14:32 I have a simple AC switch which uses 433,92 MHz and uses one way communication. I need a LUA script which will repeat on or off command every 10 minutes until on or off is pressed.
Using dzVents it would look like below.
Thanks. I'll try that.

If I would like to repeat command eg. 2 times should I change the line device.switchOff()
to device.switchOff().repeatAfterMin(1, 2])
and move weakDevices where the timer is?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands every 10 minutes

Post by waaren »

mpx2 wrote: Sunday 01 December 2019 14:55 If I would like to repeat command eg. 2 times should I change the line device.switchOff()
and move weakDevices where the timer is?
no need to do anything different with table weakDevices
just this

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every 10 minutes',
        },
    },

    execute = function(dz)
        weakDevices = 
        {
            'device-1', -- Change to name of your device(s) 
            'device-2',
            'device-3',
        }
        
        for _, name in ipairs(weakDevices) do
            local device = dz.devices(name)
            dz.log('Resending  ' .. device.state .. ' state to ' .. name, dz.LOG_INFO)
            if device.state == 'Off' then 
                device.switchOff().repeatAfterSec(30, 2) -- again after 30 and after 60 seconds
            else
                device.switchOn().repeatAfterSec(30, 2)
            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
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Repeat on and off commands every 10 minutes

Post by mpx2 »

waaren wrote: Sunday 01 December 2019 22:16
mpx2 wrote: Sunday 01 December 2019 14:55 If I would like to repeat command eg. 2 times should I change the line device.switchOff()
and move weakDevices where the timer is?
no need to do anything different with table weakDevices
just this

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every 10 minutes',
        },
    },

    execute = function(dz)
        weakDevices = 
        {
            'device-1', -- Change to name of your device(s) 
            'device-2',
            'device-3',
        }
        
        for _, name in ipairs(weakDevices) do
            local device = dz.devices(name)
            dz.log('Resending  ' .. device.state .. ' state to ' .. name, dz.LOG_INFO)
            if device.state == 'Off' then 
                device.switchOff().repeatAfterSec(30, 2) -- again after 30 and after 60 seconds
            else
                device.switchOn().repeatAfterSec(30, 2)
            end
        end
    end
}
I mean that if I don't want to run the script every 10 minutes but just when the state changes.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands when device state is updated

Post by waaren »

mpx2 wrote: Monday 02 December 2019 15:09 I mean that if I don't want to run the script every 10 minutes but just when the state changes.
It could then look like below

Code: Select all

local weakDevices = 
      {
            'device-1', -- Change to name of your device(s) 
            'device-2',
            'device-3',
      }

return 
{
    on = 
    {
        devices = weakDevices, 
    },

    execute = function(dz, item)
        local delay = 30
        local repeats = 5
        
        for repeater = 1, repeats do
            local calculatedDelay = delay * repeater
            dz.log('Resending  ' .. item.state .. ' state to ' .. item.name .. ' after ' .. calculatedDelay .. ' seconds. ', dz.LOG_INFO)
            if item.state == 'Off' then 
                item.switchOff().silent().afterSec(calculatedDelay) 
            else
                item.switchOn().silent().afterSec(calculatedDelay)
            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
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Repeat on and off commands every 10 minutes

Post by mpx2 »

waaren wrote: Saturday 30 November 2019 17:48
mpx2 wrote: Saturday 30 November 2019 14:32 I have a simple AC switch which uses 433,92 MHz and uses one way communication. I need a LUA script which will repeat on or off command every 10 minutes until on or off is pressed.
Using dzVents it would look like below.

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 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

return 
{
    on = 
    {
        timer = 
        {
            'every 10 minutes',
        },
    },

    execute = function(dz)
        weakDevices = 
        {
            'device-1', -- Change to name of your device(s) 
            'device-2',
            'device-3',
        }
        
        for _, name in ipairs(weakDevices) do
            local device = dz.devices(name)
            dz.log('Resending  ' .. device.state .. ' state to ' .. name, dz.LOG_INFO)
            if device.state == 'Off' then 
                device.switchOff()
            else
                device.switchOn()
            end
        end
    end
}
Helle

Tanks you for your script. The script has been mostly working ok but some times it fails when repeat and switch on happens at the same time.

Code: Select all

2020-01-01 16:00:02	On
2020-01-01 16:00:02	Off
2020-01-01 15:48:01	Off
2020-01-01 15:36:01	Off
At 2020-01-01 16:00:02 my switch turned on and immediately was turned off. The switch state is off so the next repeat is off command.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands every 10 minutes

Post by waaren »

mpx2 wrote: Wednesday 01 January 2020 15:12 The script has been mostly working ok but some times it fails when repeat and switch on happens at the same time.
At 2020-01-01 16:00:02 my switch turned on and immediately was turned off. The switch state is off so the next repeat is off command.
Unfortunately there is nothing dzVents can do to prevent this from happening.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Repeat on and off commands every 10 minutes

Post by mpx2 »

waaren wrote: Wednesday 01 January 2020 16:47
mpx2 wrote: Wednesday 01 January 2020 15:12 The script has been mostly working ok but some times it fails when repeat and switch on happens at the same time.
At 2020-01-01 16:00:02 my switch turned on and immediately was turned off. The switch state is off so the next repeat is off command.
Unfortunately there is nothing dzVents can do to prevent this from happening.
Hi

I added a delay just before the for loop.

Code: Select all

        os.execute("sleep " .. math.random(5,15))
This will cause a random delay between 5 and 15 seconds. dzVentz script is not triggered at 00.

Code: Select all

2020-01-02 10:48:06	Off
2020-01-02 10:36:10	Off
2020-01-02 10:24:12	Off
2020-01-02 10:12:11	Off
2020-01-02 10:00:13	Off
I guess there's no way to use atomic scripts and timers.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands every 10 minutes

Post by waaren »

mpx2 wrote: Thursday 02 January 2020 12:53 I added a delay just before the for loop.

Code: Select all

        os.execute("sleep " .. math.random(5,15))
This will cause a random delay between 5 and 15 seconds. dzVentz script is not triggered at 00.
This will work for switches triggered by other time based scripts but ( although unlikely ) it might fail for manual switched switches.
What is worse, is that the sleep command will block the entire event system for 5-15 seconds as the event system is single threaded and therefore will not process other events until this script has finished. You will see some complaining log lines like
Warning!, lua script /home/pi/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
If that does not bather you it's OK but a more elegant approach would be to use an async approach with a openURL call or by using a dummy variable or device to re-trigger this script after some seconds.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mpx2
Posts: 70
Joined: Friday 21 September 2018 18:28
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Repeat on and off commands every 10 minutes

Post by mpx2 »

waaren wrote: Thursday 02 January 2020 13:15
mpx2 wrote: Thursday 02 January 2020 12:53 I added a delay just before the for loop.

Code: Select all

        os.execute("sleep " .. math.random(5,15))
This will cause a random delay between 5 and 15 seconds. dzVentz script is not triggered at 00.
This will work for switches triggered by other time based scripts but ( although unlikely ) it might fail for manual switched switches.
What is worse, is that the sleep command will block the entire event system for 5-15 seconds as the event system is single threaded and therefore will not process other events until this script has finished. You will see some complaining log lines like
Warning!, lua script /home/pi/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
If that does not bather you it's OK but a more elegant approach would be to use an async approach with a openURL call or by using a dummy variable or device to re-trigger this script after some seconds.
I'll try this

Code: Select all

os.execute("ping -c " .. math.random(10,25)) .. " localhost > NUL")
http://lua-users.org/wiki/SleepFunction
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Repeat on and off commands every 10 minutes

Post by waaren »

mpx2 wrote: Sunday 05 January 2020 23:40 I'll try this

Code: Select all

os.execute("ping -c " .. math.random(10,25)) .. " localhost > NUL")
That will not make it better. It will stil pause the event System but now for 10-25 seconds (the default interval is 1 second between pings)
It will also create a file called NUL on your system.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest