Get idx from switch
Moderator: leecollings
Get idx from switch
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.
- habahabahaba
- Posts: 190
- Joined: Saturday 18 March 2023 14:44
- Target OS: Windows
- Domoticz version: 2024.4
- Contact:
Re: Get idx from switch
You can store some data in User Variables in settings menu and call them from script.
- jvdz
- Posts: 2189
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: Get idx from switch
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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 616
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Get idx from switch
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).
-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).
- waltervl
- Posts: 5148
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: Get idx from switch
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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Re: Get idx from switch
I don’t understand how this can be the idx or name of the device on triggering the on/off action. Can you eleborate?habahabahaba wrote: ↑Thursday 12 December 2024 8:15 You can store some data in User Variables in settings menu and call them from script.
Re: Get idx from switch
Think I’ll look into dzvents for what I’m looking for. Found the following example:
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?
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
}
- 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?
-
- Posts: 504
- Joined: Sunday 01 November 2015 22:45
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2023.2
- Location: Twente
- Contact:
Re: Get idx from switch
This is easy, have a look here first.
So using your code from the dzVents wiki:
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:
I adapted the code to what you see above.
My conversation with CoPilot is in het Nederlands.
Enjoy!
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
}
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
}
My conversation with CoPilot is in het Nederlands.
Enjoy!
Bugs bug me.
Re: Get idx from switch
Cheers, I’ll give this a try.
Re: Get idx from switch
The following works for me:
I could do without this elaborate log:
What would be nice was if the result from the url was logged.. think I'll have to dive into some documentation later.
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
}
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
Re: Get idx from switch
Changing this part:
...to this:
...allows me to get the result from the URL using the following code and show it in the Domoticz log:
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
domoticz.openURL('https://domain.tld/script/?idx=' .. switch.idx .. '&level=' .. switch.level .. '&status=' .. switch.state)
Code: Select all
domoticz.openURL({
url = 'https://domain.tld/script/?idx=' .. switch.idx .. '&level=' .. switch.level .. '&status=' .. switch.state,
callback = 'Get_Tuya'
})
Code: Select all
return {
on = {
httpResponses = { 'Get_Tuya' }
},
execute = function(domoticz, response)
domoticz.log(response.data)
end
}
Code: Select all
url = 'https://domain.tld/script',
method = 'POST',
callback = 'Get_Tuya',
postData = {
idx = switch.idx,
status = switch.state,
level = switch.level
}
Who is online
Users browsing this forum: Bing [Bot] and 1 guest