Use user name in script to modify specific user variables

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

Moderator: leecollings

Post Reply
georg2020
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

Post by georg2020 »

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!
User avatar
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

Post by waaren »

georg2020 wrote: Saturday 03 October 2020 16:02 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.
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
rgroothuis
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

Post by rgroothuis »

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

Post by georg2020 »

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?
User avatar
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

Post by waaren »

georg2020 wrote: Monday 12 October 2020 9:39 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.
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
georg2020
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

Post by georg2020 »

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.
User avatar
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

Post by waaren »

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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Andee78
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

Post by Andee78 »

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!

:roll:
lost
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

Post by lost »

Andee78 wrote: Wednesday 03 November 2021 20:46 Any news on this waaren?
Hello, as many of us, looks you missed the sad news this summer:
viewtopic.php?f=4&t=36810
Andee78
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

Post by Andee78 »

lost wrote: Tuesday 09 November 2021 11:04
Andee78 wrote: Wednesday 03 November 2021 20:46 Any news on this waaren?
Hello, as many of us, looks you missed the sad news this summer:
viewtopic.php?f=4&t=36810
Thanks. Incredibly sad news.
Rest in Peace.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest