OpenUV.io plugin max. 50 request/day Topic is solved

Moderator: leecollings

User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

OpenUV.io plugin max. 50 request/day

Post by EdwinK »

Using Xofor's script to get data from the OpenUV website.

It seems we now can only get about 50 calls a day to the OpenUV api, but I believe the script is calling it more times. Getting now the following in the log: '2020-04-29 22:25:35.557 Error: (UV Index) Daily API quota exceeded. Add billing details to get 15000 reqs/day or contact [email protected] to upgrade to Unlimited Plan.'.

Is there a way to change the script that it doesn't call in too often?
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi,

Upgrade the script in your Domoticz.
Xorfor has modified the script approx 15 month ago.

Regards
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

I pulled it from his github page yesterday.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi,

@EdwinK,

By co-incidence, I installed this plugin yesterday evening. I saw that the poll frequency has changed from once per 3 minutes to (24*60/50). If it would still be 3 minutes, then I would exceed the 50 limit in 2.5 hours.

So far no error messages. Let's wait.

Regards
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

It could be that they counted my visits to the openuv site to the total requests.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi,

If you collect data at OpenUV, they will count it.

Regards
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi

@EdwinK,

I discovered the same issue. See below:
2020-05-01 01:25:56.648 Error: (Open UV) Daily API quota exceeded. Add billing details to get 15000 reqs/day or contact [email protected] to upgrade to Unlimited Plan.
2020-05-01 01:53:57.527 Error: (Open UV) Daily API quota exceeded. Add billing details to get 15000 reqs/day or contact [email protected] to upgrade to Unlimited Plan.
As @xorfor has changed the plugin, in order to match the new limit of 50 requests/day, I believe this is on the limit and sometimes it may exceed that limit. The poll frequency is calculated from (24 * 60 / 50), which equals to 28.8. int (28.8) equals to 28 and that is to much.

See: https://github.com/Xorfor/Domoticz-Open ... 6e555cecfa

I changed this value to 30 (like 3 before). This means a maximum of 48 requests/day.

Let's wait for another week.

Regards
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

Adjusted the script to 30 minutes, and now waiting to see what happens.

edit: and i guess I did something wrong:

error: (UV Index) Module Import failed, exception: 'TabError'
2020-05-02 09:48:56.157 Error: (UV Index) Import detail: File: /home/pi/domoticz/plugins/Domoticz-OpenUV-Plugin/plugin.py, Line: 26, offset: 16
2020-05-02 09:48:56.157 Error: (UV Index) Error Line ' __MINUTES = 30
2020-05-02 09:48:56.157 '
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi
@EdwinK

Since my last post, about 1.5 weeks has been passed, and I got the message one more time.
Error: (Open UV) Daily API quota exceeded. Add billing details to get 15000 reqs/day or contact [email protected] to upgrade to Unlimited Plan.
However it happened at about 1:30h in the night. Strange. As you see in my previous post the time was also in the night 1:25h and 1:53h.
In contrary what I thought, the polling stops after sunset and starts again after sunrise. You can see that in the log and that make sense.

Conclusion: changing the polling frequency to 30 minutes is not the solution. So I think the message is not produced, because the polling has exceeded 50 polls, but is just an incident.
Therefore I will not make a PR to change it.

In the meantime I removed the script from XorFor, as I started thinking about the usefulness of this script.

The script uses the co-ordinates of Domoticz, which are mostly the same of your home address. This is not so useful, unless you are always at home or you want to use the information to warn your painter, as more UV requires more painting.

I wanted to use the location of my cellphone, because that is mostly the same, where I am and to much UV radiation is more dangerous for me (and other humans) than for my house.

Because I use "Owntracks" for localization, which sends the data to my own MQTT server and with Node Red to Domoticz, where it is used as geofencing, I decided to make it more dynamic in Node Red.

I use now:
1. The lat/lon co-ordinates are dynamic and the day length (poll time between sunrise and sunset) are accordingly. So on the North pole we have sometimes 24 h daytime.
2. Poll frequency fixed to 30 minutes, so on the North pole maximum of 48 polls.
In The Netherlands maximum 32-33 polls.
3. The dynamic lat/lon co-ordinates are used in the request to OpenUV, with back-up of the co-ordinates at home (equals Domoticz).
4. Altitude is taken into account. Up in the mountains, we have higher UV
5. Maximum safe exposure time has been added

