GetCBusByKW - Multiple args

Discussion in 'C-Bus Automation Controllers' started by Timbo, Apr 11, 2022.

  1. Timbo

    Timbo

    Joined:
    Aug 3, 2004
    Messages:
    22
    Likes Received:
    4
    Location:
    Perth, Australia
    Hi All,

    Does anybody know how I can pass GetCBusByKW a longer list of args other than just 2 (which seems to work)..?
    I have a function which pokes the correct data into MQTT auto-discover - works a treat by the way!!
    As I augment my function to cater for different types of device, I've hit a wall.

    Code:
    function mqttdiscover()
        cbusdump = GetCBusByKW({"MQTT_Light","MQTT_Window","MQTT_Exhaust","MQTT_Blind"}, 'or')  -- THIS DOES NOT WORK BEYOND 2 ARGS --
        log("MQTT cover/window/exhaust - starting the auto-discovery build")
    
        for key1, object in pairs(cbusdump) do
          objectname = object["name"]
          objectnet = object["address"][1]
          objectapp = object["address"][2]
          objectgroupid = object["address"][3]
            
          for key2, keyword in pairs(object["keywords"]) do
            if keyword == "MQTT_Light" then
              if debuglog then log("Discovered MQTT_Light: "..objectgroupid) end
              objectid = "cbus_mqtt_light_" .. objectnet .. "_" .. objectapp .. "_" .. objectgroupid
              payload = {
                ["name"] = objectname,
                ["unique_id"] = objectid,
                ["cmd_t"] = "cbus/write/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/switch",
                ["stat_t"] = "cbus/read/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/state",
                ["bri_stat_t"] = "cbus/read/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/level",
                ["bri_cmd_t"] = "cbus/write/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/ramp",
                ["pl_on"] = "ON",
                ["pl_off"] = "OFF",
                ["on_cmd_type"] = "brightness"
              }
              haintegrationtype = "light"
            end
    
              if keyword == "MQTT_Window" then
                if debuglog then log("Discovered MQTT_Window: "..objectgroupid) end
                objectid = "cbus_mqtt_window_" .. objectnet .. "_" .. objectapp .. "_" .. objectgroupid
                payload = {
                  ["name"] = objectname,
                  ["unique_id"] = objectid,
                  ["dev_cla"] = "window",
                  ["position_topic"] = "cbus/read/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/level",
                  ["set_pos_t"] = "cbus/write/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/ramp",
                  ["pos_open"] = 255,
                  ["pos_clsd"] = 0,
                  ["availability"] = { ["topic"] = "cbus/read/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/availability" }
                }
                haintegrationtype = "cover"
              end
    
              if keyword == "MQTT_Exhaust" then
                if debuglog then log("Discovered MQTT_Exhaust: "..objectgroupid) end
                objectid = "cbus_mqtt_exhaust_" .. objectnet .. "_" .. objectapp .. "_" .. objectgroupid
                payload = {
                  ["name"] = objectname,
                  ["unique_id"] = objectid,
                  ["cmd_t"] = "cbus/write/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/switch",
                  ["stat_t"] = "cbus/read/" .. objectnet .. "/" .. objectapp .. "/" .. objectgroupid .. "/state",
                  ["pl_on"] = "ON",
                  ["pl_off"] = "OFF"             
                }
                haintegrationtype = "fan"
              end
    
              if keyword == "MQTT_Blind" then
                -- do something
              end
            end
    
            local jsonpayload = json.encode( payload )
            local publishtopic = mqtt_autodiscover_topic .. "/" .. haintegrationtype .. "/" .. objectid .. "/config"
        
            if debuglog then log("Complete topic and JSON encoded BLOB for group "..objectgroupid, publishtopic, jsonpayload ) end
            client:publish(publishtopic, jsonpayload, 1, true)
          end
        end
    
    Any ideas?!!?

    Cheers,

    Tim
     
    Timbo, Apr 11, 2022
    #1
  2. Timbo

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    i did some testing, i can receive more than 2 args in the GetCBusByKW() function no problem

    cbusdump = GetCBusByKW({"MQTT_Window", "Fan_Speed", "Bathroom","MQTT_Blind", "Kitchen" }, 'or')
    log(cbusdump)

    I dont have any tags called MQTT_Window or MQTT_Blind in my project, but i did receive all other objects with the other valid tags in the cbusdump table.

    maybe add some logging, to see when the breakdown happens,

    also objectname = object["name"] dosnt always exist (well not for me anyway) eg for user parameter and measurement application the value "name" in the table is nil
    you could try commenting this out and see if anything changes?

    you could also add some checking eg

    if objectname then -- continue on as there is a value that is not nil inside variable objectname

    end
     
    Pie Boy, Apr 12, 2022
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.