Weather widget

Discussion in 'C-Bus Automation Controllers' started by Nobes, Jun 25, 2018.

  1. Nobes

    Nobes

    Joined:
    Oct 7, 2004
    Messages:
    164
    Likes Received:
    1
    Location:
    Hobart
    Has anyone experimented with things like weather widgets, or something as simple as displaying time and date. Soo many things that were easy on the old CTC's that just aren't straight forward anymore.

    Had a search through the documentation but haven't seen anything obvious yet.
     
    Nobes, Jun 25, 2018
    #1
  2. Nobes

    MadMal

    Joined:
    Dec 16, 2009
    Messages:
    75
    Likes Received:
    1
    Location:
    Perth, WA
    Thought I would just put this here in case any one else was reading this ...

    I used the following to get time to display ..

    Object, Create User Parameter in App 250, Address 0, Called "Time"

    Scripting, Create new Resident Script with interval 60 seconds.

    Script has the code:
    hour = os.date('%I')
    min = os.date('%M')
    ampm = os.date('%p')
    SetUserParam(0, 0, hour..':'..min..' '..ampm)
    Add new object to a visualisation connected to 0/250/0

    Feel free to tell me there is a better way :)
     
    MadMal, Aug 9, 2018
    #2
  3. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    Hey there, I assume the rest of your code is something like
    hour = os.date('%I', os.time())
    etc.

    My issue is that I don't appear to be able to update the user parameters.

    I created a user param of type Time/Day, and have assigned the value to that param.

    I can write the correct value to the log however cannot update the user param.

    Any prod to something I may be doing wrong?

    Cheers,
    Paul
     
    pspeirs, Apr 20, 2019
    #3
  4. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    Whoops, should have been
    hour = os.date('%I', os.time())
     
    pspeirs, Apr 21, 2019
    #4
  5. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    So I created two user params for time string and date string.

    Run a script once a second

    Code:
    local TimeString = os.date( "%H:%M:%S", Timestamp )
    local DateString = os.date( "%A, %d %B %Y", Timestamp )
    
    SetUserParam('Local Network', 'timestr', TimeString)
    SetUserParam('Local Network', 'datestr', DateString)
    Place them somewhere on your page, and you'll have time and date displayed.
     
    pspeirs, Apr 23, 2019
    #5
  6. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    Create a free API account on Willy Weather, I believe you can hit it a lot of times before you start getting charges. Check out their API docs for the different functions available.

    Create some user params for weather_min, weather_max, weather_icon, weather_precis, precisCode

    Upload some icons for sunny, cloudy, etc. Add the user params item to the page somewhere and add a bunch of icons for values 1, 2, 3, 4, etc for the total number of precisCode values. This is trial and error as I haven't found a complete listing of text.

    Code:
    
    local http = require("socket.http")
    local json = require "json"
    
    --local body, code, headers, status = http.request("https://api.willyweather.com.au/{API_KEY}/locations/1215/weather.json?forecasts=weather&days=1&startDate=2019-04-23")
    local body, code, headers, status = http.request("https://api.willyweather.com.au/{API_KEY}/locations/2370/weather.json?forecasts=weather&days=3&startDate=2019-04-23")
    
    log(code, status, body, #body)
    
    local decode = json.decode(body)
    
    --log(decode.location.name)
    
    local entry = decode.forecasts.weather.days[1].entries[1]
    
    log(entry.precis)
    
    -- degrees characters don't work :(
    local minimum = string.format("%s %sC", entry.min, string.char(176))
    local maximum = string.format("%s %sC", entry.max, string.char(176))
    local precisCode = entry.precisCode
    local precis = entry.precis
    
    -- Set value of user parameter 'now_temp' on local network to 12.1
    SetUserParam('Local Network', 'weather_min', minimum)
    SetUserParam('Local Network', 'weather_max', maximum)
    SetUserParam('Local Network', 'weather_precis', precis)
    
    if (precisCode == "chance-shower-fine") then
      SetUserParam('Local Network', 'weather_icon', 4)
    elseif (precisCode == "fine") then
      SetUserParam('Local Network', 'weather_icon', 1)
    elseif (precisCode == "mostly-fine") then
      SetUserParam('Local Network', 'weather_icon', 5)
    else
      SetUserParam('Local Network', 'weather_icon', 0)
    end
    
    Think that covers it, you should end up with a display like . . .

    upload_2019-4-23_21-24-26.png

    Call a few more API methods and you can add more cool stuff.
     
    pspeirs, Apr 23, 2019
    #6
    Damaxx likes this.
  7. Nobes

    Hollywoodperth

    Joined:
    Sep 9, 2018
    Messages:
    38
    Likes Received:
    0
    OK, I can't get this to work... what am I missing?

    I've set up the above as a resident script, delay 1 sec.

    I've defined two tags (tag map) under User Parameter (250), 'timestr' and 'datestr'

    I've added two objects 0/250/0 and 0/250/1 (timestr and datestr respectively) but the values are 0:00:00 and 00.00.2000
     
    Hollywoodperth, May 27, 2019
    #7
  8. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    have you written out to the log as an easy way of just verifying that the time prints. Check the name of your local network, might just be "Local". The user patrameters should be strings, the result above doesn't look like they are.
     
    pspeirs, May 28, 2019
    #8
  9. Nobes

    Hollywoodperth

    Joined:
    Sep 9, 2018
    Messages:
    38
    Likes Received:
    0
    Thanks, that fixed it.

    Is it possible to a run script - for example weather - only when a visualisation page is opened to prevent hitting the API too often?
     
    Hollywoodperth, May 29, 2019
    #9
  10. Nobes

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    No idea, probably not. Its a whole different interface concept to the CTC's. Run it once every 30 min or so, I mean, how often does the weather need to be updated. The time, you can run every second and update the user value.
     
    pspeirs, Jun 12, 2019
    #10
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.