Reading Afvalwijzer in JSscripting or Jython

Nice :slight_smile:

Sorry, it DOES seem to work!

removed

The OP has been updated. I’ve refactored my code:

  • added the querying of afvalkalender as an alternative to mijnafvalwijzer;
  • improved formatting after using pylint.
2 Likes

Really would like to use something like this. But i can;t get it to work. If i read Please verify the url:' + jsonUrl + 'manually in your browser · Issue #72 · xirixiz/homeassistant-afvalwijzer · GitHub the json.mijnafvalwijzer.nl no longer works. they moved to an api with apikey.

As the topic at the HA forum suggests, this API key can be obtained with some “inspection” of the Android app. I managed to get it that way and supplied in the script. See the OP for the revised python script. You will have to find and enter the APi key yourself though.

Hi,

Thanks for the work you put in this rule.
Does it still works under OH3, when I try to save the rule when I fill out the configuration, the rule don’t get loaded because of this error:

==> /var/log/openhab/openhab.log <==

2021-12-15 16:38:53.828 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Afvalwijzer.rules'

2021-12-15 16:38:53.965 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Afvalwijzer.rules' has errors, therefore ignoring it: [1,8]: no viable alternative at input 'from'

[2,22]: no viable alternative at input ','

[4,24]: no viable alternative at input 'rule'

[5,1]: no viable alternative at input 'from'

[21,6]: no viable alternative at input '('

Any thoughts? Looking forward to use this rule!

Hi Stéphane,

This is not a DSL rule that you can use in a .rules file. This is a Python script and for that you need to install the Jython add-on:

and some helper libraries that can be found here:
Jython helper libraries for OH3

Hi Ard,

Ah that’s explains it, sorry my mistake.
Will look into how to use the script with the add-on, not used anything like that before.

Thanks for the clarification :slight_smile:

Is this script still working? because i see i need an api key. That is not available anymore.
MIJNAFVALWIJZER_APIKEY = “”

It does work, but I cannot provide the api key. Did you by any chance google the terms “afvalwijzer webservices appsinput” and looked at the URL of one of the first results? :wink:

Thank you. but the api key will stop working soon? When I read the topic on home-assistant they use a scraper.

The API key is still valid. I’ll try to make a scraping version when I find more time or once the API key is no longer valid (whatever comes first).

I added a javascript version of the rule because I no longer use Jython as my main rule engine.

1 Like

@ArdKuijpers I think your zipcode and housenumber is still in the JavaScript version.

1 Like

Actually, it is not mine (although not far away). I found it the code mentioned here: Idea: use python to directly generate rules files and I have since then used it to test my code.

Hi Ard,

I tried the script. Everytime the script runs it creates a new rule “afval ophaaldata updaten”.

How do i remove these rules if i don’t want to use the script anymore?
I can’t delete them through the ui because “Some of the selected rules are not modifiable because they have been provisioned by files”

EDIT: Found out they get deleted on a system reboot.

Thanks @ArdKuijpers wrote in the xmas break a js version as well, but not as extensive as yours…
Do you have a hint on this error?
EDIT: Seems I can prevent the error by removing the 2 actions.NotificationAction.sendBroadcastNotification(message); lines. Guess they are not known as I don’t have cloud notifications enabled.

note I chose the application/javascript;version=ECMAScript-2021 version… is that the right one or I should use the other one.

10:44:09.704 [DEBUG] [enhab.automation.script.afvalkalender] - Afval ophaaldata ophalen voor 4700ZZ-25
10:44:09.867 [DEBUG] [enhab.automation.script.afvalkalender] - Afvalwijzer URI: https://api.mijnafvalwijzer.nl/webservices/appsinput/?method=postcodecheck&street=&postcode=4700ZZ&huisnummer=25&toevoeging=&apikey=5ef443e778f41c4f75c69459eea6e6ae0c2d92de729aa0fc61653815fbd6a8ca&app_name=afvalwijzer&platform=phone&mobiletype=android&afvaldata=2022-01-10&version=58&langs=nl
10:44:10.542 [DEBUG] [enhab.automation.script.afvalkalender] - Ophaaldata verwerken voor papier: oud papier en Item AfvalData_papier on 2022-01-10T10:44:10.531+01:00[SYSTEM]
10:44:10.556 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'AfvalData_papier' changed from NULL to 2022-01-10T00:00:00.000+0100
10:44:10.557 [DEBUG] [org.openhab.automation.script.items  ] - Posted update 2022-01-10 to AfvalData_papier
10:44:10.559 [INFO ] [enhab.automation.script.afvalkalender] - Vandaag wordt het oud papier opgehaald.
10:44:10.564 [ERROR] [org.openhab.automation.script.rules  ] - Failed to execute rule Afval-ophaaldata-updaten-0afa0a3c-3c53-46cb-aa47-dc7982bbd6ae: TypeError: undefined has no such function "sendBroadcastNotification": TypeError: undefined has no such function "sendBroadcastNotification"
        at processPickupdates (<eval>:129)
        at execute (<eval>:88)
        at doExecute (webpack://openhab/./node_modules/openhab/rules/rules.js?:110)
10:44:10.566 [WARN ] [re.automation.internal.RuleEngineImpl] - Fail to execute action: 1
org.graalvm.polyglot.PolyglotException: TypeError: undefined has no such function "sendBroadcastNotification"
        at <js>.processPickupdates(<eval>:129) ~[?:?]
        at <js>.execute(<eval>:88) ~[?:?]
        at <js>.doExecute(webpack://openhab/./node_modules/openhab/rules/rules.js?:110) ~[?:?]
        at org.graalvm.polyglot.Value.invokeMember(Value.java:934) ~[?:?]
        at com.oracle.truffle.host.adapters.SimpleRule$$Adapter.execute(Unknown Source) ~[?:?]
        at org.openhab.automation.jsscripting.internal.threading.ThreadsafeSimpleRuleDelegate.execute(ThreadsafeSimpleRuleDelegate.java:59) ~[?:?]
        at org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate.execute(SimpleRuleActionHandlerDelegate.java:34) ~[?:?]
        at org.openhab.core.automation.module.script.rulesupport.internal.delegates.SimpleActionHandlerDelegate.execute(SimpleActionHandlerDelegate.java:59) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1180) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1032) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1048) ~[?:?]
        at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:327) ~[?:?]
        at jdk.internal.reflect.GeneratedMethodAccessor158.invoke(Unknown Source) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) ~[bundleFile:3.4.5]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[bundleFile:3.4.5]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:265) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:225) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:298) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:217) ~[bundleFile:3.4.5]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[bundleFile:3.1.0]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:273) ~[bundleFile:3.4.5]
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550) ~[bundleFile:9.4.43.v20210629]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1434) ~[bundleFile:9.4.43.v20210629]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:294) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1349) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[bundleFile:9.4.43.v20210629]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:82) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388) ~[bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:386) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) [bundleFile:9.4.43.v20210629]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) [bundleFile:9.4.43.v20210629]
        at java.lang.Thread.run(Thread.java:829) [?:?]

Struggled with the same…
I found that restarting the rulesupport will work as well…

openhab> bundle:list | grep RuleSupport
160 x Active    x  80 x 3.2.0                 x openHAB Core :: Bundles :: Automation Script RuleSupport
openhab> bundle:restart 160

I chose this version (2021) and it works well. Well…a sort of.
A first i’ll get this message:
Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.

Then a couple of seconds later the script executes without warnings.

The problem i encounter is that only items (after manually running the rule “afval ophaaldata updaten”):
AfvalData_volgendeDatum and
AfvalData_volgendeType

get updated. The other items don’t and keep their NULL value.

Thanks.

Recognise the issue.
For me that was solved by changing the afval type.
e.g. in the feed from api.mijnafvalwijzer.nl for my gemeente the type was plastic instead of pmd.
So changing the processPickupdates line to processPickupdates(pickupdates, 'plastic', 'plastic- en metaalafval en drinkkartons', ${itemPrefix}_pmd); fixed updating plastics.

Just use the url you see in the log manually in a browser and you see the types as per your gemeente

@ArdKuijpers you use icons like <garbage_papier> which are not standard openhab-classics ones.
do you have nice icons for this to share/recommend?

I figured it would be something like that.
Not as simple as your case but i managed to extract the JSON from afvalkalender and found the right naming for the afval type :slight_smile:

So for anybody using Cyclus NV change the processPickupdates lines to:

    processPickupdates(pickupdates, 'doos-karton-papier',           'oud papier',                              `${itemPrefix}_papier`);
    processPickupdates(pickupdates, 'appel-gft',                    'groente-, fruit- en tuinafval',           `${itemPrefix}_gft`);
    processPickupdates(pickupdates, 'zak-grijs-rest',               'restafval',                               `${itemPrefix}_restafval`);
    processPickupdates(pickupdates, 'petfles-blik-drankpak_pmd',    'plastic- en metaalafval en drinkkartons', `${itemPrefix}_pmd`);  
    processPickupdates(pickupdates, 'kerstboom-zonder-kruis',       'kerstbomen',                              `${itemPrefix}_kerstbomen`);

I removed the “klein chemisch afval” and added “kerstbomen”