Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

How to handle nil value from http request


Question

Posted

I am setting up some controls for my chromecast audios with a webserver on a raspberry Pi, I am fetching the values like volume and whats playing, but when there's nothing playing on the chromecast I get a nil value and the scene stops, how can I handle these nil values? 

 

could I use something like if ~ = nil ?

 

 

 

local http = net. HTTPClient () 
http : request ( "http://xxx/getDeviceStatus?address=xxx",  {
    options =  {
        method =  "PUT",
        },
    success = function(status)
       

       response = status.data        
    svar = json.decode(response)

      --printer verdier
       
      print ("Volum: "..svar.status.volume.level);
      print ("Muted: "..tostring(svar.status.volume.muted));
      print ("Spiller fra: "..tostring(svar.status.applications[1].displayName));
      print ("Media: "..tostring(svar.status.applications[1].statusText));
      print ("sessioId: "..tostring(svar.status.applications[1].sessionId));

     fibaro:setGlobal('volumbad',svar.status.volume.level); 
     fibaro:setGlobal('spillerbad',tostring(svar.status.applications[1].displayName));
     fibaro:setGlobal('idbad',svar.status.applications[1].sessionId);
      
      end
    
   })

 

 

Debug: 

 

 

Please login or register to see this attachment.

Please login or register to see this attachment.

4 answers to this question

Recommended Posts

  • 0
Posted

Yes, I think so.

if (svar.status ~=nil) then ... end

  • 0
Posted

@mortesan

 

You could check just after the response read for a part of the json that you would expect to be there

 

Here is a an example I use..

 

Please login or register to see this code.

for the else you could use 

return - if its part of a function  or fibaro:abort() - which will stop that iteration of the main loop/button..

 

 

  • 0
  • Inquirer
  • Posted (edited)

    Thank you for some good advise, got it working with if == nil then ..  else ...

     

    if svar.status.applications == nil then
            print ("Volum bad: "..svar.status.volume.level);
            print ("Muted bad: "..tostring(svar.status.volume.muted));
            fibaro:setGlobal('volumbad',svar.status.volume.level);
            fibaro:setGlobal('spillerbad',"Nothing is playing");
            
            else 
            print ("Volum bad: "..svar.status.volume.level);
             print ("Muted bad: "..tostring(svar.status.volume.muted));
            print ("Spiller fra: "..tostring(svar.status.applications[1].displayName));
            print ("Media: "..tostring(svar.status.applications[1].statusText));
            print ("sessioId: "..tostring(svar.status.applications[1].sessionId));
            fibaro:setGlobal('volumbad',svar.status.volume.level);
            fibaro:setGlobal('spillerbad',tostring(svar.status.applications[1].displayName));
            fibaro:setGlobal('idbad',svar.status.applications[1].sessionId);
            
          end;

     

     

     

    Please login or register to see this attachment.

    Edited by mortesan

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...