Sending a code every x milliseconds -

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

Moderator: leecollings

Post Reply
fotomann100
Posts: 16
Joined: Tuesday 27 August 2019 15:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 41017
Contact:

Sending a code every x milliseconds -

Post by fotomann100 »

I try to write a script, that should send a Code via RFXcom over a specified time again and again. At the moment I solve this in dzvents with a simple for-loop (see below). It's working but it isn't very elegant. The mass of commands can not be processed at all.

It would be better to write a script that works in that way: It should send the code over 10 Seconds every 500 ms for example. I tryed to write this Code, but all previous attempts failed. Is somebody able to advice me?

Code: Select all

return {
	on = {
		devices = {
			'TestMaster'
		}
	},
	execute = function(domoticz, TestMaster)
	    
	   domoticz.log('Programmablauf gestartet')
	    
	   local TestSlave = domoticz.devices('TestSlave')
	       
	             domoticz.log('Variable gesetzt. TestMaster-Status:')
	             domoticz.log(TestMaster.state)
	             
	             
	             for loop = 1, 750 ,1 do
	             
	             
	           
	    
	   if (TestMaster.state == 'On') then
	       TestSlave.switchOn()
	        domoticz.log('Abfrage erfolgt')
	        
	   else
	        domoticz.log('else gelesen TestMaster Status:')
	        domoticz.log(TestMaster.state)
	       
        
         if (TestMaster.state == 'Off') then
	         TestSlave.switchOff()
	        domoticz.log(' 2. Abfrage erfolgt')
            
	   end

		   end
		 end
    
		        domoticz.log('Durchlauf beendet')
	end
}

Devices in use:
several RFXtrx433XL
Sonoff Basic, Sonoff POW R2 / Tasmota
D1 mini / espeasy
switches and sensors from several china suppliers
Pollin wireless sockets
Renkforce smoke detectors
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sending a code every x milliseconds -

Post by waaren »

fotomann100 wrote: Sunday 08 September 2019 15:45 I try to write a script, that should send a Code via RFXcom over a specified time again and again. At the moment I solve this in dzvents with a simple for-loop (see below). It's working but it isn't very elegant. The mass of commands can not be processed at all.
For these type of requirements the afterXXX() is a good method. I am not sure that the domoticz / RFXCom combination can deal with more then 1 command per second but the script below will send one every 500 milliseconds.

As always; please feel free to ask for clarification if something is not clear.

Code: Select all

 return 
{
    on = { devices = { 'TestMaster' }},
    
    logging = { level = domoticz.LOG_DEBUG, marker = 'many commands'},
              
    execute = function(dz, item)
        local TestSlave = dz.devices('TestSlave')

        dz.log('Variable gesetzt. TestMaster-Status: ' .. item.state ,dz.LOG_DEBUG)
        
        for loop = 0, 20 do
            if ( item.state == 'On' and TestSlave.switchOn().afterSec(0 + loop / 2 )) or TestSlave.switchOff().afterSec(0 + loop / 2 )  then 
                dz.log('Schedule switch command in ' .. ( loop * 50 ) .. ' mSec'  ,dz.LOG_DEBUG)  
            end
        end
    end
} 
Log when switching on
Spoiler: show

Code: Select all

2019-09-08 23:52:58.883  Status: dzVents: Debug: many commands: Processing device-adapter for TestSlave: Switch device adapter
2019-09-08 23:52:58.884  Status: dzVents: Debug: many commands: Variable gesetzt. TestMaster-Status: On
2019-09-08 23:52:58.884  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.884  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 0 SECONDS
2019-09-08 23:52:58.885  Status: dzVents: Debug: many commands: Schedule switch command in 0 mSec
2019-09-08 23:52:58.885  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.885  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 0.5 SECONDS
2019-09-08 23:52:58.886  Status: dzVents: Debug: many commands: Schedule switch command in 50 mSec
2019-09-08 23:52:58.886  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.886  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 1 SECONDS
2019-09-08 23:52:58.886  Status: dzVents: Debug: many commands: Schedule switch command in 100 mSec
2019-09-08 23:52:58.886  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.887  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 1.5 SECONDS
2019-09-08 23:52:58.887  Status: dzVents: Debug: many commands: Schedule switch command in 150 mSec
2019-09-08 23:52:58.887  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.888  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 2 SECONDS
2019-09-08 23:52:58.888  Status: dzVents: Debug: many commands: Schedule switch command in 200 mSec
2019-09-08 23:52:58.888  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.888  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 2.5 SECONDS
2019-09-08 23:52:58.889  Status: dzVents: Debug: many commands: Schedule switch command in 250 mSec
2019-09-08 23:52:58.889  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.889  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 3 SECONDS
2019-09-08 23:52:58.889  Status: dzVents: Debug: many commands: Schedule switch command in 300 mSec
2019-09-08 23:52:58.889  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.890  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 3.5 SECONDS
2019-09-08 23:52:58.890  Status: dzVents: Debug: many commands: Schedule switch command in 350 mSec
2019-09-08 23:52:58.890  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.891  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 4 SECONDS
2019-09-08 23:52:58.891  Status: dzVents: Debug: many commands: Schedule switch command in 400 mSec
2019-09-08 23:52:58.891  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.891  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 4.5 SECONDS
2019-09-08 23:52:58.892  Status: dzVents: Debug: many commands: Schedule switch command in 450 mSec
2019-09-08 23:52:58.892  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.892  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 5 SECONDS
2019-09-08 23:52:58.892  Status: dzVents: Debug: many commands: Schedule switch command in 500 mSec
2019-09-08 23:52:58.893  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.893  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 5.5 SECONDS
2019-09-08 23:52:58.893  Status: dzVents: Debug: many commands: Schedule switch command in 550 mSec
2019-09-08 23:52:58.893  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.894  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 6 SECONDS
2019-09-08 23:52:58.894  Status: dzVents: Debug: many commands: Schedule switch command in 600 mSec
2019-09-08 23:52:58.894  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.894  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 6.5 SECONDS
2019-09-08 23:52:58.895  Status: dzVents: Debug: many commands: Schedule switch command in 650 mSec
2019-09-08 23:52:58.895  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.895  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 7 SECONDS
2019-09-08 23:52:58.895  Status: dzVents: Debug: many commands: Schedule switch command in 700 mSec
2019-09-08 23:52:58.895  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.896  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 7.5 SECONDS
2019-09-08 23:52:58.896  Status: dzVents: Debug: many commands: Schedule switch command in 750 mSec
2019-09-08 23:52:58.896  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.897  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 8 SECONDS
2019-09-08 23:52:58.897  Status: dzVents: Debug: many commands: Schedule switch command in 800 mSec
2019-09-08 23:52:58.897  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.897  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 8.5 SECONDS
2019-09-08 23:52:58.898  Status: dzVents: Debug: many commands: Schedule switch command in 850 mSec
2019-09-08 23:52:58.898  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.898  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 9 SECONDS
2019-09-08 23:52:58.898  Status: dzVents: Debug: many commands: Schedule switch command in 900 mSec
2019-09-08 23:52:58.899  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.899  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 9.5 SECONDS
2019-09-08 23:52:58.899  Status: dzVents: Debug: many commands: Schedule switch command in 950 mSec
2019-09-08 23:52:58.899  Status: dzVents: Debug: many commands: Constructed timed-command: On
2019-09-08 23:52:58.900  Status: dzVents: Debug: many commands: Constructed timed-command: On AFTER 10 SECONDS
2019-09-08 23:52:58.900  Status: dzVents: Debug: many commands: Schedule switch command in 1000 mSec
2019-09-08 23:52:58.903  Status: dzVents: Info:  many commands: ------ Finished (dzVents) manyCommands
Log when switching off
Spoiler: show

Code: Select all