If someone is interested, let me know, I will publish the Node Red flow.

Screenshot_OpenUV.png
Screenshot_OpenUV.png (388.81 KiB) Viewed 2878 times

Best regards
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

I was using OpenUV mostly for raising and lowering the sunscreen when the UV exceeds a certain level. I guess I either need to live with the error or find another solution for the UV
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

I've got MQTT working now (was using the wrong port. Needed to be 1883, but used 1880), and could use the test thingy. Now I would like to use your flow to get the UVinfo.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi @EdwinK,

Did you get more reports of "Daily API quota exceeded", since 1.5 weeks.

I will publish the flow later today.

Regards
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hi @EdwinK,

As promised hereby the flow for OpenUV.

Screenshot_Node-Red_OpenUV.png
Screenshot_Node-Red_OpenUV.png (51.79 KiB) Viewed 2847 times

For your application (control of your sunscreens) you can simply remove the following nodes:

1. MQTT-Owntracks
2. Set Lat/Lon ( the Lat/Lon co-ordinates can be set in the node "Prepare URL OpenUV", if you modify the contents a little.
3. Sun rise/set switch
4. Control Gate
5. 30 Minutes Request
6. Gate

So the complete lower part can be removed

In order to get a 30 Minutes tick to make a request during daylight, you can use a "day time switch" (node-red-contrib-time-switch),
superseded by an "Inject" node with a 30 Minute tick, like:

Screenshot_Node-Red inject.png
Screenshot_Node-Red inject.png (6.69 KiB) Viewed 2847 times
Connect the upper output to the input of the "Function" node, called "Prepare URL OpenUV".

The contents of the function node has to be changed to:

Code: Select all

var url = "https://api.openuv.io/api/v1/uv";
var lat = "<your lat>";
var lng = "<your lon>";
var alt = "<your altitude>";
msg.url = url + "?lat=" + lat + "&lng=" + lng + "&alt=" + alt;
msg.headers = {};
msg.headers["x-access-token"] = "your OpenUV access token";

return msg;
In line 30 of the "Function"Node, called "To Domoticz" you might want to change the skin type.
I have inserted skin type 3, but you can change it to another type ( See the comment node).

Please find the complete flow below:

Code: Select all

[{"id":"9e770b5e.116dd8","type":"tab","label":"Open UV","disabled":false,"info":""},{"id":"5e4af9cf.9cae08","type":"function","z":"9e770b5e.116dd8","name":"Prepare URL OpenUV","func":"var url = \"https://api.openuv.io/api/v1/uv\";\nvar lat = msg.payload.lat || \"xx.xxxxxx\";\nvar lng = msg.payload.lon || \"y.yyyyy\";\nvar alt = msg.payload.alt || \"zz\";\nmsg.url = url + \"?lat=\" + lat + \"&lng=\" + lng + \"&alt=\" + alt;\nmsg.headers = {};\nmsg.headers[\"x-access-token\"] = \"8fd2e165a2af72db7c19acba67e82fd3\";\n\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":100,"wires":[["5b77d748.78fcc"]]},{"id":"5b77d748.78fcc","type":"http request","z":"9e770b5e.116dd8","name":"OpenUV http request","method":"GET","ret":"obj","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":420,"y":100,"wires":[["71303228.bcbd0c","37c920a1.f16b28"]]},{"id":"71303228.bcbd0c","type":"debug","z":"9e770b5e.116dd8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":160,"wires":[]},{"id":"a90a0f1a.412448","type":"mqtt in","z":"9e770b5e.116dd8","name":"MQTT-Owntracks","topic":"owntracks/name/eq","qos":"2","datatype":"json","broker":"8591549f.77809","x":160,"y":240,"wires":[["f1b9f0ba.2ca598","dae8628.8d105a"]]},{"id":"316645d2.62236a","type":"sunriseinput","z":"9e770b5e.116dd8","name":"Sun rise/set switch","lat":"xx.xxxxxx","lon":"y.yyyyyy,"start":"sunriseEnd","end":"sunsetStart","x":410,"y":320,"wires":[["64b4ef54.aefef"],[]]},{"id":"5330841a.345f84","type":"gate","z":"9e770b5e.116dd8","name":"Gate","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":630,"y":240,"wires":[["5e4af9cf.9cae08"]]},{"id":"f1b9f0ba.2ca598","type":"function","z":"9e770b5e.116dd8","name":"Set Lat/Lon","func":"msg = {\n    lat : msg.payload.lat || \"xx.xxxxxx\",\n    lon : msg.payload.lon || \"y.yyyyyy\"\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":190,"y":320,"wires":[["316645d2.62236a"]]},{"id":"64b4ef54.aefef","type":"function","z":"9e770b5e.116dd8","name":"Control Gate","func":"if (msg.topic == \"sun\") {\n    if (msg.payload == 1) {\n    msg.topic = \"control\";\n    msg.payload = \"open\"\n    }\n    else {\n    msg.topic = \"control\";\n    msg.payload = \"close\";\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":320,"wires":[["5330841a.345f84"]]},{"id":"dae8628.8d105a","type":"throttle","z":"9e770b5e.116dd8","name":"30 Minutes Request","throttleType":"time","timeLimit":"30","timeLimitType":"minutes","countLimit":0,"blockSize":0,"locked":false,"x":420,"y":240,"wires":[["5330841a.345f84"]]},{"id":"89347627.7f4218","type":"function","z":"9e770b5e.116dd8","name":"To Domoticz","func":"// Declarations\n\nvar msg1 ={}; // UV sensor value\nvar msg2 ={}; // UV alert\nvar msg3 ={}; // Max UV\nvar msg4 ={}; // Ozone\nvar msg5 ={}; // Max. Exposure Time\n\n// Function to covert UV index to level\n\nfunction uvi2level(x) {\n\nif (x < 3) {\n  return 0;\n} else if (x < 6) {\n  return 1;\n} else if (x < 8 ) {\n  return 2;\n} else if (x < 11 ) {\n  return 3;  \n} else {\n  return 4;\n}\n}\n\nmsg1.payload = {\"command\":\"udevice\",\"idx\":397,\"nvalue\":0,\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \";0\"};\nmsg2.payload = {\"command\":\"udevice\",\"idx\":398,\"nvalue\":uvi2level(msg.payload.result.uv),\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \" UVI\"};\nmsg3.payload = {\"command\":\"udevice\",\"idx\":399,\"nvalue\":uvi2level(msg.payload.result.uv_max),\"svalue\":msg.payload.result.uv_max.toFixed(1).toString() + \" UVI\" + \"\\n\" + msg.payload.result.uv_max_time};\nmsg4.payload = {\"command\":\"udevice\",\"idx\":400,\"nvalue\":0,\"svalue\":msg.payload.result.ozone.toString()};\nmsg5.payload = {\"command\":\"udevice\",\"idx\":401,\"nvalue\":0,\"svalue\":msg.payload.result.safe_exposure_time.st3.toString()};\nreturn [[msg1,msg2,msg3,msg4,msg5]];\n","outputs":1,"noerr":0,"x":910,"y":100,"wires":[["abf23830.25a0e"]]},{"id":"abf23830.25a0e","type":"mqtt out","z":"9e770b5e.116dd8","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"f9f13036.e28b58","x":1110,"y":100,"wires":[]},{"id":"37c920a1.f16b28","type":"moment","z":"9e770b5e.116dd8","name":"","topic":"","input":"payload.result.uv_max_time","inputType":"msg","inTz":"Europe/Amsterdam","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD-MM-YYYY HH:mm:ss","locale":"en_US","output":"payload.result.uv_max_time","outputType":"msg","outTz":"Europe/Amsterdam","x":680,"y":100,"wires":[["89347627.7f4218"]]},{"id":"b8e9b258.69a9","type":"comment","z":"9e770b5e.116dd8","name":"Fitzpatrick Skin Types","info":"See: https://www.openuv.io/kb/skin-types-safe-exposure-time-calculation\n\n1. Very fair skin, white; red or blond hair; light-colored eyes; freckles likely\n2. Fair skin, white; light eyes; light hair\n3. Fair skin, cream white; any eye or hair color (very common skin type)\n4. Olive skin, typical Mediterranean Caucasian skin; dark brown hair; medium to heavy pigmentation\n5. Brown skin, typical Middle Eastern skin; dark hair; rarely sun sensitive\n6. Black skin; rarely sun sensitive","x":920,"y":60,"wires":[]},{"id":"8a6230df.26add","type":"function","z":"9e770b5e.116dd8","name":"Prepare URL OpenUV","func":"var url = \"https://api.openuv.io/api/v1/uv\";\nvar lat = msg.payload.lat || \"52.179281\";\nvar lng = msg.payload.lon || \"5.606528\";\nvar alt = msg.payload.alt || \"15\";\nmsg.url = url + \"?lat=\" + lat + \"&lng=\" + lng + \"&alt=\" + alt;\nmsg.headers = {};\nmsg.headers[\"x-access-token\"] = \"token\";\n\nreturn msg;","outputs":1,"noerr":0,"x":220,"y":420,"wires":[[]]},{"id":"8591549f.77809","type":"mqtt-broker","z":"","name":"MQTT_Server","broker":"192.168.10.24","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f9f13036.e28b58","type":"mqtt-broker","z":"","name":"RPI1_ MQTT_Broker","broker":"192.168.10.51","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Regards
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

Going to try this in the coming week
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by EdwinK »

Thought I had it working, as it says 'connected' on some parts, but nothing going in to Domoticz
Screen Shot 2020-05-16 at 10.59.55.png
Screen Shot 2020-05-16 at 10.59.55.png (92.88 KiB) Viewed 2818 times
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by Jan Jansen »

FireWizard wrote: Friday 15 May 2020 21:18 As promised hereby the flow for OpenUV.
@ Firewizard,
Thanks for sharing your flow. The flow once stimulated me to study Node-RED and MQTT (domoticz/in and domoticz/out). For this I adjust your flow
Knipsel flow UV.png
Knipsel flow UV.png (33.67 KiB) Viewed 2784 times
First, is UV Alert a switch? If so, at what value does it switch on?

Otherwise the upper part of the flow works. Like I said I also wanted to play with MQTT. So in Domoticz I made a switch "Night" (IDX = 8) which is "ON" when the sun is down. So the gate must be open when IDX8 is "OFF". I did not change the configuration of "your" Gate I have been struggling with the function node but I need some help. Hopefully you are willing to help.

Thanks in advance.
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

Hello,

@Jan Jansen,

You wrote:
I have been struggling with the function node but I need some help. Hopefully you are willing to help.
Of course I willing you to support.

The contents of my "Function" Node is as follows:

Code: Select all

if (msg.topic == "sun") {
    if (msg.payload == 1) {
    msg.topic = "control";
    msg.payload = "open"
    }
    else {
    msg.topic = "control";
    msg.payload = "close";
    }
}
return msg;
To control the Gate you have to send a message with the topic msg.control, according to:
Messages with the topic Control Topic are control messages, which set the state of the gate. Control messages can have values representing open, close, toggle, and default.
In my case, I receive as input a payload of 1, during daytime (and 0, during nighttime)

In your case, you receive from IDX8 an "OFF" during daytime and "ON" during nighttime.

First we have to take care that the function is only activated, if the command is coming from IDX8, so your function should be something like:

Code: Select all

if (msg.payload.idx == 8) {
    if (msg.payload == "OFF") {
    msg.topic = "control";
    msg.payload = "open"
    }
    else {
    msg.topic = "control";
    msg.payload = "close";
    }
}
return msg;
Try this code.
First, is UV Alert a switch?
UV-Alert is not a switch, but an "Alert" sensor and it will change the color, according to the alert level

The different UV levels are 3, 6, 8 and 11.
This is standard, but you can change it in the "Function" Node, called "To Domotcz".

Best regards
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by Jan Jansen »

Hello FireWizard,

Thanks for the detailed explanation. Unfortunately I cannot open the gate. I have tried different settings of the JSON node and alse removed this node. In the control node I changed the line> if (msg.payload == "OFF") to> if (msg.payload === 0).
Hopefully you are willing to take a look at the next flow.

Code: Select all

[{"id":"54563ee4.3724f","type":"tab","label":"Open UV","disabled":false,"info":""},{"id":"41887041.de03a8","type":"function","z":"54563ee4.3724f","name":"Prepare URL OpenUV","func":"var url = \"https://api.openuv.io/api/v1/uv\";\nvar lat = msg.payload.lat || \"51.842739\";\nvar lng = msg.payload.lon || \"5.86347\";\nvar alt = msg.payload.alt || \"10\";\nmsg.url = url + \"?lat=\" + lat + \"&lng=\" + lng + \"&alt=\" + alt;\nmsg.headers = {};\nmsg.headers[\"x-access-token\"] = \"XXXXXXXXXXXXXXXXXXXX\";\n\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":100,"wires":[["6df2e1f8.ba6ec"]]},{"id":"6df2e1f8.ba6ec","type":"http request","z":"54563ee4.3724f","name":"OpenUV http request","method":"GET","ret":"obj","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":420,"y":100,"wires":[["8db20686.56f2d"]]},{"id":"4e6d8a4b.050624","type":"function","z":"54563ee4.3724f","name":"Control Gate","func":"if (msg.payload.idx == 8) {\n    if (msg.payload == \"OFF\") {\n    msg.topic = \"control\";\n    msg.payload = \"open\"\n    }\n    else {\n    msg.topic = \"control\";\n    msg.payload = \"close\";\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":320,"wires":[["42ad811d.5d234"]]},{"id":"3f946fb4.80d9a8","type":"function","z":"54563ee4.3724f","name":"To Domoticz","func":"// Declarations\n\nvar msg1 ={}; // UV sensor value\nvar msg2 ={}; // UV alert\nvar msg3 ={}; // Max UV\nvar msg4 ={}; // Ozone\nvar msg5 ={}; // Max. Exposure Time\n\n// Function to covert UV index to level\n\nfunction uvi2level(x) {\n\nif (x < 3) {\n  return 0;\n} else if (x < 6) {\n  return 1;\n} else if (x < 8 ) {\n  return 2;\n} else if (x < 11 ) {\n  return 3;  \n} else {\n  return 4;\n}\n}\n\nmsg1.payload = {\"command\":\"udevice\",\"idx\":7,\"nvalue\":0,\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \";0\"};\nmsg2.payload = {\"command\":\"udevice\",\"idx\":9,\"nvalue\":uvi2level(msg.payload.result.uv),\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \" UVI\"};\nmsg3.payload = {\"command\":\"udevice\",\"idx\":6,\"nvalue\":uvi2level(msg.payload.result.uv_max),\"svalue\":msg.payload.result.uv_max.toFixed(1).toString() + \" UVI\" + \"\\n\" + msg.payload.result.uv_max_time};\nmsg4.payload = {\"command\":\"udevice\",\"idx\":1,\"nvalue\":0,\"svalue\":msg.payload.result.ozone.toString()};\nmsg5.payload = {\"command\":\"udevice\",\"idx\":2,\"nvalue\":0,\"svalue\":msg.payload.result.safe_exposure_time.st3.toString()};\nreturn [[msg1,msg2,msg3,msg4,msg5]];\n","outputs":1,"noerr":0,"x":910,"y":100,"wires":[["e7c6f4d4.b2a3"]]},{"id":"e7c6f4d4.b2a3","type":"mqtt out","z":"54563ee4.3724f","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"4cbe18fd.401df","x":1110,"y":100,"wires":[]},{"id":"50da51e2.b5eb88","type":"comment","z":"54563ee4.3724f","name":"Fitzpatrick Skin Types","info":"See: https://www.openuv.io/kb/skin-types-safe-exposure-time-calculation\n\n1. Very fair skin, white; red or blond hair; light-colored eyes; freckles likely\n2. Fair skin, white; light eyes; light hair\n3. Fair skin, cream white; any eye or hair color (very common skin type)\n4. Olive skin, typical Mediterranean Caucasian skin; dark brown hair; medium to heavy pigmentation\n5. Brown skin, typical Middle Eastern skin; dark hair; rarely sun sensitive\n6. Black skin; rarely sun sensitive","x":920,"y":60,"wires":[]},{"id":"42ad811d.5d234","type":"gate","z":"54563ee4.3724f","name":"Gate","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":590,"y":240,"wires":[["41887041.de03a8"]]},{"id":"5b9b1522.029444","type":"mqtt in","z":"54563ee4.3724f","name":"Domoticz Out","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"4cbe18fd.401df","x":130,"y":320,"wires":[["ff68ef4a.aa671"]]},{"id":"52fae9dc.b58bc","type":"inject","z":"54563ee4.3724f","name":"","topic":"","payload":"","payloadType":"date","repeat":"1800","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":240,"wires":[["42ad811d.5d234"]]},{"id":"8db20686.56f2d","type":"moment","z":"54563ee4.3724f","name":"","topic":"","input":"payload.result.uv_max_time","inputType":"msg","inTz":"Europe/Amsterdam","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD-MM-YYYY HH:mm:ss","locale":"en_US","output":"payload.result.uv_max_time","outputType":"msg","outTz":"Europe/Amsterdam","x":680,"y":100,"wires":[["3f946fb4.80d9a8"]]},{"id":"ff68ef4a.aa671","type":"json","z":"54563ee4.3724f","name":"Converter","property":"payload","action":"obj","pretty":false,"x":300,"y":320,"wires":[["4e6d8a4b.050624"]]},{"id":"4cbe18fd.401df","type":"mqtt-broker","z":"","name":"MQTT_Server","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Thanks in advance!

Jan
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by FireWizard »

@ Jan Jansen.

Hi

Please find below some explanation.

1. After the MQTT input node, you use a JSON node, called "Converter" and configured as "Always convert to JavaScript Object".
This is not necessary, because after some versions of the MQTT input node, you can set this easily in the MQTT input node itself.
Select at "Output", "a parsed JSON object", instead of "auto-detect (string or buffer).
So I have taken this out.

2. I have created a simple switch in Domoticz. I assume you did the same.
The switch itself shows On or Off, but not in the JSON object send to domoticz/out if you operate the switch.
In the JSON object you will see nvalue =1, if the switch goes to "On" and nvalue =0, if the switch goes to "Off"
So we have to use this nvalue.

3. By default you will see all messages coming from a number of devices. And this can be a large number.
A node that I often use, in these cases is the "Filter" node. However this is not a default node, after Node Red has been installed.
So you have to install it from the palette.
Therefore:
Open the "Hamburger"menu at the top right and look for "Manage palette".
Go to in the "Install"tab and search for "node-red-contrib-filter".
Install it.

The contents of the "Function" node is then as follows:

Code: Select all

if (msg.payload.nvalue == 1) {
    msg.topic = "control";
    msg.payload = "open"
    } else {
    msg.topic = "control";
    msg.payload = "close";
}
return msg;
You can do it also without the filter node and then the contents is:

Code: Select all

if (msg.payload.idx == 8) {
    if (msg.payload.nvalue == 1) {
    msg.topic = "control";
    msg.payload = "open"
    } else {
    msg.topic = "control";
    msg.payload = "close";
    }
}
return msg;
In this case it is easier to go without the "Filter" node, but in case you have to filter multiple things, this one is very easy.

Screenshot_Node-Red_OpenUV2.png
Screenshot_Node-Red_OpenUV2.png (41.98 KiB) Viewed 2768 times

The complete flow below:

Code: Select all

[{"id":"508df59.c00588c","type":"function","z":"5b04ce33.6fd2a8","name":"Prepare URL OpenUV","func":"var url = \"https://api.openuv.io/api/v1/uv\";\nvar lat = msg.payload.lat || \"51.842739\";\nvar lng = msg.payload.lon || \"5.86347\";\nvar alt = msg.payload.alt || \"10\";\nmsg.url = url + \"?lat=\" + lat + \"&lng=\" + lng + \"&alt=\" + alt;\nmsg.headers = {};\nmsg.headers[\"x-access-token\"] = \"XXXXXXXXXXXXXXXXXXXX\";\n\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":100,"wires":[["c7a4b24b.2f12d"]]},{"id":"c7a4b24b.2f12d","type":"http request","z":"5b04ce33.6fd2a8","name":"OpenUV http request","method":"GET","ret":"obj","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":420,"y":100,"wires":[["9c2e8602.bdd02"]]},{"id":"b5c9e02a.af832","type":"function","z":"5b04ce33.6fd2a8","name":"Control Gate","func":"if (msg.payload.nvalue == 1) {\n    msg.topic = \"control\";\n    msg.payload = \"open\"\n    }\n    else {\n    msg.topic = \"control\";\n    msg.payload = \"close\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":320,"wires":[["4a716dd0.925e14"]]},{"id":"826882ed.321378","type":"function","z":"5b04ce33.6fd2a8","name":"To Domoticz","func":"// Declarations\n\nvar msg1 ={}; // UV sensor value\nvar msg2 ={}; // UV alert\nvar msg3 ={}; // Max UV\nvar msg4 ={}; // Ozone\nvar msg5 ={}; // Max. Exposure Time\n\n// Function to covert UV index to level\n\nfunction uvi2level(x) {\n\nif (x < 3) {\n  return 0;\n} else if (x < 6) {\n  return 1;\n} else if (x < 8 ) {\n  return 2;\n} else if (x < 11 ) {\n  return 3;  \n} else {\n  return 4;\n}\n}\n\nmsg1.payload = {\"command\":\"udevice\",\"idx\":7,\"nvalue\":0,\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \";0\"};\nmsg2.payload = {\"command\":\"udevice\",\"idx\":9,\"nvalue\":uvi2level(msg.payload.result.uv),\"svalue\":msg.payload.result.uv.toFixed(1).toString() + \" UVI\"};\nmsg3.payload = {\"command\":\"udevice\",\"idx\":6,\"nvalue\":uvi2level(msg.payload.result.uv_max),\"svalue\":msg.payload.result.uv_max.toFixed(1).toString() + \" UVI\" + \"\\n\" + msg.payload.result.uv_max_time};\nmsg4.payload = {\"command\":\"udevice\",\"idx\":1,\"nvalue\":0,\"svalue\":msg.payload.result.ozone.toString()};\nmsg5.payload = {\"command\":\"udevice\",\"idx\":2,\"nvalue\":0,\"svalue\":msg.payload.result.safe_exposure_time.st3.toString()};\nreturn [[msg1,msg2,msg3,msg4,msg5]];\n","outputs":1,"noerr":0,"x":910,"y":100,"wires":[["bac06464.1672"]]},{"id":"bac06464.1672","type":"mqtt out","z":"5b04ce33.6fd2a8","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"87b7c98f.e7569","x":1110,"y":100,"wires":[]},{"id":"d42df6a6.fdefc","type":"comment","z":"5b04ce33.6fd2a8","name":"Fitzpatrick Skin Types","info":"See: https://www.openuv.io/kb/skin-types-safe-exposure-time-calculation\n\n1. Very fair skin, white; red or blond hair; light-colored eyes; freckles likely\n2. Fair skin, white; light eyes; light hair\n3. Fair skin, cream white; any eye or hair color (very common skin type)\n4. Olive skin, typical Mediterranean Caucasian skin; dark brown hair; medium to heavy pigmentation\n5. Brown skin, typical Middle Eastern skin; dark hair; rarely sun sensitive\n6. Black skin; rarely sun sensitive","x":920,"y":60,"wires":[]},{"id":"4a716dd0.925e14","type":"gate","z":"5b04ce33.6fd2a8","name":"Gate","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":670,"y":240,"wires":[["508df59.c00588c"]]},{"id":"2a6bf001.93be4","type":"mqtt in","z":"5b04ce33.6fd2a8","name":"Domoticz Out","topic":"domoticz/out","qos":"2","datatype":"json","broker":"87b7c98f.e7569","x":130,"y":320,"wires":[["10bbb947.9bc327"]]},{"id":"c5bb1cce.57b1b","type":"inject","z":"5b04ce33.6fd2a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":240,"wires":[["4a716dd0.925e14"]]},{"id":"9c2e8602.bdd02","type":"moment","z":"5b04ce33.6fd2a8","name":"","topic":"","input":"payload.result.uv_max_time","inputType":"msg","inTz":"Europe/Amsterdam","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD-MM-YYYY HH:mm:ss","locale":"en_US","output":"payload.result.uv_max_time","outputType":"msg","outTz":"Europe/Amsterdam","x":680,"y":100,"wires":[["826882ed.321378"]]},{"id":"10bbb947.9bc327","type":"filter","z":"5b04ce33.6fd2a8","name":"Filter Switch IDX 8","property":"payload.idx","propertyType":"msg","asArray":false,"itemProperty":"","itemPropertyType":"item","rules":[{"t":"eq","v":"8","vt":"num","output":1}],"checkall":"true","outputs":1,"x":350,"y":320,"wires":[["b5c9e02a.af832"]]},{"id":"87b7c98f.e7569","type":"mqtt-broker","z":"","name":"MQTT_Server","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Best regards
Last edited by FireWizard on Monday 25 May 2020 21:27, edited 1 time in total.
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: OpenUV.io plugin max. 50 request/day

Post by Jan Jansen »

@ FireWizard,

Hello,

Thanks again!

Jan
Last edited by Jan Jansen on Friday 29 May 2020 6:13, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest