{"id":1026,"date":"2022-11-12T17:50:39","date_gmt":"2022-11-12T17:50:39","guid":{"rendered":"https:\/\/thomas.w-p.me.uk\/blog\/?p=1026"},"modified":"2022-11-12T17:51:30","modified_gmt":"2022-11-12T17:51:30","slug":"habapp","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2022\/11\/habapp\/","title":{"rendered":"HabAPP fail, cron win"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Trying to make python work for OpenHabian rules&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Used the openhabian cli to install HabAPP.<\/li>\n\n\n\n<li>The service didn&#8217;t start: <strong>ModuleNotFoundError: No module named &#8216;pydantic&#8217;<\/strong><\/li>\n\n\n\n<li>I knew I would need curl so&#8230;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pip3 install pydantic\nsudo systemctl start habapp.service\nsudo systemctl status habapp.service<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The service still didn&#8217;t start. Is HabAPP running in a venv?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip3 remove pydantic<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I determined from the HabAPP documentation that it might run in a venv<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>openhabian@openhabian:\/opt\/habapp $ source .\/bin\/activate\npip3 install pydantic\nsudo systemctl start habapp.service\nsudo systemctl status habapp.service<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Status now said I was missing a different module, <strong>EasyCo<\/strong>, and on I went installing and testing&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip3 install EasyCo\npip3 install eascheduler\npip3 install stackprinter\npip3 install watchdog\npip3 install paho-mqtt\npip3 install aiohttp\npip3 install aiohttp_sse_client\npip3 install immutables\npip3 install bidict<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And finally it worked! I was able to list the packages that are installed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(habapp) openhabian@openhabian:\/opt\/habapp $ sudo systemctl status habapp.service\n\u25cf habapp.service - HABApp\n   Loaded: loaded (\/etc\/systemd\/system\/habapp.service; enabled; vendor preset: enabled)\n   Active: active (running) since Sat 2022-11-12 16:03:56 GMT; 2s ago\n     Docs: https:\/\/habapp.readthedocs.io\n Main PID: 16102 (habapp)\n    Tasks: 7 (limit: 4915)\n   CGroup: \/system.slice\/habapp.service\n           \u2514\u250016102 \/opt\/habapp\/bin\/python3 \/opt\/habapp\/bin\/habapp -c \/etc\/openhab\/habapp\n\nNov 12 16:03:56 openhabian systemd&#91;1]: Started HABApp.\n(habapp) openhabian@openhabian:\/opt\/habapp $ pip3 list\nPackage            Version\n------------------ -------\naiohttp            3.8.3\naiohttp-sse-client 0.2.1\naiosignal          1.3.1\nastral             2.2\nasync-timeout      4.0.2\nasynctest          0.13.0\nattrs              22.1.0\nbidict             0.22.0\ncharset-normalizer 2.1.1\nEAScheduler        0.1.7\nEasyCo             0.2.3\nfrozenlist         1.3.3\nHABApp             0.31.2\nidna               3.4\nimmutables         0.19\nmultidict          6.0.2\npaho-mqtt          1.6.1\npendulum           2.1.2\npip                22.3.1\npkg_resources      0.0.0\npycurl             7.45.1\npydantic           1.10.2\npython-dateutil    2.8.2\npytz               2022.6\npytzdata           2020.1\nruamel.yaml        0.17.21\nruamel.yaml.clib   0.2.7\nsetuptools         65.5.1\nsix                1.16.0\nstackprinter       0.2.9\ntyping_extensions  4.4.0\nvoluptuous         0.13.1\nwatchdog           2.1.9\nyarl               1.8.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I then endlessley faffed about with no success, watching the log with tail -f and duckduckgoing until I found<a href=\"https:\/\/github.com\/spacemanspiff2007\/HABApp\/issues\/298\"> this page which told me it was broken in OH3.3<\/a>!! I went back in to the cli and removed HabAPP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Back to plan A then. I will set up a venv and script to make it work with cron. I already have the script. Should have done this ages ago. All it does is tell me the kWh yesterday as a proof of concept. I already have an Item set up with the number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\nimport pycurl\nimport certifi\nfrom io import BytesIO\nfrom openhab import OpenHAB\n\nbuffer = BytesIO()\nc = pycurl.Curl()\nc.setopt(c.URL, 'https:\/\/api.octopus.energy\/v1\/electricity-meter-points\/xxxxxxxx\/meters\/xxxxxxxxxxxxxx\/consumption\/')\nc.setopt(c.USERPWD,'xxxxxxxxxxxxx:')\nc.setopt(c.WRITEDATA, buffer)\nc.setopt(c.CAINFO, certifi.where())\nc.perform()\nc.close()\n\nbody = buffer.getvalue()\nusage = json.loads(body.decode('iso-8859-1'))\n# Body is a byte string.\n# We have to know the encoding in order to print it to a text file\n# such as standard output.\ncount = 0\nyesterday = 0\nfor x in usage&#91;\"results\"]:\n    yesterday = yesterday + x&#91;\"consumption\"]\n    count = count + 1\n    #print(count,x&#91;\"consumption\"])\n    if count == 48:\n        break\nyesterday = round(yesterday,2)\n\nbase_url = 'http:\/\/openhabian:8080\/rest'\nopenhab = OpenHAB(base_url)\n\n# fetch all items\nitems = openhab.fetch_all_items()\nitems&#91;'Yesterday_kWh'].update(str(yesterday) + 'kWh')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And schedule that with cron by making a script to call it that has the full path to the venv inside calling the main.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 7 * * * \/home\/openhabian\/projects\/energy\/energy.py<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Trying to make python work for OpenHabian rules&#8230; Notes The service still didn&#8217;t start. Is HabAPP running in a venv? I determined from the HabAPP documentation that it might run in a venv Status now said I was missing a different module, EasyCo, and on I went installing and testing&#8230; And finally it worked! I &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2022\/11\/habapp\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HabAPP fail, cron win<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,104],"tags":[],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-geekiness","category-openhab"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":4,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1026\/revisions"}],"predecessor-version":[{"id":1030,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1026\/revisions\/1030"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}