I’m trying to use the very helpful API at https://date.nager.at/Api for that.
I have one and half issue with this.
First, the HTTP Binding, from what I understand is designed to recurringly update an item value by making, for example, GET calls. In this case, I would need to make one call per day only and that call should preferably be at midnight + bit… I undertand I can increase the delay between calls to 24h, but then the call could be made at - say - 4pm.
Is there any possibility to trigger the HTTP Binding item update manually (for example through a rule) or, alternatively, tell the binding, when to make the first call?
I could obviously call the API much more frequently and ignore the result, besides one time per day, but I think that would be a bit of overkill for the purpose and also not very friendly to such an amateur-maintained API…
Second, – on this one I have no idea how to do it at all – while there are quite a few helpful endpoints in the API, returning all kinds of useful information, the easiest one would be: (to check whether today is a public holiday in Andalucia, Spain: https://date.nager.at/api/v3/IsTodayPublicHoliday/EN?countyCode=AN&offset=0
On this endpoint, the return format is not json encoded. The returned body is actually empty. but the true or false information is returned through the HTML response code:
|Code|Description|Links|
| --- | --- | --- |
|200|Today is a public holiday|*No links*|
|204|Today is not a public holiday|*No links*|
|400|Validation failure|*No links*|
|404|CountryCode is unknown|*No links*|
Is there any possibility to transform the 200 response into a true and the 204 response into a false for a binary Item on the openHAB end?
rule "holiday"
when
Time cron "0 1 0 * * ?" // 00:01
then
val String result = executeCommandLine(Duration.ofSeconds(5), "/etc/openhab/scripts/holiday.sh")
if (result == "200") holiday.sendCommand ("holiday")
else holiday.sendCommand ("no holiday")
end
Every morning at 0:01 the Script gets executed and sets the item holiday to “holiday” or “no holiday”
Greets
Edit:
You could create your item as switch item. Then send ON or OFF.
or create a contact item and send OPEN or CLOSED.
What you like…
Not that I know of but you can use the sendHttpGetRequest Action in a rule instead of using the binding. then you can use a cron trigger to specify exactly when to make the request.
That’s an unfortunately odd choice on their part. Unfortunately the only way to get the return codes like that I think is to use what ever rule engine you are using’s native HTTP functions. This is probably easiest with JavaScript or jRuby as I believe both come with a pretty robust set of HTTP libraries out of the box. In Rules DSL you’ll have to use the Java HTTP classes.
It might be easier (especially if you want to use Rules DSL) to see if you can get access to the data using the Exec binding (which can be triggered by sending a command to an Item linked to the Running Channel) and wget or curl. I’m sure one or both of those can be made to just output the response codes.
Question, is there some limitation with Ephemeris built into OH that makes you not able to use that? Actions | openHAB
Hello Baschtl
This approach seems to do the trick. I have given up on DSL rules and moved to javascript rules for those. Also, I have tried to parametrize the script publicholiday.sh bash with country and county codes, like so:
/*
Create executable /etc/openhab/scripts/publicholiday.sh with following content:
-------------------------------------------------------------------------------------------------------------------------------------------------
if [[ -z $2 ]]
then extension=""
else extension="?countyCode=$1-$2"
fi
STATUSCODE=$(curl --output /dev/stderr --write-out "%{http_code}" https://date.nager.at/Api/v3/IsTodayPublicHoliday/$1$extension)
echo $STATUSCODE
-------------------------------------------------------------------------------------------------------------------------------------------------
Give execution privilege to script:
------------------------------------------------------
sudo chmod ugo+x /etc/openhab/scripts/publicholiday.sh
------------------------------------------------------
*/
var region = items.getItemsByTag('Region')[0].state.split('-');
var result = actions.Exec.executeCommandLine(time.Duration.ofSeconds(5), "/etc/openhab/scripts/publicholiday.sh", region[0], region[1]);
if (result != "200")
items.getItemsByTag('PublicHoliday')[0].postUpdate('OFF');
else
items.getItemsByTag('PublicHoliday')[0].postUpdate('ON');
There are two concerns:
Not an expert on bash shell scripts in Linux at all… but the if/else condition in there doesn’t work as expected, it returns:
2023-05-09 11:25:22.155 [INFO ] [automation.script.ui.isPublicHoliday] - /etc/openhab/scripts/publicholiday.sh: 1: [[: not found
Do you know, what’s wrong with the if condition? (I have changed the [[ to [ and to ( without any result).
I haven’t quite tested with a ‘real public holiday’ yet, but is there not a concern with the async character of the code? Is DSL avoiding this issue?