This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Openweather api
#1
Openweather api
Hello
I am implementing the Openweather API in a wiser for KNX.
For now the current time I get it perfectly and I can save the data in virtual objects.
This is the script.
Code:
require('json')
http = require('socket.http')
escape = require('socket.url').escape

key = 'xxxxxxxxxxxxxxxxxxx'
location = 'city'

url = 'http://api.weatherstack.com/current?access_key=%s&query=%s&lang=es'
url = string.format(url, key, escape(location))

res = http.request(url)

data = json.pdecode(res)
if type(data) ~= 'table' then
  alert('failed to load weather data')
  return
end

if data.error then
  log('error', data.error)
  return
end

current = data.current
log(current)

-- temperature
grp.write('32/2/10', current.temperature)

-- Dirección del viento
grp.write('32/2/11', current.wind_dir)

-- Velocidad del viento
grp.write('32/2/12', current.wind_speed)

-- Humedad
grp.write('32/2/13', current.humidity)

-- Presión
grp.write('32/2/14', current.pressure)

-- Descripción
grp.write('32/2/15', current.weather_descriptions)

-- ¿Día/Noche?
grp.write('32/2/16',current.is_day)

-- ¿Índice UV?
if current.uv_index <2  then
 
grp.write('32/2/17','Bajo')
 
elseif current.uv_index == 3  then
 
  grp.write('32/2/17','Normal')
 
  elseif current.uv_index == 4  then
 
  grp.write('32/2/17','Normal')
 
  elseif current.uv_index == 5  then
 
  grp.write('32/2/17','Normal')
 
  elseif current.uv_index == 6  then
 
  grp.write('32/2/17','Alto')
 
  elseif current.uv_index == 7  then
 
  grp.write('32/2/17','Alto')
 
  elseif current.uv_index == 8  then
 
  grp.write('32/2/17','Muy Alto')
 
  elseif current.uv_index == 9  then
 
  grp.write('32/2/17','Muy Alto')
 
  elseif current.uv_index == 10  then
 
  grp.write('32/2/17','Muy Alto')
 
  elseif current.uv_index >11  then
 
  grp.write('32/2/17','Extremadamente Alto')

end
This is the result on the current variable
Code:
weather 20.07.2020 13:00:38
* table:
["cloudcover"]
  * number: 0
["visibility"]
  * number: 10
["observation_time"]
  * string: 10:53 AM
["uv_index"]
  * number: 7
["wind_dir"]
  * string: SE
["humidity"]
  * number: 59
["weather_descriptions"]
  * table:
   [1]
    * string: Sunny
["wind_speed"]
  * number: 17
["weather_code"]
  * number: 113
["is_day"]
  * string: yes
["pressure"]
  * number: 1014
["precip"]
  * number: 0
["weather_icons"]
  * table:
   [1]
    * string: https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png
["feelslike"]
  * number: 32
["temperature"]
  * number: 30
["wind_degree"]
  * number: 130

The problem is with weather_descriptions, I see that it is a table and I assign it to an object like the others, if I log it, the contents of the table appear to me, the object is updated but it does not show anything. It is defined as the 14 byte DPT ASCII string. How do I get that data from that table inside another table and assign it to the object?

Also I would like to know if it is possible to use in a conditional loop if a range of numbers, for example that it only be executed if it is between 8 and 10. Since I don't know how to do it I have evaluated each value independently at the end of the script. I do this so that the UV index is displayed in text instead of a number.
a greeting

Thank you very much
Reply
#2
1. Use current.weather_descriptions[1]
2. You can use Custom Values for object value for easy conversion of number to text
Reply
#3
(20.07.2020, 11:16)admin Wrote: 1. Use current.weather_descriptions[1]
2. You can use Custom Values for object value for easy conversion of number to text
Thanks Admin for such a prompt response.
Effectively with [] it works. What's more, I had tried it but with a point before and that's why it didn't work for me.
What you tell me about custom values I don't know how to do.
Thank you very much
a greeting
Reply
#4
Custom values:
   
Reply
#5
Hello
Thanks for the information, I had seen the icon but I did not understand what it was for.
Thank you very much.
a greeting
Reply
#6
Hello
I keep polishing my script for Openweather, now with the direction of the wind.

If I have a value from 0 to 360 degrees of the wind direction, but I want to transform it into a character string according to its denomination of the winds in the Mediterranean:

• North or Terral wind (N): from 337.5 ° to 22.5 °
• Northeast wind (NE): 22.5 ° to 67.6 °
• East or Levante wind (E): from 67.5 ° to 112.5 °
• Southeast wind (SE): from 112.5 ° to 157.5 °
• South wind (S): from 157.5 ° to 202.5 °
• Southwest wind (SW): 202.5 ° to 247.5 °
• West or west wind (W): from 247.5 ° to 292.5 °
• Northwest wind (NW): 292.5 ° to 337.5 °

With the icons I have no problem adding new icons with the addresses and ranges, as if it were a dimmer or a blind.

I know that if I make icons instead of arrows with the text I could do it, but it doesn't seem like a very elegant solution.

And using a custom gauge like the compass rose?

