SHAC Weather Coding Problem

Discussion in 'C-Bus Automation Controllers' started by julscam03, Aug 27, 2020.

  1. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    I have been trying to create a weather widget on the SHAC using a scrpting program that gets data from a weather api and outputs it into a User Parameter. The code so far is really simple it looks like this:

    Code:
    http = require("socket.http")
    json = require("json")
    
    weather = http.request("https://api.openweathermap.org/data/2.5/weather?q=melbourne,au&appid=*****&units=metric")
    
    response = json.decode(weather)
    
    temp = response.main.temp
    
    SetUserParam('WIRED', 'test', temp)
    Obviously "*****" is where I put my api key.

    I have tried to run this code directly off of windows where it prints the temp and it works fine. However, when trying to run it on the SHAC it comes up with the error:

    Resident script:6: bad argument #1 to 'decode' (string expected, got nil)

    It looks like its not finding a string from the request?
    Any ideas?
     
    julscam03, Aug 27, 2020
    #1
  2. julscam03

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,420
    Likes Received:
    62
    Location:
    Adelaide
    You can use the log() function to print what you actually get back for "weather".. might shed some light.
     
    NickD, Aug 28, 2020
    #2
  3. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    Thanks for the tip. It's definetaly not grabbing anything from the website I just can't seem to figure out why.
     
    julscam03, Aug 28, 2020
    #3
  4. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    The variable weather needs to be a valid json string to be decoded that is why you have an error.
    most likely the http.request is failing for some reason and returning an empty value,
    what dose the below return?
    Code:
    http = require("socket.http")
    json = require("json")
    
    weather, err = http.request("https://api.openweathermap.org/data/2.5/weather?q=melbourne,au&appid=*****&units=metric")
    log( weather, err)
    
    
     
    Pie Boy, Aug 28, 2020
    #4
  5. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    It comes up with this error:
    * arg: 1
    * nil
    * arg: 2
    * string: Try again
     
    julscam03, Aug 28, 2020
    #5
  6. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    Var weather is nil and err is returning an error.
    Try the below,

    Code:
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    require('json')
    
    sink ={}
    
    apikey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
    res, err = http.request({
        url ="https://api.openweathermap.org/data/2.5/weather?q=melbourne,au&appid="..apikey.."&units=metric",
         method = 'GET',
        sink = ltn12.sink.table(sink)
    })
    
    log(res, err, sink)
    log(json.decode(sink[1]))
    
      
     
    Pie Boy, Sep 2, 2020
    #6
  7. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    Thankyou very much for all your help. It comes up with this:

    * arg: 1
    * nil
    * arg: 2
    * string: Try again
    * arg: 3
    * table:
     
    julscam03, Sep 2, 2020
    #7
  8. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    What sort of script are you running this in?
    And did you replace the xxxxx with your api key?
     
    Pie Boy, Sep 2, 2020
    #8
  9. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    I did replace with my apikey and I'm running this as a resident script.
     
    julscam03, Sep 2, 2020
    #9
  10. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    I think the resident script is the problem,
    As it will keep running the command and never return the data, this needs to just run once, then wait for response.
    What is the delay set to in your resident script,
    Try setting that to 60 sec
     
    Pie Boy, Sep 2, 2020
    #10
  11. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    Ye it is already set to 60 seconds. What other kind of script could I use to test it?
     
    julscam03, Sep 2, 2020
    #11
  12. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    Tr
    Try putting

    script.disable(_SCRIPTNAME)

    after log() etc, this will disable the script after it has run etc, not a long term solution but it will test proof of concept
     
    Pie Boy, Sep 2, 2020
    #12
  13. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    It comes up with the same error but its also not even disabling the script.
    The scripts name is Weather I tried
    script.disable('Weather')
    and nothing happened.

    Ive tried logging somthing after the log() and its not running that either. Seems like its not running any code after log(json.decode(sink[1]))
     
    Last edited: Sep 2, 2020
    julscam03, Sep 2, 2020
    #13
  14. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    Ma
    Ok, are there any error messages in the error tab?
    Try manually stopping the script, then replace
    json.decode(Sink[1]) etc With log(sink[1])

    Then restart the script
     
    Pie Boy, Sep 2, 2020
    #14
  15. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    The error that comes up is
    Resident script:17: bad argument #1 to 'decode' (string expected, got nil)
    stack traceback:
    [C]: in function 'decode'

    When I replace the json.decode line it runs the script.disable but logs 'nil'
     
    julscam03, Sep 2, 2020
    #15
  16. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    I think the forum is removing some char when it posts the code... when i go to edit it, it is correct but when it displays it on the post it is wrong... Someone needs to fix this.... if i go to edit and change then save it looks like it corrects it so it may work as below but ill pm you the below code anyway

    This works for me in a resident script with 60 sec delay (with my own API key), but i would stick it in a scheduled script (and remove the script.disable(_SCRIPTNAME)) to run every 10 min as the server side updates in 10 min intervals (according to the open weather documentation)
    cron format for every 10 min would be minute : * Hour: */10 Day:*

    Code:
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    require('json')
    
    sink ={}
    
    apikey = "d72f8e66c9974ee"
    
    res, err = http.request({
        url ="https://api.openweathermap.org/data/2.5/weather?q=melbourne,au&appid="..apikey.."&units=metric",
         method = 'GET',
        sink = ltn12.sink.table(sink)
    })
    
    --log(res, err, sink)
    data = json.pdecode(sink[1])
    
    if data then
    log(data)
    
      log(data.name, 'temp = ' .. data.main.temp.. ' °C')
      SetUserParameter('Local', "Temp", data.main.temp)
    
    end
    
    script.disable(_SCRIPTNAME)
    
    
     
    Last edited: Sep 3, 2020
    Pie Boy, Sep 3, 2020
    #16
  17. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0

    I copied your code directly from your pm into both a resident and scheduled with your apikey and everything. The script is disabling itself so its running all the code but it comes up with no logs and no errors.

    Is it possible that I have missed something fundamental in the setup of the SHAC that would cause the scripting to not work properly?
     
    julscam03, Sep 3, 2020
    #17
  18. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    run me through what you have connected as such ethernet, 24v dc etc and how have you configured the ethernet dhcp or static ip?
    Also my api key above is not complete i removed some char etc so you will need to replace that with your api key.
     
    Pie Boy, Sep 3, 2020
    #18
  19. julscam03

    julscam03

    Joined:
    Aug 27, 2020
    Messages:
    14
    Likes Received:
    0
    Connected to the shac I have ethernet, 24v and cbus connection. The ip is static.

    I am able to create frames in visualisation that show webpages so I dont think the internet is the problem.
     
    Last edited: Sep 5, 2020
    julscam03, Sep 5, 2020
    #19
  20. julscam03

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    Pie Boy, Sep 6, 2020
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.