This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Read Data from Table
#1
Hi everyone, I follow this forum since many month and it helps me every time, but today I've a problem and i dont find a similar post in this forum :

I want to check a nest smoke detector. Okay for communication, but the answer is :

* table:
[smoke_co_alarms]
 * table:
  [sgUHZf.....APJP7e2-z........Y]
   * table:
    [last_manual_test_time]
     * string: 2017-11-24T19:47:51.000Z
    [co_alarm_state]
     * string: ok
    [ui_color_state]
     * string: green
    [is_manual_test_active]
     * bool: false
    [where_id]
     * string: gBtrRP2jzy1uqWVr.....s8yYloagGi61-wviuEzKpbe...JsA
    [structure_id]
     * string: OA-re4bF-UhfZGCAXXn6......4cMtcYTovEXfZGlY_BMyoipn6UA
    [device_id]
     * string: sgUHZf.....APJP7e2-z........Y
    [battery_health]
     * string: ok
    [smoke_alarm_state]
     * string: ok
    [is_online]
     * bool: true
    [software_version]
     * string: 3.1.3rc2
    [name_long]
     * string: Bedroom Nest Protect
    [last_connection]
     * string: 2017-11-27T14:02:52.906Z
    [locale]
     * string: fr-FR
    [name]
     * string: Bedroom
    [where_name]
     * string: Bedroom


But I don't know how to extract data to a group object (I think is very easy for you but my knowledge in Lua Langage is very low )
For example I want to read the data [co_alarm_state], and I'm forced to use this table, because nest product don't allow more of two http request/minute so I can't just ask direct http request.

Thanks for your help !

Adrien

Ps : I have a Schneider Spacelynk 2.0.1
Reply
#2
Try this, assuming data variable is a Lua table that you get by decoding JSON response:
Code:
alarms = data['smoke_co_alarms']['sgUHZf.....APJP7e2-z........Y']

co_alarm_ok = alarms['co_alarm_state'] == 'ok'
grp.update('1/1/1', co_alarm_ok, dt.bool)

smoke_alarm_ok = alarms['smoke_alarm_state'] == 'ok'
grp.update('1/1/2', smoke_alarm_ok, dt.bool)

battery_health_ok = alarms['battery_health'] == 'ok'
grp.update('1/1/3', battery_health_ok, dt.bool)
Reply
#3
Thanks for your answer, but it doesn't work, I think my script is bad I've only paste your answer after an other script from you about nest thermostat:


"
function request()

  local auth, url, res, stat, hdrs



  require('json')

  require('ssl.https')



  auth = '...'

 

  url = 'https://developer-api.nest.com/devices.json?auth=' .. auth

  res, stat, hdrs = ssl.https.request(url)



  if stat == 307 then

    res = ssl.https.request(hdrs.location)

  end



  return json.decode(res)

end



res, data = pcall(request)
log(data)

alarms = data['smoke_co_alarms']['sgUHZf.....APJP7e2-z........Y']

co_alarm_ok = alarms['co_alarm_state'] == 'ok'
grp.update('1/1/1', co_alarm_ok, dt.bool)
"

But I think it's not the good way .. I have to create an other script ? or an other variable ?
Reply
#4
What kind of error are you getting?

Also try this:
Code:
key, alarms = next(data['smoke_co_alarms'])
co_alarm_ok = alarms['co_alarm_state'] == 'ok'
grp.update('1/1/1', co_alarm_ok, dt.bool)
Reply
#5
Works great ! Thanks a lot !
"co_alarm_ok = alarms['co_alarm_state'] == 'ok' " A last question for undestand, this line is to transform string to boolean, co_alarm_ok = 1 when the string is 'ok', right ?
Reply
#6
It is the same as this code, just much shorter Smile

Code:
if alarms['co_alarm_state'] == 'ok' then
  co_alarm_ok = true
else
  co_alarm_ok = false
end
Reply
#7
(28.11.2017, 14:28)admin Wrote: It is the same as this code, just much shorter Smile

Code:
if alarms['co_alarm_state'] == 'ok' then
 co_alarm_ok = true
else
 co_alarm_ok = false
end

Thanks for your time and yours answers !!
Reply
#8
I'm interested to this application.
How can HL communicate with the Google Nest Protect ? Do you have a script for this ? Can I use the same script of the Google Nest (termostat) ?

I read something about Google API...opened...closed....Is it open now ?

BR,
Alberto
KNX Advanced Partner + Tutor
Reply
#9
Hello.
I saw this post and I had to try this myself, but it did not work. 

This is my script that I have copied from here:

Code:
alarms = data['smoke_co_alarms']['RTMTKxsQTCxzVcsy.................s']

key, alarms = next(data['smoke_co_alarms'])
co_alarm_ok = alarms['co_alarm_state'] == 'ok'
grp.update('4/4/3', co_alarm_ok, dt.bool)

smoke_alarm_ok = alarms['smoke_alarm_state'] == 'ok'
grp.update('4/4/4', smoke_alarm_ok, dt.bool)

battery_health_ok = alarms['battery_health'] == 'ok'
grp.update('4/4/5', battery_health_ok, dt.bool)

And this is the error: 
"Nest Protect 11.10.2022 10:58:19
Resident script:1: attempt to index global 'data' (a nil value)
stack traceback:"


This is the API from google nest: 

"smoke_co_alarms": {
"RTMTKxsQTCxzVcsy.................s": {
"device_id" :"RTMTKxsQTCxzVcsy.................s",
"locale" :"en-US",
"software_version" :"1.01",
"structure_id" :"VqFabWH21nwVyd4RWgJgNb292wa7hG_.....................w",
"name" :"Hallway (upstairs)",
"name_long" :"Hallway Protect (upstairs)",
"last_connection" :"2016-10-31T23:59:59.000Z",
"is_online" :true,
"battery_health" :"ok",
"co_alarm_state" :"ok",
"smoke_alarm_state" :"ok",
"is_manual_test_active" :true,
"last_manual_test_time" :"2016-10-31T23:59:59.000Z",
"ui_color_state" :"gray",
"where_id" :"UNCBGUnN24...",
"where_name" :"Hallway"
Reply
#10
It's unlikely that this old API still works. Nest support can be implemented but it requires a completely new script. I haven't seen an officially supported API for Nest. As with others solution that emulate a mobile app or user interaction it can stop working at any moment.
Reply


Forum Jump: