{"id":13315,"date":"2021-04-22T10:32:33","date_gmt":"2021-04-22T10:32:33","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13315"},"modified":"2021-04-22T10:56:59","modified_gmt":"2021-04-22T10:56:59","slug":"using-an-inky-display","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/using-an-inky-display\/","title":{"rendered":"Using an Inky Display"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/04\/Room-Assistant-Pi-Kitchen.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"688\" src=\"https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/04\/Room-Assistant-Pi-Kitchen.jpg\" alt=\"\" class=\"wp-image-13316\" srcset=\"https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/04\/Room-Assistant-Pi-Kitchen.jpg 600w, https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/04\/Room-Assistant-Pi-Kitchen-262x300.jpg 262w, https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/04\/Room-Assistant-Pi-Kitchen-131x150.jpg 131w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>I have wanted a display in the kitchen for a while, just to show the time remaining on the alexa timer, or when the bins need to go out.<\/p>\n\n\n\n<p>When setting up a Pi zero for <a href=\"https:\/\/www.room-assistant.io\/\">Room Assistant<\/a> it seemed like a good idea to add a small screen, so I purchased an <a href=\"https:\/\/shop.pimoroni.com\/products\/inky-phat?variant=12549254217811\">Inky Phat<\/a>.  I also got a 3d printed stand based on one from <a href=\"https:\/\/blog.adafruit.com\/2020\/08\/06\/raspberry-pi-zero-stand-3dthursday-3dprinting\/\">AdaFruit<\/a><\/p>\n\n\n\n<p>After installing the standard software using the instructions <a href=\"https:\/\/learn.pimoroni.com\/tutorial\/sandyj\/getting-started-with-inky-phat\">here<\/a>, I wrote a short script to set the screen contents when ever a json message was received on MQTT.<\/p>\n\n\n\n<p>The timer value for the Alexa is the time the timer will go off,  so to get a changing value for the number of minutes left I used a template sensor as shown below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  - platform: template\n    sensors:\n      kitchen_time_remaining:\n        friendly_name: Kitchen Timer Time Remaining\n        value_template: >\n            {%set v= states('sensor.kitchen_sonos_next_timer')%}\n            {% if v =='unavailable' %}\n            -1\n            {% else %}\n            {{((states('sensor.kitchen_sonos_next_timer')|as_timestamp -             \n                    utcnow()|as_timestamp)\/60)|round}}\n            {% endif %}<\/pre>\n\n\n\n<p>Next I created an automation to create a json string to send down.   This contains the number of minutes the next timer, or -1 for no timer, a message text from a input-text helper, and a quote from another helper, which changes every hour on another automation.<\/p>\n\n\n\n<p>If the timer is running that is displayed, if not if there is a message that is displayed, if there are items on the shopping list (calendar) those are displayed.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias: Kitchen Alarm and Messages\ndescription: >-\n  Monitor for the Alexa Timer or for messages to be sent to the Inky Pi in the\n  Kitchen and send to it.\ntrigger:\n  - platform: state\n    entity_id: sensor.kitchen_time_remaining\n  - platform: state\n    entity_id: calendar.alexa_shopping_list\n  - platform: state\n    entity_id: input_text.kitchen_message\n  - platform: state\n    entity_id: input_text.current_quote\ncondition: []\naction:\n  - service: mqtt.publish\n    data:\n      topic: rapiz2\/message\n      payload_template: |\n        { {%if states.calendar.alexa_shopping_list.attributes.all_tasks %}\n          \"shopping\":\n                 {{states.calendar.alexa_shopping_list.attributes.all_tasks | tojson() }}, {% endif %}\n          \"timer\": \n                 {{states.sensor.kitchen_time_remaining.state}},\n          \"quote\": \n                  \"{{states.input_text.current_quote.state}}\" {%if states.input_text.kitchen_message.state != ''%},\n          \"message\": \"{{states.input_text.kitchen_message.state}}\" {% endif %}\n          }\nmode: single<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Script for Pi<\/h2>\n\n\n\n<p>I have removed the ip for my Pi and the user and password for the MTQQ but the rest is as used.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># mqtt callbacks\ndef on_connect(client, userdata, flags, rc):\n    if rc == 0:\n        logging.info(\"connected OK\")\n        client.subscribe(topic)\n        logging.info(\"subscribed to \" + topic )\n    else:\n        logging.warning(\"Bad connection Returned code=\", rc)\n\n\ndef on_publish(client, userdata, mid):\n    print(\"mid: \" + str(mid))\n\ndef on_message(client, userdata, message):\n    msg =  str(message.payload.decode(\"utf-8\"))\n\n    font_size = 18\n    payload = json.loads(msg) \n    output = 'Smile Nothing to do'\n    if 'quote' in payload:          \n                output = (payload['quote'])\n    if 'message' in payload:          \n                output = (payload['message'])\n    elif 'shopping' in payload:\n                output = 'Buy: '\n                for item in payload['shopping']:\n                   output = output + item + ', '           \n\n    if 'timer' in payload:\n          if payload['timer'] > -1:\n             if payload['timer'] > 1:\n               output = str(payload['timer']) + ' mins'\n             elif payload['timer'] == 1:\n               output = str(payload['timer']) + ' min'\n             else:\n               output = '&lt;1 min'\n             font_size = 50\n\n\n    display_message(output,font_size)\n    logging.info(\"display =\" + output)\n        \n\n# ---------------------------------------------- Main Logic           \nbroker_address=\"xxxxxxxx\" \ntopic = \"rapiz2\/message\"\n\nclient = mqtt.Client(\"P2\") #create new instance\nclient.username_pw_set(\"xxxx\", \"xxx\")\n\nclient.on_connect = on_connect\nclient.on_publish = on_publish\nclient.on_message=on_message \n\nclient.connect(broker_address) #connect to broker\n\nclient.loop_forever() <\/pre>\n\n\n\n<p>The service config is very simple.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[Unit]\nDescription=Inky MQTT Message Display\nAfter=network.target\n\n[Service]\nExecStart=\/usr\/bin\/python3 \/home\/pi\/enviroplus-python\/mtqq-message-inky.py\nWorkingDirectory=\/home\/pi\/enviroplus-python\nStandardOutput=inherit\nStandardError=inherit\nRestart=always\nUser=pi\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n","protected":false},"template":"","class_list":["post-13315","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}