0

Using the Nest API I am trying to set the nest thermostat's away status

  • Reading & Setting for temperature is working fine.

  • I have the read and write permissions correctly configured for both
    thermostat temperature control and for setting thermostat away

I can read the status correctly. Does anyone with some experience of this API know how to go about setting this status?

in "FirebaseManager.h"

 Firebase *newFirebase2 = [self.rootFirebase childByAppendingPath:@"structures"];
    [newFirebase2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

         // Put structures into a dictionary
         NSMutableDictionary *dict = snapshot.value;
         NSLog(@"\n\n\n1. Away Status =  %@", [dict valueForKey:@"away"]);

         NSLog(@"Dict Contents %@", dict); // <--- Reads thermostat status.  A string either home or away

        dict[@"away"] = @"away";  //<--- Changes status string but not a correct solution, and does not set the stat to away

        //Changes status name but this is not parsed back to firebase
        NSLog(@"new status =  %@", [dict valueForKey:@"away"]);

    }];
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • The question is a little unclear. Do you want to store the string 'away' in firebase? If so, you need a setValue to do the write. – Jay Mar 22 '16 at 18:53
  • Yes thats correct, im trying to store the string "away" in firebase. – JSA986 Mar 22 '16 at 19:10

1 Answers1

3

To update a child value

assume this structure

structures
   structure_id_0
      away: "home"

setting the away node to a string of away (this code is quite verbose so it's easy to follow)

Firebase *structuresRef = [self.rootFirebase childByAppendingPath:@"structures"];

//build a reference to where we want to write structures/structure_id/
Firebase *thisStructureRef = [structuresRef childByAppendingPath:@"structure_id_0"];
Firebase *awayRef = [thisStructureRef childByAppendingPath:@"away"];

[awayRef setValue:@"away"];

Now, if you want to do this for snapshots that have been retrieved via observing a node with FEventTypeChildAdded, the node name will be whatever is used in place of structure_id_0. The is the key of the key:value pair.

That can be obtained via snapshot.key.

So NSString *key = snapshot.key

Substitute the key variable in for @"structure_id_0" in the path.

Also check out Firebase Writing Data, and then the updateChildValues for another option.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thanks Jay. That initially does set status to away, however this is not saved. According to the logs the status is set back to home again immediately after in `- (void)beginSubscriptionForThermostat:(Thermostat *)thermostat` – JSA986 Mar 26 '16 at 16:58
  • If it is set to away, then it is 'saved'. If it changes thereafter in beginSubscription then that's the function to investigate. It wasn't included or mentioned in the original question so maybe updating the question with more complete would help. Perhaps the beginSubscription sets the node to a default state since it appears to be called early in the process (hence the 'begin' part). – Jay Mar 26 '16 at 17:40
  • Thanks again Jay. Will investigate more – JSA986 Mar 26 '16 at 17:43
  • After more investigation every time the method is called the logs say the dictionary value has changed according to the logs in the dictionary but actual thermostat is not changed to reflect this. Same as before when i asked the question initially. Further to this no matter where this is called, called a few times and then set it to home again on the last call. Also the code snippet is not quite correct should be `Firebase *thisStructureRef = [structuresRef childByAppendingPath: @"structure_id_0"]; Firebase *awayRef = [thisStructureRef childByAppendingPath: @"away"];` – JSA986 Mar 26 '16 at 19:28
  • Thanks - I updated the answer. Going back and forth between ObjC and swift.. The code I posted definitely changes the node value to 'away'. If it's not sticking, another piece of code must be changing it. – Jay Mar 26 '16 at 22:31