I installed the script and changed devices (only added one Tasmota device called 'Verlichting Achtertuin'
Code: Select all
--[[ dumpMQTT for all switch type devices
this dzVents script sends current status of all switch-type devices with ()optionally) the string mqttsync in the descriptionfield to
the defined mqtt broker with the topic domoticz/out in the same json format as domoticz mqtt hardware sends it on a device update
it uses the mosquitto_pub OS program for this.
configurable items:
MQTTBokerhost // hostnamae or IP where MQTT Broker is active ; defaults to localhost
mosquitto_pub // location of full /path/executable ; defaults to /usr/bin/moquitto_pub
MQTTTopic // MQTT topic to be used ; defaults to domoticz/out
MQTTSyncTrigger // string in description-field of device to enable syncTrigger ; defaults to All switch type devices
Thanks to @Gravityz for the initial idea, testing-, and feedback.
]]--
local scriptTrigger = 'MQTT-Broadcast' -- this is a virtual dummy switch in domoticz which triggers the script
return
{
on =
{
-- timer = {'every 10 minutes'}, -- when frequent broadcasts are required
devices = { 'Verlichting Achtertuin' },
},
logging = { level = domoticz.LOG_ERROR }, -- switch to LOG_DEBUG when not eoowrking as expected
execute = function(dz)
-- ************** Your settings (when not default) below this line *********************
local MQTTBrokerHost
local MQTTTopic
local mosquitto_pub = "/volume1/@appstore/mosquitto/bin/mosquitto_pub" -- set to '/volume1/@appstore/mosquitto/bin/mosquitto_pub' for Synology NAS
local MQTTSyncTrigger = "MQTTSyncTrigger" -- only sync switches with this string in description field leave as nil when all need to be syncd
-- ************** No changes required below this line ************************
-- set defaults
_G.logMarker = _G.logMarker or _G.moduleLabel -- set logmarker to scriptname
local MQTTBrokerHost = MQTTBokerHost or 'localhost'
local MQTTTopic = MQTTTopic or 'domoticz/out'
local mosquitto_pub = mosquitto_pub or '/usr/bin/mosquitto_pub'
local MQTTSyncTrigger = MQTTSyncTrigger ~= nil and MQTTSyncTrigger
local function osCommand(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local function composeMQTT(dv)
local json = {}
if dv.deviceSubType == 'Selector Switch' then
json.LevelActions = dv.levelActions
json.LevelNames = table.concat(dv.levelNames,'|')
json.LevelOffHidden = dv.levelOffHidden
json.SelectorStyle = "0"
json.svalue1 = dv.levelVal
elseif dv.switchType == 'Dimmer' then
json.Level = dv.level
end
if dv.color ~= '' then
json.Color = dz.utils.fromJSON(dv.color)
end
if not(json.svalue1) then
for key, value in ipairs(dv.rawData) do
json['svalue' .. key] = value:match('%d+%.*%d*')
end
end
json.battery = (dv.batteryLevel or 255)
json.RSSI = (dv.signalLevel or 12)
json.description = (dv.description):sub(0,99)
json.dtype = dv.deviceType
json.id = dv.deviceId
json.idx = dv.id
json.name = dv.name
json.nvalue = dv.nValue
json.stype = dv.deviceSubType
json.switchType = dv.switchType
json.unit = (dv.unit or 1)
unit = dv.unit
json = dz.utils.toJSON(json)
local jsonFormatted = json:gsub(',',',\n '):gsub('}','\n } \n'):gsub('{','\n { \n '):gsub(':',' : ') --origineel
-- replace.gsub(/#{year}","1/, %{#{year}","b})
dz.log('Composed json: ' .. jsonFormatted,dz.LOG_DEBUG)
return ( mosquitto_pub .. ' -h '.. MQTTBrokerHost .. ' -t ' .. MQTTTopic .. " -m '" .. jsonFormatted .. "'")
end
-- Main code
dz.devices().forEach(function(dv) -- loop over all devices in domoticz instance
-- All switch like devices ( with ( optionally ) MQTTSyncTrigger in description )
if (dv.deviceType or ''):find('witch') and ( not(MQTTSyncTrigger) or dv.description:find(MQTTSyncTrigger)) then
dz.log(dv.id .. ', ' .. dv.name .. ', ' .. dv.deviceSubType,dz.LOG_DEBUG)
osCommand(composeMQTT(dv))
end
end)
end
}
Unfortunately it still doesn't work when using the Home-app.
I does seem that the script gets called, but nothing happens.
Log when using Pilot-app and the light does come on.
2020-04-18 15:04:01.939 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:01.462 Status: EventSystem: Script event triggered: PowerConsumptie
2020-04-18 15:04:01.936 Status: User: Admin initiated a switch command (41/Verlichting Achtertuin/On)
2020-04-18 15:04:01.993 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "On"
2020-04-18 15:04:04.215 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:04.212 Status: User: Admin initiated a switch command (41/Verlichting Achtertuin/Off)
2020-04-18 15:04:04.270 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "Off"
Log when using Home-app and light doesn't come on.
2020-04-18 15:04:21.675 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":41,"switchcmd":"On"}
2020-04-18 15:04:21.678 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:21.731 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "On"
2020-04-18 15:04:30.423 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":41,"switchcmd":"Off"}
2020-04-18 15:04:30.426 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:30.479 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "Off"