2019-09-08 23:53:29.469  Status: dzVents: Info:  many commands: ------ Start internal script: (dzVents) manyCommands: Device: "TestMaster (Virtual)", Index: 2382
2019-09-08 23:53:29.474  Status: dzVents: Debug: many commands: Processing device-adapter for TestSlave: Switch device adapter
2019-09-08 23:53:29.474  Status: dzVents: Debug: many commands: Variable gesetzt. TestMaster-Status: Off
2019-09-08 23:53:29.474  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.475  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 0 SECONDS
2019-09-08 23:53:29.475  Status: dzVents: Debug: many commands: Schedule switch command in 0 mSec
2019-09-08 23:53:29.475  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.475  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 0.5 SECONDS
2019-09-08 23:53:29.475  Status: dzVents: Debug: many commands: Schedule switch command in 50 mSec
2019-09-08 23:53:29.476  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.476  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 1 SECONDS
2019-09-08 23:53:29.476  Status: dzVents: Debug: many commands: Schedule switch command in 100 mSec
2019-09-08 23:53:29.476  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.476  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 1.5 SECONDS
2019-09-08 23:53:29.477  Status: dzVents: Debug: many commands: Schedule switch command in 150 mSec
2019-09-08 23:53:29.477  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.477  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 2 SECONDS
2019-09-08 23:53:29.477  Status: dzVents: Debug: many commands: Schedule switch command in 200 mSec
2019-09-08 23:53:29.477  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.478  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 2.5 SECONDS
2019-09-08 23:53:29.478  Status: dzVents: Debug: many commands: Schedule switch command in 250 mSec
2019-09-08 23:53:29.478  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.478  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 3 SECONDS
2019-09-08 23:53:29.478  Status: dzVents: Debug: many commands: Schedule switch command in 300 mSec
2019-09-08 23:53:29.479  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.479  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 3.5 SECONDS
2019-09-08 23:53:29.479  Status: dzVents: Debug: many commands: Schedule switch command in 350 mSec
2019-09-08 23:53:29.479  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.479  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 4 SECONDS
2019-09-08 23:53:29.480  Status: dzVents: Debug: many commands: Schedule switch command in 400 mSec
2019-09-08 23:53:29.480  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.480  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 4.5 SECONDS
2019-09-08 23:53:29.480  Status: dzVents: Debug: many commands: Schedule switch command in 450 mSec
2019-09-08 23:53:29.481  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.481  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 5 SECONDS
2019-09-08 23:53:29.481  Status: dzVents: Debug: many commands: Schedule switch command in 500 mSec
2019-09-08 23:53:29.481  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.482  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 5.5 SECONDS
2019-09-08 23:53:29.482  Status: dzVents: Debug: many commands: Schedule switch command in 550 mSec
2019-09-08 23:53:29.482  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.482  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 6 SECONDS
2019-09-08 23:53:29.482  Status: dzVents: Debug: many commands: Schedule switch command in 600 mSec
2019-09-08 23:53:29.483  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.483  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 6.5 SECONDS
2019-09-08 23:53:29.483  Status: dzVents: Debug: many commands: Schedule switch command in 650 mSec
2019-09-08 23:53:29.483  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.483  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 7 SECONDS
2019-09-08 23:53:29.484  Status: dzVents: Debug: many commands: Schedule switch command in 700 mSec
2019-09-08 23:53:29.484  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.484  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 7.5 SECONDS
2019-09-08 23:53:29.484  Status: dzVents: Debug: many commands: Schedule switch command in 750 mSec
2019-09-08 23:53:29.484  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.485  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 8 SECONDS
2019-09-08 23:53:29.485  Status: dzVents: Debug: many commands: Schedule switch command in 800 mSec
2019-09-08 23:53:29.485  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.485  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 8.5 SECONDS
2019-09-08 23:53:29.485  Status: dzVents: Debug: many commands: Schedule switch command in 850 mSec
2019-09-08 23:53:29.486  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.486  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 9 SECONDS
2019-09-08 23:53:29.486  Status: dzVents: Debug: many commands: Schedule switch command in 900 mSec
2019-09-08 23:53:29.486  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.486  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 9.5 SECONDS
2019-09-08 23:53:29.487  Status: dzVents: Debug: many commands: Schedule switch command in 950 mSec
2019-09-08 23:53:29.487  Status: dzVents: Debug: many commands: Constructed timed-command: Off
2019-09-08 23:53:29.487  Status: dzVents: Debug: many commands: Constructed timed-command: Off AFTER 10 SECONDS
2019-09-08 23:53:29.487  Status: dzVents: Debug: many commands: Schedule switch command in 1000 mSec
2019-09-08 23:53:29.490  Status: dzVents: Info:  many commands: ------ Finished (dzVents) manyCommands
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: No registered users and 1 guest