@waaren, you made below script for me to push a msg for NRvolume to mqtt which i use in nodered.
This works perfect. I like to add a device change to this script, so i add a on device and a device part.
Generallly it works when using the line specified in local test.
When i add nvalue and svalue values for transmitting values for a switch it wont, what am i doing wrong ?
Code: Select all
-- mosquitto_pub -u pvkmqtt -P pvkmqtt -t domoticz/out -m '{ "name" : "NRvolume", "xxx" : 0, "value" : 10}'
-- http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/
local scriptVar = 'sendMQTT'
return
{
on =
{
variables = {"NRvolume",},
devices = {'lampx'}, --beweginsalarm_huis niet, die lopen via pir*
shellCommandResponses = {scriptVar,}
},
execute = function(dz, item, info)
_G.logLevel = dz.helpers.get_logtype(dz) -- dz.log('debug test',dz.LOG_DEBUG) this will only make it to the log when LOG_DEBUG is set AND dz.log('blabla',dz.LOG_FORCE) -- this will allways make it tp the log
_G.logMarker = _G.moduleLabel -- marker wordt scriptnaam, _G.logMarker = info.scriptName is idem
local myiptmp = (dz.utils.stringSplit(dz.variables('IPthisrpi').value,':') or dz.variables('IPthisrpi').value)
local ipthisrpi = (myiptmp[1] or myiptmp)
--local portthisrpi = (myiptmp[2] or '')
--dz.log('XX ip: ' .. ipthisrpi .. ' port :' .. portthisrpi, dz.LOG_INFO) --(weer.verw or "Geen weerverwachting actief")
if item.isVariable then
local MQTTTopic = "domoticz/out"
local value
if not(tonumber(item.value)) then value = '"' .. item.value .. '"' end
local payload = '{ "name" : "NRvolume", "xxx" : 0, "value" : ' .. ( value or item.value ) .. ' }'
-- bij docker hier het sudo command verwijderen en ipadress toevoegen, dus mosquitto_pub ipv sudo mosquitto_pub -h <ip>
-- local mqttCommand = "mosquitto_pub -h 192.168.20.48 -u pvkmqtt -P pvkmqtt -t " .. MQTTTopic .. " -m '" .. payload .. "'"
local mqttCommand = "mosquitto_pub -h " .. ipthisrpi .. " -u pvkmqtt -P pvkmqtt -t " .. MQTTTopic .. " -m '" .. payload .. "'"
dz.executeShellCommand(
{
command = mqttCommand,
callback = scriptVar,
timeout = 120 -- in seconds
})
elseif item.isDevice then
local MQTTTopic = "domoticz/out"
local test= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '" }'
-- above works but adding nvalue and svalue not...
-- local test= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '","nvalue" : "'.. item.nvalue .. '" }'
-- to do svalue1 en nvalue die werken niet
local payload = test
-- bij docker hier het sudo command verwijderen en ipadress toevoegen, dus mosquitto_pub ipv sudo mosquitto_pub -h <ip>
-- local mqttCommand = "mosquitto_pub -h 192.168.20.48 -u pvkmqtt -P pvkmqtt -t " .. MQTTTopic .. " -m '" .. payload .. "'"
local mqttCommand = "mosquitto_pub -h " .. ipthisrpi .. " -u pvkmqtt -P pvkmqtt -t " .. MQTTTopic .. " -m '" .. payload .. "'"
dz.executeShellCommand(
{
command = mqttCommand,
callback = scriptVar,
timeout = 120 -- in seconds
})
elseif item.isShellCommandResponse and item.ok then
dz.log('MQTT successfully send: ', dz.LOG_INFO)
else
dz.log('No valid response from MQTT command: ' .. item.statusCode, dz.LOG_ERROR)
dz.log(item, dz.LOG_ERROR)
end
end
}