Any suggestions on how to do it?
a greeting
Reply
#7
This might be helpful: https://forum.logicmachine.net/showthread.php?tid=1831
Reply
#8
Hello
Thanks for the answer but I don't understand it well, I have used it to turn a button and it has not worked for me, could I develop it in more concrete steps?
Thank you
Reply
#9
This example will set rotate the icon according to object value (data type 5.003 - angle).
Set Additional classes of your object to rotate
Add to Custom JavaScript:
Code:
$(function(){
  $('.rotate').each(function(i, el) {
    var $el = $(el), addr = $el.data('object'); // or 'status-object'

    grp.listen(addr, function(obj) {
      $el.find('.icon').css('transform', 'rotate(' + obj.value + 'deg');
    });
  });
});
Note that this won't work in the editor, only in the visualization view.
Reply
#10
Thanks for responding so quickly

In object, inside the quotes, I have to put the object that contains the rotation value, in my case 32/2/13 which is the direction of the wind. It is right?

Then as I configure in the visualization, that I select an object or an image and I put it in additional classes rotare?

a greeting
Reply
#11
You don't need to edit Custom JS at all. Just set the Additional classes property and the script will find the attached object automatically.
Reply
#12
I have the visualization open to see the effect.

I copy your latest code into custom JS and save it.

In the visualization I go to any button (according to the image) and in Additional classes I write rotate and save.

I update the script that gets the wind direction.

The button does not rotate.

Is it possible to do it with this control or should it be of another type?
[Image: JV5xhrz.png]
Reply
#13
Make sure the object 0/0/1 is (data type 5.003 - angle). and open the final visu not the editor to test
------------------------------
Ctrl+F5
Reply
#14
Of course, it is logical that it is with the wind direction object and not with the object that I show in the image. My mistake.

Now it works perfectly.

Thank you very much.

With the icon already solved, but how could it be done for the names:

• North or Terral wind (N): from 337.5 ° to 22.5 °
• Northeast wind (NE): 22.5 ° to 67.6 °
• East or Levante wind (E): from 67.5 ° to 112.5 °
• Southeast wind (SE): from 112.5 ° to 157.5 °
• South wind (S): from 157.5 ° to 202.5 °
• Southwest wind (SW): 202.5 ° to 247.5 °
• West or west wind (W): from 247.5 ° to 292.5 °
• Northwest wind (NW): 292.5 ° to 337.5 °
Let each value range display the appropriate text on the display.

Thank you
Reply
#15
(02.09.2020, 11:09)admin Wrote: This example will set rotate the icon according to object value (data type 5.003 - angle).
Set Additional classes of your object to rotate
Add to Custom JavaScript:
Code:
$(function(){
  $('.rotate').each(function(i, el) {
    var $el = $(el), addr = $el.data('object'); // or 'status-object'

    grp.listen(addr, function(obj) {
      $el.find('.icon').css('transform', 'rotate(' + obj.value + 'deg');
    });
  });
});
Note that this won't work in the editor, only in the visualization view.
Can I modify this function to rotate a fan icon, that keeps rotating in a sertain speed if value´s not equal to 0?
Reply
#16
It's easier to use an icon with animation. See this: https://forum.logicmachine.net/showthrea...132#pid132
Reply
#17
(15.11.2020, 09:05)admin Wrote: It's easier to use an icon with animation. See this: https://forum.logicmachine.net/showthrea...132#pid132
Yes I found those too. They could work, but in this case those icons doesn´t match the rest of it. 
So had hoped to start a rotation of the existing icon that I have.
Reply
#18
Add to Custom CSS:
Code:
.spin-animation {
  animation: spin 2s infinite linear;
}

@keyframes spin {
  from {
    transform:rotate(0deg);
  }
  to {
    transform:rotate(360deg);
  }
}

Add to Custom JS, change additional class from rotate to spin:
Code:
$(function(){
  $('.spin').each(function(i, el) {
    var $el = $(el), addr = $el.data('object'); // or 'status-object'

    grp.listen(addr, function(obj) {
      $el.find('.icon').toggleClass('spin-animation', obj.value > 0);
    });
  });
});
Reply
#19
(16.11.2020, 07:40)admin Wrote: Add to Custom CSS:
Code:
.spin-animation {
  animation: spin 2s infinite linear;
}

@keyframes spin {
  from {
    transform:rotate(0deg);
  }
  to {
    transform:rotate(360deg);
  }
}

Add to Custom JS, change additional class from rotate to spin:
Code:
$(function(){
  $('.spin').each(function(i, el) {
    var $el = $(el), addr = $el.data('object'); // or 'status-object'

    grp.listen(addr, function(obj) {
      $el.find('.icon').toggleClass('spin-animation', obj.value > 0);
    });
  });
});
Nice, thanks Admin
Reply
#20
I had the wiser for KNX disconnected for quite a long time and have put it back into operation.
When I have run the openweather script to check if it is still valid, I happen to get this error in the registry.

Code:
User script: 35: attempt to index global 'Time_table' (a nil value) stack traceback: User script: 35: in main chunk.


The point is that the data is obtained correctly and if I run it other times the error does not repeat itself.

It is a problem with the declaration of the variable Tabla_tiempo, but what is the problem?

A greeting.
Reply


Forum Jump: