ifttt node-red mqtt and dzvents

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:

ifttt node-red mqtt and dzvents

Post by pvklink »

Better to place the dzvents part of my ifttt, node-red - mqtt script here
I can manage the uservar IFTTTVAR now by speech.
In IFTTT i have made a webhook with a {"value":"{{TextField}}"} body
My Node-red receives this and makes an complete json body for an update of an user-var iftttvar
in this var a name of a device or scene or group or selector whatever On or Off is registrated.

After that the next script fires
is still very global and has hardcoded values, but it is working!

to do:
search in domoticz if the name is an device, selector, scene, group etc.
split the device name and on or off
put the object on/off

@waaren is this wat you mentioned ?

Code: Select all

return {
            on =    {  variables = { iftttvar },   -- var, define as type string
                    },
                    
        logging =   {   level     =   domoticz.LOG_ERROR,
                        marker    =   "iftttvar"   },

    execute = function(dz, item)
        local iftttvar = dz.variables(iftttvar)
        
            if iftttvar.value == 'lamp ON' then             --retrieve if it is a switch, a scene, a group etc. then if it is on or off and then action
                 dz.devices('lantaarn').switchOn()
            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: ifttt node-red mqtt and dzvents

Post by pvklink »

few errors on split function... almost there...

Code: Select all

local iftttvar = "iftttvar"  -- var, define as type string

return {
            on =    {  variables = { iftttvar },   -- var, define as type string
                    },
                    
        logging =   {   level     =   domoticz.LOG_ERROR,
                        marker    =   "iftttvar"   },

    execute = function(dz, item)
    local utils = require("Utils") 
    local iftttvar = dz.variables(iftttvar)
    local iftttvar2 = iftttvar.value
    result= utils.dumpTable(utils.stringSplit(iftttvar2," "))
    dz.log("Waarde1: " .. result[1] .. " ", dz.LOG_INFO)


            if result[2] == 'ON' then             --retrieve if it is a switch, a scene, a group etc. then if it is on or off and then action
                 dz.devices(result[1]).switchOn()
            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: ifttt node-red mqtt and dzvents

Post by pvklink »

Got it working!
Only thing i am looking for is
a) when the object is a device, the group and scene check gives an error

Code: Select all

local iftttvar = "iftttvar"  -- var, define as type string

return {
            on =    {  variables = { iftttvar },   -- var, define as type string
                    },
                    
        logging =   {   level     =   domoticz.LOG_ERROR,
                        marker    =   "iftttvar"   },

    execute = function(dz, item)
    local utils = require("Utils") 
    local iftttvar = dz.variables(iftttvar)

    result = dz.utils.stringSplit(iftttvar.value," ")
    utils.dumpTable(utils.stringSplit(iftttvar.value," "))

    if result[2] == 'ON' then             --retrieve if it is a switch, a scene, a group etc. then if it is on or off and then action
        if dz.devices(result[1]) ~= ''    then 
            if result[3] ~= nil then 
                dz.devices(result[1]).switchSelector(result[3])
            else
                dz.devices(result[1]).switchOn() 
            end
        end
        if dz.groups(result[1]) ~= nil then dz.groups(result[1]).switchOn() end
        if dz.scenes(result[1]) ~= nil then dz.scenes(result[1]).switchOn() end 
    end
end
}
-06-12 19:43:38.820 Status: User: Admin initiated a switch command (1002/lantaarn/Off)
2019-06-12 19:43:45.538 Status: dzVents: > 1: lantaarn
2019-06-12 19:43:45.538 Status: dzVents: > 2: ON
2019-06-12 19:43:45.560 Status: dzVents: Error (2.4.23): iftttvar: There is no group or scene with that name or id: lantaarn. If you just created the scene or group you may have to restart Domoticz to make it become visible to dzVents.
2019-06-12 19:43:45.560 Status: dzVents: Error (2.4.23): iftttvar: There is no group or scene with that name or id: lantaarn. If you just created the scene or group you may have to restart Domoticz to make it become visible to dzVents.
2019-06-12 19:43:45.562 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-06-12 19:45:49.332 (zwavepluspvk) General/Voltage (Voltage)
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: ifttt node-red mqtt and dzvents

Post by pvklink »

when starting with an group object, the devices check finishes the script... same for scenes

2019-06-12 20:22:49.681 MQTT: Topic: domoticz/in, Message: {"value":"Sfeerverlichting/ON/","item":"","command":"setuservariable","idx":12}
2019-06-12 20:22:49.783 Status: dzVents: > 1: Sfeerverlichting
2019-06-12 20:22:49.783 Status: dzVents: > 2: ON
2019-06-12 20:22:49.783 Status: dzVents: Error (2.4.23): iftttvar: There is no device with that name or id: Sfeerverlichting
2019-06-12 20:22:49.783 Status: dzVents: Error (2.4.23): iftttvar: There is no device with that name or id: Sfeerverlichting
2019-06-12 20:22:49.783 Status: dzVents: Error (2.4.23): iftttvar: An error occurred when calling event handler DZ_Ifttt_name
2019-06-12 20:22:49.783 Status: dzVents: Error (2.4.23): iftttvar: ...ticz/scripts/dzVents/generated_scripts/DZ_Ifttt_name.lua:28: attempt to index a nil value
2019-06-12 20:22:50.588 (zwavepluspvk) Usage (Unknown)

Code: Select all

-- message switch {"value":"usb lamp","item":"Flame"}
-- message selector {"value":"lantaarn","item":""}
-- message group {"value":"Sfeerverlichting","item":""}


local iftttvar = "iftttvar"  

return {
            on =    {  variables = { iftttvar },   
                    },
                    
        logging =   {   level     =   domoticz.LOG_ERROR,
                        marker    =   "iftttvar"   },

    execute = function(dz, item)
    local utils = require("Utils") 
    local iftttvar = dz.variables(iftttvar)
    local delim = '/'

    result = dz.utils.stringSplit(iftttvar.value,delim)
    utils.dumpTable(utils.stringSplit(iftttvar.value,delim))

    if result[2] == 'ON' then             
        if dz.devices(result[1]) ~= ''    then 
            if result[3] ~= nil then 
                dz.devices(result[1]).switchSelector(result[3])
            else
                dz.devices(result[1]).switchOn() 
            end
        elseif dz.groups(result[1]) ~= nil then -- does not start because thirst devices if gives an error when the object isnt a device
            dz.groups(result[1]).switchOn()
        elseif dz.scenes(result[1]) ~= nil then -- does not start, see above
            dz.scenes(result[1]).switchOn() 
        end 
    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: ifttt node-red mqtt and dzvents

Post by waaren »

pvklink wrote: Wednesday 12 June 2019 20:22 when starting with an group object, the devices check finishes the script... same for scenes
starting small.. But already functional.

The error messages when checking for a none existing device / scene / group can be ignored but do not look good in the log. Will investigate further for a resource friendly method to prevent these 'warnings'

Code: Select all

local iftttvar = "IFTTT"  

return 
{
    on =    
    { 
        variables = { iftttvar },   
    },

    logging =
    {   
        level = domoticz.LOG_DEBUG,
        marker = iftttvar,
   },

    execute = function(dz, item)
        result = dz.utils.stringSplit(item.value,'/')
        dz.utils.dumpTable(result)

        if             dz.devices(result[1]) then target = dz.devices(result[1])
        elseif        dz.groups(result[1]) then target = dz.groups(result[1])
        elseif        dz.scenes(result[1]) then target = dz.scenes(result[1])
        else dz.log(result[1] .. " Not found",dz.LOG_ERROR) return
        end

        if result[2] == 'ON' then
            target.switchOn()
        else
            target.switchOff()
        end
    end
}
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: ifttt node-red mqtt and dzvents

Post by pvklink »

added selector switch, ad THANKS, it WORKS!

Code: Select all

-- message switch {"value":"usb lamp","item":"Flame"}
-- message selector {"value":"lantaarn","item":""}
-- message group {"value":"Sfeerverlichting","item":""}

local iftttvar = "iftttvar"  

return { on =    {  variables = { iftttvar },   
                    },
                    
        logging =   {   level     =   domoticz.LOG_ERROR,
                        marker    =   "ifttt"   },

    execute = function(dz, item)
    local utils = require("Utils") 
    local iftttvar = dz.variables(iftttvar)
    local delim = '/'

    result = dz.utils.stringSplit(iftttvar.value,delim)
    utils.dumpTable(utils.stringSplit(iftttvar.value,delim))

    if            dz.devices(result[1]) then target = dz.devices(result[1])
    elseif        dz.groups(result[1])  then target = dz.groups(result[1])
    elseif        dz.scenes(result[1])  then target = dz.scenes(result[1])
    else          dz.log(result[1] .. " Not found",dz.LOG_ERROR) return
    end

    if result[2] == 'ON' then
        if result[3] ~= nil then 
            target.switchSelector(result[3])
        else
            target.switchOn() 
        end
    else
        target.switchOff()
    end
end
}[code]
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: ifttt node-red mqtt and dzvents

Post by waaren »

pvklink wrote: Wednesday 12 June 2019 23:14added selector switch, ad THANKS, it WORKS!
Found a solution to check without getting error messages in the logfile.

Code: Select all

local iftttvar = "iftttvar"  

return 
{
    on =    
    { 
        variables = { iftttvar },   
    },
                    
    logging =
    {   
        level = domoticz.LOG_DEBUG,
        marker = 'ifttt',
   },

    execute = function(dz, item)
        local validBaseTypes = { device=true, group=true, scene=true, uservariable=false, camera=false }
        local validActions = { ON=true, OFF=true }
        result = dz.utils.stringSplit(item.value,'/')
        local name = result[1]
        local action = result[2]
        local parm = result[3]     
        
        local function getBaseType(name)
            for i, tuple in ipairs(_G.domoticzData) do 
                if tuple.name == name then 
                    return tuple.baseType, tuple.id 
                end
            end
        end
        
        local baseType, idx = getBaseType(name)
        if baseType and validBaseTypes[baseType] then
            local target 
            if baseType == 'device' then 
                target = dz.devices(name)
            elseif baseType == 'group' then 
                target = dz.groups(name)
            elseif baseType == 'scene' then 
                target = dz.scenes(name) 
                validActions.OFF = nil -- No Off action for scenes
            end
            if validActions[action] then
                if action == 'ON' then 
                    if parm then
                        target.switchSelector(parm) 
                    else
                        target.switchOn() 
                    end
                elseif action == 'OFF' then target.switchOff() 
                else -- no other actions implemented yet
                end
            else 
                dz.log('action ' .. action .. ' is not implemented for ' .. baseType .. ' ' .. name .. '!',dz.LOG_ERROR) 
            end
        elseif baseType then
            dz.log(name .. " is a " .. baseType .. " don't know what to do with that!",dz.LOG_ERROR) 
        else
            dz.log(name .. " Not found!",dz.LOG_ERROR) 
        end
    end
}
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: ifttt node-red mqtt and dzvents

Post by pvklink »

works Perfect!
This script is now a sort of API for MQTT and IFTTT (i use node-red in between)
In IFFF i have a simple json body
{"value":" ,{{TextField}}","item",""}
i use item for the selector value....

it calls a ON-flow which adds the rest of the values like the idx of the uservar and the ON statement
When IFTTT supports more then one TextFields i only need one flow for ON and OFF...

Thanks again!
I am out of projects now, everything works!
Need a new challenge :-)


node-red code

Code: Select all

[{"id":"7d0d51d5.bda63","type":"tab","label":"switch by name uservar","disabled":false,"info":""},{"id":"efd89514.ce9718","type":"http in","z":"7d0d51d5.bda63","name":"LIVE met post","url":"/pvkapi4","method":"post","upload":false,"swaggerDoc":"","x":150,"y":300,"wires":[["1181d507.972f4b","7654a96c.61c898"]]},{"id":"1181d507.972f4b","type":"http response","z":"7d0d51d5.bda63","name":"responce post","statusCode":"","headers":{"content-type":"application/json"},"x":320,"y":80,"wires":[]},{"id":"f1f5763f.01d648","type":"mqtt out","z":"7d0d51d5.bda63","name":"","topic":"domoticz/in","qos":"","retain":"","broker":"21fd69bf.668b46","x":850,"y":180,"wires":[]},{"id":"d0f0155b.51fc88","type":"debug","z":"7d0d51d5.bda63","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":850,"y":300,"wires":[]},{"id":"7654a96c.61c898","type":"function","z":"7d0d51d5.bda63","name":"changemsg","func":"//var onoff = context.get('onoff')||'ON';\nonoff = 'ON';\ndelim = '/';\nitem = msg.payload.item;\n\n// store the value back\n//context.set('onoff',onoff);\nmsg.payload.command = \"setuservariable\";\nmsg.payload.idx = 12;\nmsg.payload.value = msg.payload.value + delim + onoff + delim + item;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":300,"wires":[["f1f5763f.01d648","d0f0155b.51fc88"]]},{"id":"f533574f.ce41e8","type":"inject","z":"7d0d51d5.bda63","name":"input switch lantaarn by name","topic":"","payload":"{\"value\":\"lantaarn\",\"item\":\"\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":420,"wires":[["7654a96c.61c898"]]},{"id":"83b507da.d09258","type":"inject","z":"7d0d51d5.bda63","name":"input selector usb lamp by name","topic":"","payload":"{\"value\":\"usb lamp\",\"item\":\"Flame\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":460,"wires":[["7654a96c.61c898"]]},{"id":"78f9f3e.f12710c","type":"inject","z":"7d0d51d5.bda63","name":"input group Sfeerverlichting by name","topic":"","payload":"{\"value\":\"Sfeerverlichting\",\"item\":\"\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":500,"wires":[["7654a96c.61c898"]]},{"id":"21fd69bf.668b46","type":"mqtt-broker","z":"","name":"mqttdomoticz","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
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: ifttt node-red mqtt and dzvents

Post by waaren »

pvklink wrote: Thursday 13 June 2019 22:59 works Perfect! This script is now a sort of API for MQTT and IFTTT (i use node-red in between)
Thx. for the feedback. What I do not understand about this (I am probably overlooking something) is why you need MQTT and node-red. I did not have time yet to study this in much detail but using the webhook maker in IFTTT and the dzVents script I posted I already managed to control devices in domoticz.

Code: Select all

dz.triggerIFTTT -> IFTTT webhook -> update domoticz variable -> dzVents script -> do stuff in domoticz
  • still need to work out
    • Google home or Alexa voice command -> IFTTT -> update domoticz variable -> dzVents script -> do stuff in domoticz
So maybe that could be a new project for you :) Creating a workbook on how you set this up so others can copy your method?
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: ifttt node-red mqtt and dzvents

Post by pvklink »

Hi, you dont need MQTT and NODE_RED for using google home with domoticz via IFTTT.
I wanted to learn more about message brokers, so i installed MQTT (default) with the domoticz mqtt plugin as already described....
Problem was that IFTTT cant talk with MQTTT so that domoticz can use the messages well...
It needs some transformations of the messages.... Another thing was that i also wanted to learn how to use a json body (POST) instead of GET's...
NODE-red did the thing....i can use both now....

Pure hobby and a drive to understand things/technics and a drive to get it working.

My implementation is useful when you have more then one domotica environments, and want to connect to them at a application independent way, by messaging....

With default installations of NODE-RED and MQTTT and documentation that is already there you can get it working.
importing the above NODE-RED flow for managing a USERVAR via MQTTT and NODERED is the only thing to do...
When sending get's and posts via IFTTT and NODE-RED i have two different flows in NODE-RED...
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: No registered users and 1 guest