Domoticz MQTT Helper 0 then 1

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Domoticz MQTT Helper 0 then 1

#1 Post by hestia » 12 Jul 2021, 22:33

Hi
I've a switch input connected to dz with MQTT Helper like this
Screenshot 2021-07-12 222543.png
Screenshot 2021-07-12 222543.png (148.17 KiB) Viewed 4109 times
I've found that the behavior is different when used from dz than from the hardware switch
From the switch, On then Off

Code: Select all

272215: EVENT: Button4#State=0
272232: ACT  : gpio,15,=!0
272235: GPIO : port#15: set to 1
272318: EVENT: Relay4#State=1
274514: EVENT: Button4#State=1
274532: ACT  : gpio,15,=!1
274535: GPIO : port#15: set to 0
274622: EVENT: Relay4#State=0
274647: ACT  : LogEntry,'*** Relay4=0'
274649: *** Relay4=0
From the dz, On then Off

Code: Select all

285203: inputswitchstate is deprecatedinputSwitchState,7,1.00
285207: GPIO : port#15: set to 1
285229: EVENT: Relay4#State=0
285247: ACT  : LogEntry,'*** Relay4=0'
285248: *** Relay4=0
285307: EVENT: Pump#Output=1
285409: EVENT: Relay4#State=1
288303: inputswitchstate is deprecatedinputSwitchState,7,0.00
288307: GPIO : port#15: set to 0
288326: EVENT: Relay4#State=1
288409: EVENT: Pump#Output=0
288507: EVENT: Relay4#State=0
288525: ACT  : LogEntry,'*** Relay4=0'
288526: *** Relay4=0
There is 1 more event on Relay4, with the value opposite of the real ones :-(
Which cause issue in my code because the code do the reverse to what I need!
ESP_Easy_mega_20210223_normal_ESP8285_1M
It is a "normal" behavior ?
If yes how could I do to trigger the good event?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Domoticz MQTT Helper 0 then 1

#2 Post by TD-er » 12 Jul 2021, 22:55

Just to be sure, have you followed it according to this description? https://espeasy.readthedocs.io/en/lates ... #p029-page

Especially the "Switch Button Type", which should not be a "normal" switch, but a toggle switch.

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: Domoticz MQTT Helper 0 then 1

#3 Post by hestia » 13 Jul 2021, 00:15

I've just checked, and it looks good
Screenshot 2021-07-13 001205.png
Screenshot 2021-07-13 001205.png (58.4 KiB) Viewed 4103 times

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: Domoticz MQTT Helper 0 then 1

#4 Post by hestia » 13 Jul 2021, 22:12

I've removed the Domoticz MQTT Helper and made a custom script
on dz

Code: Select all

--[[
13/07/2021 - turn on /off a switch from / to esp <-> dz
--]]

local SWITCH_ID = 1893
local DEVICES = {SWITCH_ID}
local RELAY_ID = '4'
local MQTTEVENT = {"aquaOnOffPump"}
local ESP_NAME = 'aquaonoff'

return {
    logging =   {
        level   =   
                domoticz.LOG_ERROR, --select one to override system log level normal = LOG_ERROR
                --domoticz.LOG_DEBUG,
                --domoticz.LOG_FORCE
	            },
    on =        {
                devices = DEVICES,
                customEvents = MQTTEVENT
  	            },

    execute = function(dz, item, triggerInfo)
    _G.logMarker =  dz.moduleLabel -- set logmarker to scriptname 

 	local LOG_LEVEL = dz.LOG_INFO -- normal 
    --local LOG_LEVEL = dz.LOG_DEBUG
 	
 	local l_logId = 0
 	local l_logIdx 
    local function logWrite(str, level)  -- Support function for shorthand debug log statements
        l_logId = l_logId + 1
        if l_logId < 10 then
            l_logIdx = '0' .. l_logId
        else
            l_logIdx = l_logId         
        end
        if level == nil then
            --level = dz.LOG_INFO
            level = dz.LOG_DEBUG
        end
        dz.log(l_logIdx .. ": " .. tostring(str), level)
    end


    local function cmdToESP (theItemId_i, theOnOff)
        logWrite('urlToESP')
        -- send the command to ESP

        local esp_event = 'DzRelay'..theItemId_i..'='..theOnOff
        local urlToESP = 'http://'..ESP_NAME..'/control?cmd=asyncevent,'..esp_event
        logWrite('urlToESP ' .. urlToESP)
        dz.openURL(urlToESP)
   
        return
    end
    
----------------------------------------------------------------------------------

    logWrite('*** start *** ' .. triggerInfo.type)

    if item.isDevice then
        logWrite('Switch ' .. item.id .. ' ' .. item.name)
        if item.id == SWITCH_ID then
            if dz.devices(SWITCH_ID).active then
                cmdToESP(RELAY_ID, 1)
            else
                cmdToESP(RELAY_ID, 0)
            end
        else
            logWrite('Unexpeted Error ' .. item.id, dz.LOG_ERROR)
       end

    elseif item.isCustomEvent then
        logWrite('MQTT ' .. item.data)
        if item.data == '1' then
            dz.devices(SWITCH_ID).switchOn().silent()
        elseif item.data == '0' then
            dz.devices(SWITCH_ID).switchOff().silent()
        else
            logWrite('MQTT ' .. item.data, dz.LOG_ERROR)
        end

    else
        logWrite('trigger item? ' .. triggerInfo.type, dz.LOG_ERROR)
    end
    
end}
on esp

Code: Select all

on Button4#State do // the pump
	gpio,15,=![Relay4#State]
        Let,6,1 // button
endon

on DzRelay4 do
        gpio,15,%eventvalue1%
        Let,6,0 // dz
endon

on Relay4#State do
	if [Relay4#State]=0
	AsyncEvent,StopRelay123 // if the pump is turned off all relays are turned off
	endif
       if [int#6]=1 // from the button (not dz)
          Publish domoticz/in,'{"command": "customevent", "event": "aquaOnOffPump", "data": [Relay4#State]}'
       endif
endon
More complicated, but as I've already some scripts...

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests