dzvents mqtt  [Solved]

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

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

dzvents mqtt

Post by pvklink »

Hi,

@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
}

Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents mqtt

Post by waaren »

pvklink wrote: Friday 11 June 2021 20:22 When i add nvalue and svalue values for transmitting values for a switch it wont, what am i doing wrong ?
"It won't work" might be an accurate description of your issue but please try to give some more details, like what you expect and what do you actually see in the log,
If you don't see much, a next step could be to add some log statements.

nvalue is an integer and do not need to be surrounded by quotes.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: dzvents mqtt

Post by pvklink »

:-) , i know, it does not work gives not much info, little bit more specific:

My current script works ok for sending a message to MQTT and receive it in NODERED when changing a variable
Adding a specific device like lampx also works when using below messagestructure and content

local test= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '" }'

When extending the message for example with nvalue i dont get a message in NODERED, so it must be the nvalue part..
also removed the quotes as nvalue is a number
I will look around if a can get moren specific errors and report back...

-- local test= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '","nvalue" : ' .. item.nvalue .. " }'
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: dzvents mqtt

Post by pvklink »

Okay, i solved the main part of the problem!
I do have a working template now for an on/off switch that is received by nodered via mqtt.
This is the main part:
local payload= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '", "nvalue" : ' .. item.nValue .. ', "svalue1": "' .. item.sValue .. '", "stype" : "Switch" }'

Only part of this script i cant get to work, is to check if the item is an on/off switch to make this script suitable for more then one device type.
I tried several options, but none of them work

if (item.deviceSubType == 'On/Off') then ...
if (item.deviceSubType == 'Switch') then ...
if (item.deviceType == 'Light/Switch' and item.switchType == 'On/Off') then ....

Code: Select all

-- http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/ 
-- http://192.168.20.38:82/json.htm?type=devices&rid=1483
local scriptVar = 'sendMQTT'

return
{
    on =
        {
        devices = {'lampx'}, 
        shellCommandResponses = {scriptVar,}
        },
    logging = 
    {
        level = domoticz.LOG_FORCE,
        marker = xxx,
    },

    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 messageTable = {}

    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 '')
    local MQTTTopic    = "domoticz/out"

    local function call_exec(mycommand)
        dz.executeShellCommand(
        {
        command = mycommand,
        callback = scriptVar,
        timeout = 120           -- in seconds
        })
    end

    --dz.log(item, dz.LOG_FORCE)

    if item.isDevice then

        --if (item.deviceSubType == 'On/Off') then
        --if (item.deviceSubType == 'Switch') then
        --if (item.deviceType == 'Light/Switch' and item.switchType == 'On/Off')
            local payload= '{"idx" : ' .. item.idx .. ', "name" : "' .. item.name .. '", "state" : "'.. item.state .. '", "nvalue" : ' .. item.nValue .. ', "svalue1": "' .. item.sValue .. '", "stype" : "Switch" }'
        --end

        local mqttCommand  = "mosquitto_pub -h " .. ipthisrpi .. " -u pvkmqtt -P pvkmqtt -t " .. MQTTTopic .. " -m '" .. payload .. "'"     -- bij docker geen sudo en ipadress toevoegen
        call_exec(mqttCommand)
        dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump

    elseif item.isShellCommandResponse and item.ok then
        dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' SERVICE_MQTT shell responce ok...',30) 
        dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump
        
    elseif (item.isHTTPResponse) then
        if (item.ok) then
            dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' SERVICE_MQTT http responce ok...',30) 
        else
            dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' SERVICE_MQTT http responce NOK: ' .. item.statusCode,30) 
        end
        dz.helpers.globalMessage2(dz,item,info,messageTable,'add', ' SERVICE_MQTT http responce NOK: ' .. item,30) 
        dz.helpers.globalMessage2(dz,item,info,messageTable,'chg') -- dump

    end
end
}

Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: dzvents mqtt  [Solved]

Post by pvklink »

Solved. Problem was not correct usage of "local". In a if then end local only is persistent in this context. I needed the varable outside the if then...
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest