1

I'm having a lot of troubles to get the data from thingspeak into a json response. I have a url and the response of that give me a many fields. This is the response of the JSON:

?({"channel":{"id":XXXXX,"name":"XXXXX","field1":"Temperature","field2":"Humidity","created_at":"2015-11-03T13:12:06Z","updated_at":"2015-11-15T12:07:37Z","last_entry_id":142},"feeds":[{"created_at":"2015-11-14T21:06:16Z","entry_id":136,"field1":"0"},{"created_at":"2015-11-14T21:06:39Z","entry_id":137,"field1":"25"},{"created_at":"2015-11-14T21:06:59Z","entry_id":138,"field1":"24.05"},{"created_at":"2015-11-14T21:07:13Z","entry_id":139,"field1":"24.45"},{"created_at":"2015-11-14T21:08:16Z","entry_id":140,"field1":"24.45"},{"created_at":"2015-11-15T12:06:18Z","entry_id":141,"field1":"24.5"},{"created_at":"2015-11-15T12:07:37Z","entry_id":142,"field1":"21.4"}]})

I want to retrieve the maximum and minimum value of each field1 data. I have been reading and the response is not in int, therefore should be converted to an int array.

This is my code at the moment:

$.getJSON('http://api.thingspeak.com/channels/'+channel+'/field/1.json?callback=?',
                {key: read_API_key, days: "1"},
                function(data) {
                    $.each(data.feeds, function() {
                        var temp_vals = this.field1;
                        var temp_vals_date = this.created_at;
                        console.log(temp_vals);
                    });
                }
            );

I want to search inside feed array looking for the field1 numbers and save into an int array to after do the maths using Math.max.apply(Math, temp_vals); Using the console, the values are saved correctly into temp_vals, but I cannot use that function, with the following error: Function.prototype.apply: Arguments list has wrong type.

Therefore, how can I change the response into a int array or something to be able to find the max and min value? And another quick question, is any way to print the values outside of the $.each(data.feeds,function(){});, because I didn't find a way to do it....

Changing to parseInt:

$.getJSON('http://api.thingspeak.com/channels/'+channel+'/field/1.json?callback=?',
                {key: read_API_key, days: "1"},
                function(data) {
                    $.each(data.feeds, function() {
                        var temp_vals = parseInt(this.field1);
                        var temp_vals_date = this.created_at;
                        var temp_max = Math.max.apply(Math, temp_vals);
                        console.log(temp_max);

                        $('#temp1_max').text(temp_max + ' ºC');

                        console.log(temp_vals);
                    });
                }
            );

1 Answer 1

1

Use parseInt

var temp_vals = parseInt( this.field1 );

Edit:

$.getJSON('http://api.thingspeak.com/channels/'+channel+'/field/1.json?callback=?',
                {key: read_API_key, days: "1"},
                function(data) {
                 var temp_vals = [];
                    $.each(data.feeds, function() {
                        temp_vals.push( parseInt( this.field1 ) );
                        var temp_vals_date = this.created_at;
                    });
                     var temp_max = Math.max.apply(Math, temp_vals);
                     console.log(temp_max);
                     console.log(temp_vals);
                     $('#temp1_max').text(temp_max + ' ºC');
                }
            );
Sign up to request clarification or add additional context in comments.

5 Comments

I still have the same problem when do the math function...I think the problem is not with the int
Maybe because var temp_vals is redefining the variable for every data.feeds entry. Define it outside AJAX request and then push the result into it like: temp_vals.push( parseInt( this.field1 ) );
It says: Cannot read property 'push' of undefined, and the log does not show anything. So I do not know if I am receiving some value. I am going to check again to solve this.
Without using push it works and show all the values inside the console log, but when I tried to do the math function it doesn't work...
I have edited the answer with some code changes. Should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.