Search code examples
javascriptjsontasker

Parsing a value from JSON response that keeps changing order


Im trying to parse JSON response from my home automation system in javscript. The response is available here

That is only a small part of the response, also, the order of keys changes on every reboot for reasons i do not know, su using the the number index wont really work i need to be able to store the state value of sensor.out, sensor.in, sensor.door into variables for tasker on andorid i tried to select using the entity.id, but for some reason the code never finished ( i believe i just didnt know what i was doing)


Solution

  • With ES6 you can use the Array#find method:

    response.find(o => o.entity_id == 'sensor.out').state
    

    See snippet:

    var response = [ { "attributes":{ "friendly_name":"door" }, "entity_id":"sensor.door", "last_changed":"2016-12-31T11:15:59.395808+00:00", "last_updated":"2016-12-31T11:15:59.395808+00:00", "state":"closed" }, { "attributes":{ "friendly_name":"In", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.in", "last_changed":"2016-12-31T11:20:02.179821+00:00", "last_updated":"2016-12-31T11:20:02.179821+00:00", "state":"20.7" }, { "attributes":{ "changed_by":null, "code_format":".+", "friendly_name":"panel" }, "entity_id":"alarm_control_panel.panel", "last_changed":"2016-12-31T11:14:56.471966+00:00", "last_updated":"2016-12-31T11:14:56.471966+00:00", "state":"disarmed" }, { "attributes":{ "friendly_name":"Out", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.out", "last_changed":"2016-12-31T11:14:58.452345+00:00", "last_updated":"2016-12-31T11:14:58.452345+00:00", "state":"7.1" }];
    var state = response.find(o => o.entity_id == 'sensor.out').state;
    console.log('sensor.out state is', state);

    Alternatively, you could convert the response to an object with the entity id values as keys, so you can access it like response['session.out'].state:

    response = Object.assign({}, ...response.map( o => ({[o.entity_id]: o}) ));
    

    See snippet:

    var response = [ { "attributes":{ "friendly_name":"door" }, "entity_id":"sensor.door", "last_changed":"2016-12-31T11:15:59.395808+00:00", "last_updated":"2016-12-31T11:15:59.395808+00:00", "state":"closed" }, { "attributes":{ "friendly_name":"In", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.in", "last_changed":"2016-12-31T11:20:02.179821+00:00", "last_updated":"2016-12-31T11:20:02.179821+00:00", "state":"20.7" }, { "attributes":{ "changed_by":null, "code_format":".+", "friendly_name":"panel" }, "entity_id":"alarm_control_panel.panel", "last_changed":"2016-12-31T11:14:56.471966+00:00", "last_updated":"2016-12-31T11:14:56.471966+00:00", "state":"disarmed" }, { "attributes":{ "friendly_name":"Out", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.out", "last_changed":"2016-12-31T11:14:58.452345+00:00", "last_updated":"2016-12-31T11:14:58.452345+00:00", "state":"7.1" }];
    response = Object.assign({}, ...response.map( o => ({[o.entity_id]: o}) ));
    console.log('sensor.out state is', response['sensor.out'].state);