Using an Inky Display

Home Assistant General

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.

When setting up a Pi zero for Room Assistant it seemed like a good idea to add a small screen, so I purchased an Inky Phat. I also got a 3d printed stand based on one from AdaFruit

After installing the standard software using the instructions here, I wrote a short script to set the screen contents when ever a json message was received on MQTT.

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.

  - platform: template
    sensors:
      kitchen_time_remaining:
        friendly_name: Kitchen Timer Time Remaining
        value_template: >
            {%set v= states('sensor.kitchen_sonos_next_timer')%}
            {% if v =='unavailable' %}
            -1
            {% else %}
            {{((states('sensor.kitchen_sonos_next_timer')|as_timestamp -             
                    utcnow()|as_timestamp)/60)|round}}
            {% endif %}

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.

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.

alias: Kitchen Alarm and Messages
description: >-
  Monitor for the Alexa Timer or for messages to be sent to the Inky Pi in the
  Kitchen and send to it.
trigger:
  - platform: state
    entity_id: sensor.kitchen_time_remaining
  - platform: state
    entity_id: calendar.alexa_shopping_list
  - platform: state
    entity_id: input_text.kitchen_message
  - platform: state
    entity_id: input_text.current_quote
condition: []
action:
  - service: mqtt.publish
    data:
      topic: rapiz2/message
      payload_template: |
        { {%if states.calendar.alexa_shopping_list.attributes.all_tasks %}
          "shopping":
                 {{states.calendar.alexa_shopping_list.attributes.all_tasks | tojson() }}, {% endif %}
          "timer": 
                 {{states.sensor.kitchen_time_remaining.state}},
          "quote": 
                  "{{states.input_text.current_quote.state}}" {%if states.input_text.kitchen_message.state != ''%},
          "message": "{{states.input_text.kitchen_message.state}}" {% endif %}
          }
mode: single

Script for Pi

I have removed the ip for my Pi and the user and password for the MTQQ but the rest is as used.

# mqtt callbacks
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        logging.info("connected OK")
        client.subscribe(topic)
        logging.info("subscribed to " + topic )
    else:
        logging.warning("Bad connection Returned code=", rc)


def on_publish(client, userdata, mid):
    print("mid: " + str(mid))

def on_message(client, userdata, message):
    msg =  str(message.payload.decode("utf-8"))

    font_size = 18
    payload = json.loads(msg) 
    output = 'Smile Nothing to do'
    if 'quote' in payload:          
                output = (payload['quote'])
    if 'message' in payload:          
                output = (payload['message'])
    elif 'shopping' in payload:
                output = 'Buy: '
                for item in payload['shopping']:
                   output = output + item + ', '           

    if 'timer' in payload:
          if payload['timer'] > -1:
             if payload['timer'] > 1:
               output = str(payload['timer']) + ' mins'
             elif payload['timer'] == 1:
               output = str(payload['timer']) + ' min'
             else:
               output = '<1 min'
             font_size = 50


    display_message(output,font_size)
    logging.info("display =" + output)
        

# ---------------------------------------------- Main Logic           
broker_address="xxxxxxxx" 
topic = "rapiz2/message"

client = mqtt.Client("P2") #create new instance
client.username_pw_set("xxxx", "xxx")

client.on_connect = on_connect
client.on_publish = on_publish
client.on_message=on_message 

client.connect(broker_address) #connect to broker

client.loop_forever() 

The service config is very simple.

[Unit]
Description=Inky MQTT Message Display
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/enviroplus-python/mtqq-message-inky.py
WorkingDirectory=/home/pi/enviroplus-python
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target