Page 1 of 1
Get idx from switch
Posted: Wednesday 11 December 2024 20:27
by vespino
I’m building a script that controls devices using the manufacturer’s API. First I’m adding virtual devices, then I’m adding an on and off script to each device. I was wondering if it’s possible to dynamically add device data to the script that is run, so I don’t have to add variables to every single script. When running a script, does Domoticz have a number of globals for example that I can use? If not, I can of course check the SQLite database for the latest trigger.
Re: Get idx from switch
Posted: Thursday 12 December 2024 8:15
by habahabahaba
You can store some data in User Variables in settings menu and call them from script.
Re: Get idx from switch
Posted: Thursday 12 December 2024 9:50
by jvdz
vespino wrote: ↑Wednesday 11 December 2024 20:27
I was wondering if it’s possible to dynamically add device data to the script that is run, so I don’t have to add variables to every single script.
I am using some code in lua that simply tests for the changed devicename whether that starts or end with a fixed sting, and then do what is needed.
EG: I have 4 smoke alarms named
xxxx_RookMelder so this code that would handle them all 4:
Code: Select all
-- This is just for demo purpose, normally comes from domoticz
devicechanged= {}
devicechanged["n1_Rookmelder"]= "On"
devicechanged["n2_Rookmelder"]= "Off"
-- ----------------------------------------------------------
for i, v in pairs(devicechanged) do
if i:find(".*_Rookmelder") then
print("Rookmelder" .. i .. " status:" .. v)
end
end
Re: Get idx from switch
Posted: Thursday 12 December 2024 10:51
by lost
There may be 2 subjects in your question:
-With a naming convention (see also wiki for alarm setup, on top of jvdz anwser), you can manage several devices the same way in a single script.
-If you need to store data between scripts execution/triggers, you already know user variables in pure Lua... if you need more you have dzVent that extends this using files to store script data but that's no more pure Lua.
Domoticz does not implement a way to store persistent structured data from scripts for their own use, I also sometimes missed this feature: Nothing between user variables that can be quickly limiting/cumbersome and the dzVent (file) hack to cope with this.
A usertable could be made available/returned to store key-value pairs solely managed by users, with a user defined max size from the webUI for instance to manage it's allocation. This would also allow sharing data between different scripts (as user variables, but not dzVent file storage IMO as this use script name).
Re: Get idx from switch
Posted: Thursday 12 December 2024 16:26
by waltervl
You also can create a Python plugin as the python plugin framework has functionality to maintain devices connected to the plugin:
https://wiki.domoticz.com/Developing_a_Python_plugin
Re: Get idx from switch
Posted: Friday 13 December 2024 20:29
by vespino
habahabahaba wrote: ↑Thursday 12 December 2024 8:15
You can store some data in User Variables in settings menu and call them from script.
I don’t understand how this can be the idx or name of the device on triggering the on/off action. Can you eleborate?
Re: Get idx from switch
Posted: Friday 13 December 2024 21:24
by vespino
Think I’ll look into dzvents for what I’m looking for. Found the following example:
Code: Select all
return {
on = {
devices = {
'<exact name of the switch>'
}
},
execute = function(domoticz, switch)
if (switch.state == 'On') then
domoticz.log('Hey! I am on!')
else
domoticz.log('Hey! I am off!')
end
end
}
Questions:
- I can add multiple devices?
- Inside execute I can get idx, status and level?
- No need to add it to every individual device but simply use a device array?
Re: Get idx from switch
Posted: Friday 13 December 2024 22:29
by HvdW
This is easy,
have a look here first.
So using your code from the dzVents wiki:
Code: Select all
return {
on = {
devices = {
'switch1', 'switch2', 'switch3', 'YourSwitch' -- In my domoticz there is a switch which I always use for testing things
}
},
execute = function(domoticz, switch)
domoticz.log('Triggered item is: ' .. switch.name, domoticz.LOG_INFO)
local yourSwitchName = switch.name
if (switch.state == 'On') then
domoticz.log('Hey! '.. switch.name ..' is on !', domoticz.LOG_INFO)
else
domoticz.log('Hey! '.. yourSwitchName ..' is off !', domoticz.LOG_INFO)
end
end
}
For level you could create a dummysensor 'YourSwitchSelector' which you can create and use as a test sensor.
And even better; I am doing this typing on Microsoft Edge browser.
The bad thing is that CoPilot - the Microsoft ChatGPT - can read my open tab in the browser.
The good thing is that it helps me to write you this answer.
I started my answer using the link and copied the small script.(more or less)
In that script I added 'switch1', 'switch2', 'switch3'
Then I activated CoPilot and typed 'triggerinfo'
The answer I got was this:
Code: Select all
return {
on = {
devices = {
'switch1', 'switch2', 'switch3'
}
},
execute = function(domoticz, switch)
domoticz.log('Triggered item is: ' .. switch.name, domoticz.LOG_INFO)
if (switch.state == 'On') then
domoticz.log('Hey! I am on!', domoticz.LOG_INFO)
else
domoticz.log('Hey! I am off!', domoticz.LOG_INFO)
end
end
}
I adapted the code to what you see above.
My conversation with CoPilot is in het Nederlands.
Enjoy!
Re: Get idx from switch
Posted: Friday 13 December 2024 22:41
by vespino
Cheers, I’ll give this a try.
Re: Get idx from switch
Posted: Saturday 14 December 2024 0:16
by HvdW
PS
If you want to use 'ethic' AI then use
claude.ai
Re: Get idx from switch
Posted: Saturday 14 December 2024 9:54
by vespino
The following works for me:
Code: Select all
return {
on = {
devices = {
'Voordeur',
'Spotjes werkblad',
'Plantenlamp',
'Staande lamp',
'Kerstboom',
'Spotjes eetkamer',
'Hanglamp',
'Spotjes kast',
'Spotjes woonkamer',
'Vloerlamp',
'TV',
'Slaapkamer 2',
'Kantoor',
'Achterdeur',
'Gangpad',
'Carpot',
}
},
execute = function(domoticz, switch)
domoticz.openURL('https://domain.tld/script/?idx=' .. switch.idx .. '&level=' .. switch.level .. '&status=' .. switch.state)
end
}
I could do without this elaborate log:
Code: Select all
2024-12-14 10:27:51.027 dzVents: Handling events for: "Kantoor", value: "Off"
2024-12-14 10:27:51.027 dzVents: ------ Start internal script: Do_Tuya: Device: "Kantoor (Dummy schakelaars)", Index: 424
2024-12-14 10:27:51.027 dzVents: ------ Finished Do_Tuya
What would be nice was if the result from the url was logged.. think I'll have to dive into some documentation later.
Re: Get idx from switch
Posted: Saturday 14 December 2024 15:59
by vespino
Changing this part:
Code: Select all
domoticz.openURL('https://domain.tld/script/?idx=' .. switch.idx .. '&level=' .. switch.level .. '&status=' .. switch.state)
...to this:
Code: Select all
domoticz.openURL({
url = 'https://domain.tld/script/?idx=' .. switch.idx .. '&level=' .. switch.level .. '&status=' .. switch.state,
callback = 'Get_Tuya'
})
...allows me to get the result from the URL using the following code and show it in the Domoticz log:
Code: Select all
return {
on = {
httpResponses = { 'Get_Tuya' }
},
execute = function(domoticz, response)
domoticz.log(response.data)
end
}
I was hoping to use POST instead of GET, just to make the code better readable, unfortunately the following doesn't seem to work (read: the data is not received on the remote server):
Code: Select all
url = 'https://domain.tld/script',
method = 'POST',
callback = 'Get_Tuya',
postData = {
idx = switch.idx,
status = switch.state,
level = switch.level
}