Use user name in script to modify specific user variables
Moderator: leecollings
-
- Posts: 3
- Joined: Saturday 03 October 2020 15:36
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Use user name in script to modify specific user variables
I would like to implement an accounting system tracking the usage of a switch.
For this I created Users on Domoticz and assigned the Switch to the users.
Then also created specific User Variales (UserName_Credits).
Also created a simple script that should read out and manipulate the UserName_Credits variable based on the actual logged in user that triggered the switch.
If switch switched on
UserName_Credits = UserName_Credits - Switch_Cost.
ISSUE:
The issue I have is accessing the actual logged in user name in the dzVents event script. I could not find this info anywhere.
Basically a working dzVents expression for this:
domotics.variables(LOGGEDINUSERNAME..'_Credits').value
Thanks!
For this I created Users on Domoticz and assigned the Switch to the users.
Then also created specific User Variales (UserName_Credits).
Also created a simple script that should read out and manipulate the UserName_Credits variable based on the actual logged in user that triggered the switch.
If switch switched on
UserName_Credits = UserName_Credits - Switch_Cost.
ISSUE:
The issue I have is accessing the actual logged in user name in the dzVents event script. I could not find this info anywhere.
Basically a working dzVents expression for this:
domotics.variables(LOGGEDINUSERNAME..'_Credits').value
Thanks!
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Use user name in script to modify specific user variables
This information is not natively available in dzVents but can be retrieved from domoticz using openURL.
Can you try below script?
Code: Select all
local scriptVar = 'Creditable'
return
{
on =
{
devices =
{
'Creditable*', -- All devices with name that starts with Creditable (change to your list of devices)
'switch3',
643,
},
httpResponses =
{
scriptVar .. '*'
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
execute = function(dz, item)
local function switchCost(switch)
local costs =
{-- switch costs
['CreditableTest'] = 4, -- change to reflect your device IDX and cost
['switch1'] = 2,
['switch2'] = 4,
['switch3'] = 7,
}
return costs[switch] or 0 -- when no entry in costs table the charge will be 0
end
local function calculateCredits(user, switch)
local var = user .. '_Credits'
if dz.utils.variableExists(var) then
local userCredit = dz.variables(var)
local oldCredit = userCredit.value
local cost = switchCost(switch)
local newCredit = oldCredit - cost
dz.log('user ' .. user .. ' will be charged ' .. cost .. ' credits for activating device ' .. switch .. '. Remaining credits: ' .. newCredit , dz.LOG_DEBUG)
userCredit.set(newCredit)
else
dz.log('Variable ' .. user .. '_Credits does not exist. No credits subtracted. ', dz.LOG_DEBUG)
end
end
if item.isDevice and item.active then
dz.openURL(
{
url = dz.settings['Domoticz url'] .. '/json.htm?type=lightlog&idx=' .. item.idx,
callback = scriptVar .. item.idx,
})
elseif item.isHTTPResponse then
if item.ok and item.isJSON then
local idxString = item.trigger:gsub(scriptVar, '') -- extract the idx from the callback
local idx = tonumber(idxString)
local deviceName = dz.devices(idx).name
local userName = item.json.result[1].User
dz.log('User ' .. userName .. ' switched device ' .. deviceName, dz.LOG_DEBUG)
calculateCredits(userName, deviceName)
else
dz.log('There was a problem handling the request', dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 347
- Joined: Friday 03 April 2015 17:09
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
Yes, that helps, this points me into the good direction.
Conclusion is that the info is not available in the script itself as a variable. You need to get the log from the device from Domoticz and determine the user name from the logged info. That is clear now. Thanks.
Conclusion is that the info is not available in the script itself as a variable. You need to get the log from the device from Domoticz and determine the user name from the logged info. That is clear now. Thanks.
-
- Posts: 3
- Joined: Saturday 03 October 2020 15:36
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
Thanks!
I will give it a try this week. To access the URL is a cool workaround. However it would be nice to have the logged-in user name as global system variable available.
It would simplifiy all kind of user / role based scripting.
Could that be put on the "Feature Requirement List" for the future?
I will give it a try this week. To access the URL is a cool workaround. However it would be nice to have the logged-in user name as global system variable available.

Could that be put on the "Feature Requirement List" for the future?
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Use user name in script to modify specific user variables
Not sure that I understand what you want. What would be the value of the logged-in user name given the fact that there can be many logged in users at the same time and that devices can be switched using an API call by users that are not logged in as a session.
I am working on a PR that will reveal the last user that switched a device to dzVents. Is that what you are looking for?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 3
- Joined: Saturday 03 October 2020 15:36
- Target OS: NAS (Synology & others)
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
Yes exactly. In the event of switching "On Device..." call. I would like to access the user name that triggered the script by activating the switch.
Use case:
-> PrePay Systems
Similar to SmartCard, Coins, Tokens or RFID based PrePayed Systems: Accounting - use domoticz to identify user and track consumption.
Book Keeping by using User Variables.
So you are right - not the currently logged in user is required but the one invoking the script.
Use case:
-> PrePay Systems
Similar to SmartCard, Coins, Tokens or RFID based PrePayed Systems: Accounting - use domoticz to identify user and track consumption.
Book Keeping by using User Variables.
So you are right - not the currently logged in user is required but the one invoking the script.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: Use user name in script to modify specific user variables
Too give an update on the status for this request:
The PR i prepared to include an updatedBy was merged into domoticz Beta but had to be reverted because it consumed too much CPU / memory. Internally it needed to extract information from a couple of potentially large database tables. So for now scripts that need the updatedBy information will have to do this via the API I posted earlier in this topic or access information in the database using sqlite via an os.command.
The PR i prepared to include an updatedBy was merged into domoticz Beta but had to be reverted because it consumed too much CPU / memory. Internally it needed to extract information from a couple of potentially large database tables. So for now scripts that need the updatedBy information will have to do this via the API I posted earlier in this topic or access information in the database using sqlite via an os.command.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- Posts: 5
- Joined: Saturday 27 August 2016 18:12
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
Any news on this waaren?
What Im looking for is when someone in my family arms/disarms our alarm system. It would be Nice to get a notification that: <user> armed the alarm!

What Im looking for is when someone in my family arms/disarms our alarm system. It would be Nice to get a notification that: <user> armed the alarm!

-
- Posts: 664
- Joined: Thursday 10 November 2016 9:30
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
-
- Posts: 5
- Joined: Saturday 27 August 2016 18:12
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Use user name in script to modify specific user variables
Thanks. Incredibly sad news.lost wrote: ↑Tuesday 09 November 2021 11:04Hello, as many of us, looks you missed the sad news this summer:
viewtopic.php?f=4&t=36810
Rest in Peace.
Who is online
Users browsing this forum: No registered users and 1 guest