Sending JSON payload into Tasmota via MQTT

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

Moderator: leecollings

Post Reply
titvs
Posts: 6
Joined: Sunday 20 October 2019 17:35
Target OS: Windows
Domoticz version:
Contact:

Sending JSON payload into Tasmota via MQTT

Post by titvs »

Hi everyone.

Im trying to do a simple on/off switch in Domoticz to send the appropriate json payload into a Tasmota device that will turn on or off an AC unit, using a virtual switch.
Im not into dzVents so i asked Bing Chat :P and it came up with the following script:

Code: Select all

return {
    on = {
        devices = {
            'AC_ESCRIT'
        }
    },
    execute = function(domoticz, device)
        if device_state == 'On' then
            local mqttCommand = {
                Vendor = "FUJITSU_AC",
                Model = "ARRAH2E",
                Command = "Control",
                Mode = "Auto",
                Power = "On",
                Celsius = "On",
                Temp = 23,
                FanSpeed = "Auto",
                SwingV = "Off",
                SwingH = "Off",
                Quiet = "Off",
                Turbo = "Off",
                Econo = "Off",
                Light = "Off",
                Filter = "Off",
                Clean = "Off",
                Beep = "Off",
                Sleep = -1,
                iFeel = "Off",
                SensorTemp = 0
            }
            domoticz_publish('cmnd/tasmota_F6CD26/IRHVAC', domoticz_utils_toJSON(mqttCommand))
        end
    end
}
(had to switch the . to _ or the forum woudnt let me publish the code).

If i run this script and press the virtual switch i get the error: field 'publish' is not callable (a nil value)

This seems to be a rather simple thing to do but i couldn't find info in the wikis... Also i would prefer to publish the json payload into the mqtt broker rather than opening an URL since the devices IPs can change.

Thanks in advence
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by habahabahaba »

You need this
titvs
Posts: 6
Joined: Sunday 20 October 2019 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by titvs »

Im sorry but i cant figure out in that page how i send the previous data payload into the " cmnd/tasmota_F6CD26/IRHVAC " mqtt topic... The command domoticz_publish doesnt seem to exist anymore so im a bit clueless...
User avatar
waltervl
Posts: 5741
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by waltervl »

First you have to know what is the proper MQTT payload to switch on/off the AC by MQTT.

Then you send it with mosquito_pub in Dzvents through a shell command. See this topic for some examples viewtopic.php?t=32772
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
titvs
Posts: 6
Joined: Sunday 20 October 2019 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by titvs »

Thanks waltervl. Had to do some changes in the server that runs Mosquitto but i managed to send the payload to the tasmota device using the command line:

Code: Select all

mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {\"Vendor\":\"FUJITSU_AC\",\"Model\":\"ARRAH2E\",\"Command\":\"Control\",\"Mode\":\"Auto\",\"Power\":\"On\",\"Celsius\":\"On\",\"Temp\":23,\"FanSpeed\":\"Auto\",\"SwingV\":\"Off\",\"SwingH\":\"Off\",\"Quiet\":\"Off\",\"Turbo\":\"Off\",\"Econo\":\"Off\",\"Light\":\"Off\",\"Filter\":\"Off\",\"Clean\":\"Off\",\"Beep\":\"Off\",\"Sleep\":-1,\"iFeel\":\"Off\",\"SensorTemp\":null}
I had to escape the quotes so they are included so i don get an "invalid JSON" error in the tasmota device. But now im in more JSON hell on Domoticz side. The code i got from the other topic and slightly modified is removing the slashes from the message. Here is the script:

Code: Select all

local myDevicename = 'AC_ESCRIT' -- change to the name of the device that will trigger this script

return
{
    on =
    {
        devices =
        {
            myDevicename,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to LOG_ERROR when ok
    },

    execute = function(dz)

        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_DEBUG)
            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 sendMQTT(message, topic)
            local MQTTTopic = topic or 'domoticz/out'
            osCommand ( 'mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {\"Vendor\":\"FUJITSU_AC\",\"Model\":\"ARRAH2E\",\"Command\":\"Control\",\"Mode\":\"Auto\",\"Power\":\"Off\",\"Celsius\":\"On\",\"Temp\":23,\"FanSpeed\":\"Auto\",\"SwingV\":\"Off\",\"SwingH\":\"Off\",\"Quiet\":\"Off\",\"Turbo\":\"Off\",\"Econo\":\"Off\",\"Light\":\"Off\",\"Filter\":\"Off\",\"Clean\":\"Off\",\"Beep\":\"Off\",\"Sleep\":-1,\"iFeel\":\"Off\",\"SensorTemp\":null}')
        end

        sendMQTT(myMessage, myTopic)

    end
}
I get this on the log and an invalid JSON error on the tasmota device:

Code: Select all

Status: dzVents: Debug: Executing Command: mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {"Vendor":"FUJITSU_AC","Model":"ARRAH2E","Command":"Control","Mode":"Auto","Power":"Off","Celsius":"On","Temp":23,"FanSpeed":"Auto","SwingV":"Off","SwingH":"Off","Quiet":"Off","Turbo":"Off","Econo":"Off","Light":"Off","Filter":"Off","Clean":"Off","Beep":"Off","Sleep":-1,"iFeel":"Off","SensorTemp":null}
It is removing the slashes in the above command... Any thoughts?

Thanks again!
User avatar
jvdz
Posts: 2269
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by jvdz »

Try to double up the \ as that is what LUA requires for a backslash:

Code: Select all

            osCommand ( 'mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {\\"Vendor\\":\\"FUJITSU_AC\\",\\"Model\\":\\"ARRAH2E\\",\\"Command\\":\\"Control\\",\\"Mode\\":\\"Auto\\",\\"Power\\":\\"Off\\",\\"Celsius\\":\\"On\\",\\"Temp\\":23,\\"FanSpeed\\":\\"Auto\\",\\"SwingV\\":\\"Off\\",\\"SwingH\\":\\"Off\\",\\"Quiet\\":\\"Off\\",\\"Turbo\\":\\"Off\\",\\"Econo\\":\\"Off\\",\\"Light\\":\\"Off\\",\\"Filter\\":\\"Off\\",\\"Clean\\":\\"Off\\",\\"Beep\\":\\"Off\\",\\"Sleep\\":-1,\\"iFeel\\":\\"Off\\",\\"SensorTemp\\":null}')
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
titvs
Posts: 6
Joined: Sunday 20 October 2019 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Sending JSON payload into Tasmota via MQTT

Post by titvs »

Thanks jvdz! Got it to work.

Here is my final script:

Code: Select all

local myDevicename = 'AC_ESCRIT' -- change to the name of the device that will trigger this script

return
{
    on =
    {
        devices =
        {
            myDevicename,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- change to LOG_ERROR when ok
    },

    execute = function(dz, device)

        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_DEBUG)
            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 sendMQTT()
            if device.state == 'On' then
                osCommand ( 'mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {\\"Vendor\\":\\"FUJITSU_AC\\",\\"Model\\":\\"ARRAH2E\\",\\"Command\\":\\"Control\\",\\"Mode\\":\\"Heat\\",\\"Power\\":\\"On\\",\\"Celsius\\":\\"On\\",\\"Temp\\":23,\\"FanSpeed\\":\\"Auto\\",\\"SwingV\\":\\"Off\\",\\"SwingH\\":\\"Off\\",\\"Quiet\\":\\"Off\\",\\"Turbo\\":\\"Off\\",\\"Econo\\":\\"Off\\",\\"Light\\":\\"Off\\",\\"Filter\\":\\"Off\\",\\"Clean\\":\\"Off\\",\\"Beep\\":\\"Off\\",\\"Sleep\\":-1,\\"iFeel\\":\\"Off\\",\\"SensorTemp\\":null}')
            else
                osCommand ( 'mosquitto_pub -t cmnd/tasmota_F6CD26/IRHVAC -m {\\"Vendor\\":\\"FUJITSU_AC\\",\\"Model\\":\\"ARRAH2E\\",\\"Command\\":\\"Control\\",\\"Mode\\":\\"Auto\\",\\"Power\\":\\"Off\\",\\"Celsius\\":\\"On\\",\\"Temp\\":23,\\"FanSpeed\\":\\"Auto\\",\\"SwingV\\":\\"Off\\",\\"SwingH\\":\\"Off\\",\\"Quiet\\":\\"Off\\",\\"Turbo\\":\\"Off\\",\\"Econo\\":\\"Off\\",\\"Light\\":\\"Off\\",\\"Filter\\":\\"Off\\",\\"Clean\\":\\"Off\\",\\"Beep\\":\\"Off\\",\\"Sleep\\":-1,\\"iFeel\\":\\"Off\\",\\"SensorTemp\\":null}')
            end
        end

        sendMQTT()

    end
}
I thought this would be easier than it was... :D
Thanks for all the help!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest