Logic Machine Forum
Read Data from Table - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Read Data from Table (/showthread.php?tid=1115)



Read Data from Table - Alphaplasti - 28.11.2017

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


RE: Read Data from Table - admin - 28.11.2017

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)



RE: Read Data from Table - Alphaplasti - 28.11.2017

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 ?


RE: Read Data from Table - admin - 28.11.2017

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)



RE: Read Data from Table - Alphaplasti - 28.11.2017

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 ?


RE: Read Data from Table - admin - 28.11.2017

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



RE: Read Data from Table - Alphaplasti - 28.11.2017

(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 !!


RE: Read Data from Table - toujour - 14.02.2020

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


RE: Read Data from Table - AlexD - 11.10.2022

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"


RE: Read Data from Table - admin - 11.10.2022

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.