Need help OH3 http binding

hello,
i want to translate my OH2 setup here http binding into the new OH3

Bt i am completely lost with the switch

here is my old setup

item:

Switch item_pihole_enabled "PiHole" ["Switchable"] { http="<[pihole_http_cache:20000:JS(getPiHoleStatus.js)]" }

transform.js

( function (input) { var parsedInput = JSON.parse(input); if (parsedInput.status === 'enabled') { return 'ON'; } return 'OFF'; } )(input);

rule

rule "Enable PiHole"
 when 
Item item_pihole_enabled received command ON
 then 
var String HTTPString = "http://pi.hole/admin/api.php?enable&auth=API_TOKEN" HTTPString.sendHttpGetRequest(20000); end rule "Disable PiHole" when Item item_pihole_enabled received command OFF then var String HTTPString = "http://pi.hole/admin/api.php?disable&auth=API_TOKEN" HTTPString.sendHttpGetRequest(20000); 
end

my OH3 thing

Thing http:url:pihole "pihole api" [ baseURL="http://pihole.fritz.box/admin/api.php", refresh=3600] {
    Channels:
        Type number : PiHole_DomainsBeingBlocked "Domains being blocked [%d]" [ stateTransformation="JSONPATH:$.domains_being_blocked" ]


}

can someone help me?

I’ve been moving to a UI based config and done things slightly different in that I have a Proxy item to switch the status of PiHole - easy to define as it wouldn’t be assigned a channel:

Switch PiHole_Toggle "PiHole" ["Switchable"]

I then use a rule that updates the toggle in case the PiHole admin interface is used to disable it (note: there will be a delay in the update depending on your refresh interval). The rule is triggered by a the status item changing.

if (event.itemState.toString == 'enabled') {
  events.postUpdate('PiHole_Toggle', ON)
} else {
  events.postUpdate('PiHole_Toggle', OFF)
}

Finally, I have a javascript for the toggle (I’ve left logging commands in which are set to warn as they show up in a different color )

var APIKEY = "XXXXXXXXXX"
var PIHOLE_IP = "XXX.XXX.XXX.XXX"

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
logger.warn( "[PiHole] Toggle - Item: " + event.itemName);
logger.warn( "[PiHole] Toggle - State: " + event.itemCommand);

var client = java.net.http.HttpClient.newBuilder().build();
function httpGet(uri) {
  //logger.warn( "[PiHole] Toggle - Get: " + uri);
  try {
    var request = java.net.http.HttpRequest.newBuilder()
      .GET()
      .uri(java.net.URI.create(uri))
      .header("Content-Type", "application/json")
      .build();
    var response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
    //logger.warn( "[PiHole] Toggle - Response: " + response.body());
    return response.body();
  } catch (e) {
    print(e);
  }
}

//build request URL
var url = "http://" + PIHOLE_IP + "/admin/api.php?auth=" + APIKEY
if (event.itemCommand == OFF) {
  url = url + "&disable=300";
} else {
  url = url + "&enable=300";
}

//logger.warn( "[PiHole] Toggle - Url: " + url);
logger.warn( "[PiHole] Toggle - Response: " + httpGet(url));

I tried this

Thing http:url:pihole "pihole api" [ baseURL="http://pihole.fritz.box/admin/api.php", refresh=3600, commandMethod="GET"] {
    Channels:
        Type switch : PiHole_Switch "Status [%s]" [ stateTransformation="JSONPATH:$.status", commandExtension="%2$s", onValue="enable&auth=TOKEN", offValue="disable&auth=TOKEN" ]
}

But this is not running

@J-N-K can you help me?

No. Try setting in UI and Post YAML if it is not working.

@J-N-K here is the yaml

UID: http:url:pihole
label: pihole api
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://pihole.fritz.box/admin/api.php
delay: 0
refresh: 60
commandMethod: GET
timeout: 3000
bufferSize: 2048
channels:

  • id: PiHole_DomainsBeingBlocked
    channelTypeUID: http:number
    label: Domains being blocked [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.domains_being_blocked
  • id: PiHole_DnsQueriesToday
    channelTypeUID: http:number
    label: DNS queries today [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.dns_queries_today
  • id: PiHole_AdsBlockedToday
    channelTypeUID: http:number
    label: Ads blocked today [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.ads_blocked_today
  • id: PiHole_AdsPercentageToday
    channelTypeUID: http:number
    label: Ads percentage today [%.2f %%]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.ads_percentage_today
  • id: PiHole_UniqueDomains
    channelTypeUID: http:number
    label: Unique domains [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.unique_domains
  • id: PiHole_QueriesForwarded
    channelTypeUID: http:number
    label: Queries forwarded [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.queries_forwarded
  • id: PiHole_QueriesCached
    channelTypeUID: http:number
    label: Queries cached [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.queries_cached
  • id: PiHole_ClientsEverSeen
    channelTypeUID: http:number
    label: Clients ever seen [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.clients_ever_seen
  • id: PiHole_UniqueClients
    channelTypeUID: http:number
    label: Unique clients [%d]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.unique_clients
  • id: PiHole_Status
    channelTypeUID: http:string
    label: Status [%s]
    description: null
    configuration:
    stateTransformation: JSONPATH:$.status
  • id: PiHole_Switch
    channelTypeUID: http:switch
    label: Status [%s]
    description: null
    configuration:
    onValue: enable&auth=token
    offValue: disable&auth=token
    stateTransformation: JSONPATH:$.status
    commandExtension: “%2$s”

But still not working

And what is not working?

I want to disable or enable my pihole instance this is done by the URL

pihole.fritz.box/admin/api.php?enable&auth=token

But with my switch it is not possible to enable or disable the pihole instance and with this “stateTransformation: JSONPATH:$.status” I want that my switch is “on” when pihole.fritz.box/admin/api.php json status is shown “enable” and “OFF” when the api.php shows disable.

it is runing now

1 Like