Domoticz command: get uservariable?

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Domoticz command: get uservariable?

Post by manjh »

I am trying to set up a Node-Red dashboard to read and modify a number of usesr variables.
Writing to Domoticz is not a problem, I found this in the Wiki:

Code: Select all

{"command": "setuservariable", "idx": 1, "value": "12.3" }
But how do I read a uservariable from Domoticz? I was hoping for something like "getuservaruable", but cannot find anything about that command.
Any ideas? Suggestions for a way to do this?
Hans
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Domoticz command: get uservariable?

Post by FireWizard »

Hello Hans,

@manjh

I think this topic is a follow up of https://www.domoticz.com/forum/viewtopi ... =4&t=30330, although it is not exactly the same.

You found already howto write a value into a uservariable.
You can do that either with MQTT or HTTP in a JSON call.
But how do I read a uservariable from Domoticz? I was hoping for something like "getuservaruable", but cannot find anything about that command.
Any ideas? Suggestions for a way to do this?
Yes, "getuservariable" exist. See: https://www.domoticz.com/wiki/Domoticz_ ... _variables

You can add, update, list all, list one and delete a variable with an API call.
I do it with HTTP, as I don't think MQTT can be used.

Example below:

Screenshot_user_variable1.png
Screenshot_user_variable1.png (240.59 KiB) Viewed 2154 times

I created just a User Variable, with the name "Getal", and the type "Integer" and a value of "12345" as an example.

The following flow will get the User variable to Node Red.

Screenshot_user_variable2.png
Screenshot_user_variable2.png (9.85 KiB) Viewed 2154 times

And it will give the following output:

Screenshot_user_variable3.png
Screenshot_user_variable3.png (16.69 KiB) Viewed 2154 times

As you can see an array of objects has been returned, each containing info about the user variable.

You can also ask for a specific IDX by changing the call and add the IDX, but in this case the returned msg is identical.

Please find the flow below (for a single IDX).

Code: Select all

[{"id":"c194714d.dd08b","type":"inject","z":"4268f77.7847488","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":220,"wires":[["a77e308e.7145b8"]]},{"id":"a77e308e.7145b8","type":"function","z":"4268f77.7847488","name":"Prepare URL RPi1","func":"var url = \"http://127.0.0.1:8080\";\nmsg.url = url + \"/json.htm?type=command&param=getuservariable&idx=1\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":220,"wires":[["4f6ff499.397524"]]},{"id":"4f6ff499.397524","type":"http request","z":"4268f77.7847488","name":"HTTP","method":"PUT","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":220,"wires":[["4bae7778.ac76c"]]},{"id":"4bae7778.ac76c","type":"debug","z":"4268f77.7847488","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":220,"wires":[]}]
I think, this is where you are looking for.

Regards
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

looks like I'm getting there. Thanks...
Hans
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Domoticz command: get uservariable?

Post by FireWizard »

Hello Hans,
@manjh

You have seen the alternative at https://www.domoticz.com/forum/viewtopi ... =4&t=30330

Regards
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

OK, I finally got around to playing with this some more.
My objective is to pull the value from a user variable from Domoticz, offer that in a Node Red input field, and store the changed value back.
With a bit of debugging I found out that the output of the http node is: msg.payload.result.0.Value.
When I dump the content using a debug node I can see the correct value as string.

My next step was to feed that into an input node, but for some reason this value never shows up! Any hints?
Hans
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Domoticz command: get uservariable?

Post by FireWizard »

Hello Hans,

You wrote:
With a bit of debugging I found out that the output of the http node is: msg.payload.result.0.Value.
This sentence indicates that it was not simple to find the output of the HTTP node.
As you look to the picture of August 20 you see that the output is an array of objects.
In this example the array contains only 1 object, as I have only 1 User Variable.

In the picture you see, that the data, you are interested in, is indicated by Value and the value is "12345", which is an integer value, presented as a string (because it is between " ").

Now, if you hoover with your mouse over the line Value: "12345", you will see that a couple of extra buttons are shown. The most left of these will copy the complete path to the clipboard. In this case payload.result.0.Value. Now you can easily paste the contents of the clipboard to another node, such as a Function node.
When I dump the content using a debug node I can see the correct value as string.
If you push this to a debug node as msg.payload.result.0.Value you will only see "12345" or something.

To understand, I recommend to read: https://nodered.org/docs/user-guide/messages and then specific: Understanding the structure of a message.

My next step was to feed that into an input node, but for some reason this value never shows up! Any hints?
As you have seen, msg.payload.result.0.Value represent a specific value.
This can be:
0 = Integer, e.g. -1, 1, 0, 2, 10
1 = Float, e.g. -1.1, 1.2, 3.1
2 = String e.g. On, Off, Hello
3 = Date in format DD/MM/YYYY
4 = Time in 24 hr format HH:MM
Let us assume it is an integer, as in the example. If you want to do mathematical calculations with this value, you have to convert this String value to a numerical value. Next do the calculation and convert it back to a String.

Assume that you want to use a User Variable with IDX 1`and you want to divide that value by 10 and return to Domoticz User Variable IDX 2

Code: Select all

msg.payload = {"command": "setuservariable", "idx": 2, "value": ((parseInt (msg.payload.result.0.Value))/10).toString()};
return msg;
I have used some extra brackets in the formula to make it clear.
You can put this in a "Function" node. The input of this node, you connect to the output of the HTTP node (See the example of August 20).
I suggest that you connect a Debug node to the output of the Function node, before you connect a MQTT output node to Domoticz.

Regards
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

Thank you for your patience. Although I have quite a bit of experience in programming, JavaScript is very new to me and I am right in the learning curve..

Perhaps I should explain what I want to do.
In Domoticz I have a uservariable that is used for a sprinkler system, it indicates the number of days the systemm should be active.
This number can be updated in Domoticz, but I would like to be able to do this from the Node Red dashboard.
Actually, I have six or seven similar uservariables in the same system.
So my thinking was to :
1. define a "refresh" button that will pull all uservariables from Domoticz
2. have one inputfield for each uservariable, It should be pre-filled with the current value in Domoticz, and allow the user to enter a new value
3. store all changed values back into Domoticz

Item 3 is not a problem, and item 1 should be do-able as well. Where I run into problems is the input field, pre-filled with a value...
I found the output from HTTP node, and sam that it was an object within an object, and am not sure how to handle that.
Also, what is the best way to define an input node...

Again, thanks for your patience. I am learning!
Hans
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

When I use this formula:

Code: Select all

msg.payload = {"command": "setuservariable", "idx": 2, "value": ((parseInt (msg.payload.result.0.Value))/10).toString()};
return msg;
I get an error indicator: it complains about the ".0."...
Hans
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

OK, I solved one part of the puzzle.
The zero in that info is actually the index of the secondary object.
So accessing the value is:

Code: Select all

msg.payload = msg.payload.result[0].Value;
This isolates the value and puts it to the output of the node.
Now to set up the input field...
Hans
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

OK, looks good. Connecting the output of the function node to an input node does what I want. Now finish off the last loose ends...
Hans
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

Right, making progress.
I now have a flow where a single uservariable is pulled from Domoticz, displayed in an input text field, and then the changed value is stored back in Domoticz. A "refresh" button triggers this process.

Next step is to do this for nine or ten variables.
I could duplicate the code, but that seems silly. I know I can send "getuservariables" (plural) and that will get me an array of all uservariables.
In that array I could search for the idx values and keep keep them. The arr.find method seems suitable.

For the last hour or so I have been struggling with the syntax... no luck. Can someone help me get back on track with this?
Hans
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Domoticz command: get uservariable?

Post by manjh »

I got things working. Added a function node, with a forEach loop to examine each of the uservariables.
Works great!

Thanks for all the useful help! :)
Hans
User avatar
FireWizard
Posts: 1968
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Domoticz command: get uservariable?

Post by FireWizard »

@manjh
Hello Hans,

You wrote at September 16, 14:14
With a bit of debugging I found out that the output of the http node is: msg.payload.result.0.Value.
Sorry for giving you the wrong proposal for the "Function" node.

I did not build my Domoticz User Variable and Node Red flow. I just copy/paste, what you found after debugging.
Of course, as it is an array, it should be result[0] and not result.0

In the meantime I saw, you solved your problem. Great.

Perhaps, to support other users, cab you publish your flow.

Regards
User avatar
sincze
Posts: 1302
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Domoticz command: get uservariable?

Post by sincze »

I've been reading this thread and just for my sake of mind ;-) and summarize

It is possible to:
- Use MQTT to set a uservariable in Domoticz

Code: Select all

{"command": "setuservariable", "idx": 1, "value": "12.3" }
- it is however NOT possible to retrieve that specific uservariable using MQTT, no MQTT getuservariable exists in MQTT.cpp (Feature request ? :lol: )

I guess not an easy fix in the code as the underlying functions getuservariable and void SendVariableInfo also needs to be created.

Code: Select all

	void SendDeviceInfo(int HwdID, uint64_t DeviceRowIdx, const std::string &DeviceName, const unsigned char *pRXCommand);
	void SendSceneInfo(uint64_t SceneIdx, const std::string &SceneName);
To achieve my goal I've to use the JSON API.

Code: Select all

/json.htm?type=command&param=getuservariable&idx=IDX
In my case the MQTT getuservariable (feature) would prefer as I don't have direct access to the API (long story.. only access to MQTT)
Anyone for a PR :D
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz command: get uservariable?

Post by waaren »

sincze wrote: Tuesday 05 January 2021 12:12 In my case the MQTT getuservariable (feature) would prefer as I don't have direct access to the API (long story.. only access to MQTT)
Anyone for a PR :D
Not a PR :D this time but this dzVents approach seems pretty close to what you describe.

Code: Select all

--[[
        script to send some attributes of a uservariable via MQTT when requested in MQTT

        requirements
            dzVents 3.1.0 ( for executeShellCommand )
            mosquitto_pub on OS level
            mosquitto_sub on OS level -- For testing only

        triggered by:
            MQTT command to domoticz/in

            examples:
            mosquitto_pub -h <MQTT host> -m '{ "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" : \"<variable name>\"}" }' -t domoticz/in
            mosquitto_pub -h <MQTT host> -m '{ "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" :  <variable id> }" }' -t domoticz/in
            mosquitto_pub -h <MQTT host> -m '{ "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" :  <variable id> , \"host\" : \"ip of <MQTT Host>\" }" }' -t domoticz/in
            mosquitto_pub -h <MQTT host> -m '{ "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" : \"<variable name>\", \"host\" : \"ip of <other MQTT Host>\" , \"topic\" : \" <other topic/sub topic> \"}" }' -t domoticz/in

            defaults: host ==>> localhost, topic ==>> domoticz/out


        test in
        -------
        mosquitto_pub -h nuc -m '{ "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" : \"MQTTexample\", \"host\" : \"nuc\" , \"topic\" : \"otherTopic/subtopic/subsubtopic\"}" }' -t domoticz/in

        listening with
        --------------
        mosquitto_sub -F "%U - %t - %p" -v -p 1883 -t "#"

        1609856166.651730901 - domoticz/in - { "command": "customevent", "event" : "getUservariable", "data" : "{\"variable\" : \"MQTTexample\", \"host\" : \"nuc\" , \"topic\" : \"otherTopic/subtopic/subsubtopic\"}" }
        1609856166.855446136 - otherTopic/subtopic/subsubtopic - {"variable":{"id":39,"lastUpdate":"2021-01-05 14:31:54","name":"MQTTexample","type":"string","value":"Hi there"}}

]]

local scriptVar = 'getUservariable'

return
{
    on =
    {
        customEvents =
        {
           scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.ShellCommandResponse then
            if item.statusCode ~= 0 then
                dz.log('Problem with shellExecuteCommand ' .. item.trigger ,dz.LOG_ERROR)
                dz.log(item,dz.LOG_DEBUG)
            end
            return
        end

        local function getVariableData(v)
            local json = {} json.variable = {}
            json.variable.name = v.name
            json.variable.id = v.id
            json.variable.value = v.value
            json.variable.type = v.type
            json.variable.lastUpdate = v.lastUpdate.raw

            dz.log('json: ' .. dz.utils.toStr(json),dz.LOG_DEBUG)

            return dz.utils.toJSON(json)
        end
        
        local function sendMQTT(t)
            local variable = dz.variables(t.variable)
            local topic = t.topic or 'domoticz/out'
            local host = ( t.host and (' -h ' .. t.host) ) or ''

            dz.executeShellCommand(
            {
                command = 'mosquitto_pub ' ..  host .. ' -m ' .. "'" .. getVariableData(variable) .. "'" .. ' -t '  .. topic ,
                callback = scriptVar,
            })
        end

       --main
       if item.json then
           sendMQTT(item.json)
       else
           dz.log('Problem with customEvent ' .. item.trigger .. ' data', dz.LOG_ERROR)
           dz.log(item, dz.LOG_DEBUG)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
sincze
Posts: 1302
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Domoticz command: get uservariable?

Post by sincze »

indeed it does :D and the answer came from the dzVents king himself.

To be honest I'm not really a wizard on dzVents, did not use it as back then as it did not exist and my Domoticz response already became slower.
Moved to Pass2PHP ;) to solve the issue. Had to rewrite the LUA scripts.
dzEvents.JPG
dzEvents.JPG (24.51 KiB) Viewed 1725 times
It seems now possible to have Pass2PHP and dzVents working simultaneously, did not test that yet, but I've been following the work. :lol:
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest