Public Holiday API and HTTP Binding

  • Platform information:
    • Raspberrypi 4
    • openhab-js 4
    • openHAB version: 4

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?

Many thanks for your ideas!

Instead of the HTTP Binding, you can use a core HTTP action in a cron rule and pass that result to an unbound item.

Unfortunately, I can’t help with the second part.

Use a set shell commands together with executeCommandLine to return the status code.
Or have a look at e.g. this thread Get http status code when using sendHttpGetRequest in rule

Have you had a look into Actions | openHAB if that already covers your requirements ?

This is a nice Website, thx for this.

Create a string-item holiday

Create a textfile “holiday.sh” in the etc/openhab/scripts folder:

STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" https://date.nager.at/Api/v3/IsTodayPublicHoliday/ES?countyCode=ES-AN)

echo $STATUSCODE

Create a holiday.rules file in the rules folder:

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…

1 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 :slight_smile:
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:

  1. 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).

  1. 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?

1.Try

#!/bin/bash

at first in shell script.

  1. What do you mean? I don’t get it…