Writing to Domoticz is not a problem, I found this in the Wiki:
Code: Select all
{"command": "setuservariable", "idx": 1, "value": "12.3" }Any ideas? Suggestions for a way to do this?
Moderators: leecollings, remb0
Code: Select all
{"command": "setuservariable", "idx": 1, "value": "12.3" }Yes, "getuservariable" exist. See: https://www.domoticz.com/wiki/Domoticz_ ... _variablesBut 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?
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¶m=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":[]}]This sentence indicates that it was not simple to find the output of the HTTP node.With a bit of debugging I found out that the output of the http node is: msg.payload.result.0.Value.
If you push this to a debug node as msg.payload.result.0.Value you will only see "12345" or something.When I dump the content using a debug node I can see the correct value as string.
As you have seen, msg.payload.result.0.Value represent a specific value.My next step was to feed that into an input node, but for some reason this value never shows up! Any hints?
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.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
Code: Select all
msg.payload = {"command": "setuservariable", "idx": 2, "value": ((parseInt (msg.payload.result.0.Value))/10).toString()};
return msg;
Code: Select all
msg.payload = {"command": "setuservariable", "idx": 2, "value": ((parseInt (msg.payload.result.0.Value))/10).toString()};
return msg;Code: Select all
msg.payload = msg.payload.result[0].Value;Sorry for giving you the wrong proposal for the "Function" node.With a bit of debugging I found out that the output of the http node is: msg.payload.result.0.Value.
Code: Select all
{"command": "setuservariable", "idx": 1, "value": "12.3" }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);Code: Select all
/json.htm?type=command¶m=getuservariable&idx=IDXNot a PRsincze 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![]()
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
}
Users browsing this forum: No registered users and 1 guest