Query a device managed by a Fibaro HC3 Topic is solved

Moderator: leecollings

Post Reply
jguellec
Posts: 5
Joined: Saturday 09 May 2020 17:51
Target OS: -
Domoticz version:
Contact:

Query a device managed by a Fibaro HC3

Post by jguellec »

Hi everyone,

I’m starting with LUA and I need help from you guys :)

I’m trying to query a door sensor managed by my Fibaro HC3. I need to get the ‘batteryLevel” parameter in my Domoticz installation.

With a web browser, I successfully query the HC3 API and display the device’s informations :
Screenshot 2020-05-09 at 17.59.36.png
Screenshot 2020-05-09 at 17.59.36.png (139.64 KiB) Viewed 2933 times
Please note the browser is asking me to authenticate to perform the query. I guess this might be the problem in my LUA.

So far, this is what I have. I’ve honestly tried a lot of different things and now I’m lost… :

Code: Select all

handle = io.popen("https://login:[email protected]/api/devices/184")
devicesJson = handle:read('*all')
handle:close()
devices = json:decode(devicesJson)
 
local batterylevel = devices.properties.batterylevel
print(batterylevel)
Any help would be much appreciated !
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Query a device managed by a Fibaro HC3

Post by waaren »

jguellec wrote: Sunday 10 May 2020 12:29 I’m trying to query a door sensor managed by my Fibaro HC3. I need to get the ‘batteryLevel” parameter in my Domoticz installation.
Any help would be much appreciated !
Do you want the batterlevel as a separate device or add it as batteryLevel of an existing device? Please state the devicetype
What do see when you use curl -m5 "https://login:[email protected]/api/devices/184" from the command line on the domoticz system?
If you could send the raw JSON return I might be able to help with a dzVents Lua script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jguellec
Posts: 5
Joined: Saturday 09 May 2020 17:51
Target OS: -
Domoticz version:
Contact:

Re: Query a device managed by a Fibaro HC3

Post by jguellec »

Thanks for your reply waaren.

I just want to store the batteryLevel in a local variable. No need to create a new device in Domoticz.

I already have a LUA script that sends weekly email and pushover notifications of my battery-powered devices. I just miss my door sensors because they are managed in the HC3.

I'm not quite sure what you mean with curl (sorry... I'm beginning). I've tried this :

Code: Select all

handle = io.popen("curl -m5 'https://login:[email protected]/api/devices/184'")
devicesJson = handle:read('*all')
handle:close()
devices = json:decode(devicesJson)
    
local batterylevel = devices.properties.batterylevel
print(batterylevel)
It seems to work, but then I have this in my Log :

Code: Select all

attempt to index a nil value (global 'devices')
The JSON return :

Code: Select all

{
  "id": 184,
  "name": "Porte Chambre",
  "roomID": 228,
  "view": {
    "assetsPath": "/dynamic-plugins/com.fibaro.securitySensor/assets",
    "jsPath": "/dynamic-plugins/com.fibaro.securitySensor",
    "name": "com.fibaro.securitySensor",
    "translatesPath": "/dynamic-plugins/com.fibaro.securitySensor/i18n",
    "type": "ts"
  },
  "type": "com.fibaro.doorSensor",
  "baseType": "com.fibaro.doorWindowSensor",
  "enabled": true,
  "visible": true,
  "isPlugin": false,
  "parentId": 183,
  "viewXml": false,
  "configXml": false,
  "interfaces": [
    "battery",
    "fibaroAlarm",
    "fibaroAlarmArm",
    "fibaroBreach",
    "fibaroFirmwareUpdate",
    "tamper",
    "zwave",
    "zwaveAlarm",
    "zwaveMultiChannelAssociation",
    "zwaveWakeup"
  ],
  "properties": {
    "parameters": [
      {
        "id": 1,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 1,
        "value": 0
      },
      {
        "id": 2,
        "lastReportedValue": 6,
        "lastSetValue": 6,
        "size": 1,
        "value": 6
      },
      {
        "id": 3,
        "lastReportedValue": 3,
        "lastSetValue": 3,
        "size": 1,
        "value": 3
      },
      {
        "id": 11,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 1,
        "value": 0
      },
      {
        "id": 12,
        "lastReportedValue": 255,
        "lastSetValue": 255,
        "size": 2,
        "value": 255
      },
      {
        "id": 13,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 2,
        "value": 0
      },
      {
        "id": 14,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 2,
        "value": 0
      },
      {
        "id": 15,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 2,
        "value": 0
      },
      {
        "id": 30,
        "lastReportedValue": 5,
        "lastSetValue": 5,
        "size": 2,
        "value": 5
      },
      {
        "id": 31,
        "lastReportedValue": 1,
        "lastSetValue": 1,
        "size": 1,
        "value": 1
      },
      {
        "id": 50,
        "lastReportedValue": 300,
        "lastSetValue": 300,
        "size": 2,
        "value": 300
      },
      {
        "id": 51,
        "lastReportedValue": 10,
        "lastSetValue": 10,
        "size": 2,
        "value": 10
      },
      {
        "id": 52,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 2,
        "value": 0
      },
      {
        "id": 53,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 2,
        "value": 0
      },
      {
        "id": 54,
        "lastReportedValue": 0,
        "lastSetValue": 0,
        "size": 1,
        "value": 0
      },
      {
        "id": 55,
        "lastReportedValue": 350,
        "lastSetValue": 350,
        "size": 2,
        "value": 350
      },
      {
        "id": 56,
        "lastReportedValue": 100,
        "lastSetValue": 100,
        "size": 2,
        "value": 100
      }
    ],
    "pollingTimeSec": 0,
    "wakeUpTime": 21600,
    "zwaveCompany": "Fibargroup",
    "zwaveInfo": "3,4,38",
    "zwaveVersion": "3.2",
    "alarmDelay": 0,
    "alarmExclude": true,
    "alarmLevel": 0,
    "alarmTimeTimestamp": 0,
    "alarmType": 0,
    "armConditions": {
      "auto": false,
      "devices": [
        {
          "id": 164,
          "propertyName": "value",
          "propertyValue": "0"
        }
      ],
      "time": 0
    },
    "armConfig": 0,
    "armDelay": 0,
    "armError": {},
    "armTimeTimestamp": 0,
    "armed": false,
    "batteryLevel": 60,
    "batteryLowNotification": true,
    "categories": [
      "security"
    ],
    "configured": true,
    "dead": false,
    "deadReason": "",
    "defInterval": 0,
    "deviceControlType": 0,
    "deviceIcon": 42,
    "emailNotificationID": 0,
    "emailNotificationType": 0,
    "endPointId": 0,
    "fibaroAlarm": false,
    "firmwareUpdate": {
      "info": "",
      "progress": 0,
      "status": "UpToDate",
      "updateVersion": "3.2"
    },
    "lastBreached": 1589104821,
    "log": "",
    "logTemp": "",
    "manufacturer": "",
    "markAsDead": true,
    "maxInterval": 0,
    "minInterval": 0,
    "model": "",
    "nodeId": 27,
    "parametersTemplate": "802",
    "pendingActions": false,
    "productInfo": "1,15,7,2,16,0,3,2",
    "pushNotificationID": 0,
    "pushNotificationType": 0,
    "remoteGatewayId": 0,
    "saveLogs": true,
    "serialNumber": "h'000000000001a775",
    "smsNotificationID": 0,
    "smsNotificationType": 0,
    "stepInterval": 0,
    "tamper": true,
    "updateVersion": "",
    "useTemplate": true,
    "userDescription": "",
    "value": true
  },
  "actions": {},
  "created": 1588543434,
  "modified": 1588543434,
  "sortOrder": 63
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Query a device managed by a Fibaro HC3

Post by waaren »

jguellec wrote: Sunday 10 May 2020 13:06 I just want to store the batteryLevel in a local variable. No need to create a new device in Domoticz.
This is how to it could work in dzVents:

When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Code: Select all

local scriptVar = 'getFibaro' 
local url = "https://login:[email protected]/api/devices/184"

return 
{
    on = 
    {
        timer = 
        {
            'every 5 minutes'
        },

        httpResponses = 
        {
            scriptVar,
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LEG_ERROR when all is ok
    },
    
    execute = function(dz, item)
        
        batteryLevel = dz.variables('Fibaro device  batteryLevel') -- change to name of your variable
        
        if item.isTimer or item.isDevice then
            dz.openURL({
                url = url,
                method = 'GET',
                callback = scriptVar, 
            })
            return
        end

        if item.isHTTPResponse and item.ok then
            local rt = dz.utils.fromJSON(item.data:gsub("'","")) -- in your example there was a single quote ruining the json. Filter that one out before processing
            local newBatterylevel = rt.properties.batteryLevel   
            local oldBatteryLevel = batteryLevel.value

            if oldBatteryLevel ~= newBatterylevel then 
                dz.log('Old batteryLevel is ' ..oldBatteryLevel .. '%, new batteryLevel: ' ..  newBatterylevel .. ' %', dz.LOG_DEBUG)
                batteryLevel.set(newBatterylevel)
            else
                dz.log('Battery level did not change. It is stil ' .. oldBatteryLevel .. '% ', dz.LOG_DEBUG)
            end
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, 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
jguellec
Posts: 5
Joined: Saturday 09 May 2020 17:51
Target OS: -
Domoticz version:
Contact:

Re: Query a device managed by a Fibaro HC3

Post by jguellec »

Wow ! Thanks !!

I'll try this tonigh and let you know how it works.

Much appreciated :)
jguellec
Posts: 5
Joined: Saturday 09 May 2020 17:51
Target OS: -
Domoticz version:
Contact:

Re: Query a device managed by a Fibaro HC3

Post by jguellec »

I quickly added the dzVents before leaving the house. I made sure of the prerequisites you mentioned.

This is what I have :
Screenshot 2020-05-10 at 15.18.52.png
Screenshot 2020-05-10 at 15.18.52.png (176.35 KiB) Viewed 2922 times
I made sure I could ping the IP address from Domoticz : OK

I'm not quite sure why it's trying to resolve host name ? The IP address should be enough ?

I'll have a look later today. I'll try to create a DNS record maybe.

Anyway, thanks again for your help :)
jguellec
Posts: 5
Joined: Saturday 09 May 2020 17:51
Target OS: -
Domoticz version:
Contact:

Re: Query a device managed by a Fibaro HC3

Post by jguellec »

@waaren : it’s working now. I had special characters in the password I used. I created a new user in the HC3 with basic login/pwd and with a very limited access on the platform.

Everything’s working as expected.

A HUGE THANK YOU to you. This was VERY helpful :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest