Stuff & Nonsense

Pill Prompter in Home Assistant

As someone with many, many neurological conditions and also many many regular pills to take for those conditions, one of the problems I encounter regularly is not being sure whether or not I’ve actually taken my pills today. Obviously just slamming down more of them isn’t really an option, but my memory really isn’t up to the task of consistently knowing whether I’ve taken them or not. What I need is some way to simply and consistently record that I’ve taken them that day.

Luckily, I have a Home assistant setup, and lots of extra buttons and sensors so…..

First of all, I need a button. This can be anything really that can trigger a sensor state change in Home assistant. I used a sonoff zigbee button because I had it lying aorund already, but use whatever you have available.


Next, we need to work out if the button has been pushed today or not. We use a template sensor for this, so add this to your configuration.yaml:

template:
  - binary_sensor:
      - name: "Taken Pills"
        unique_id: taken_pills_sensor
        state: >
                {% set today = now().year + now().month + now().day  %} 
                {% set last = states.sensor.pillbutton_action.last_changed.year + states.sensor.pillbutton_action.last_changed.month + states.sensor.pillbutton_action.last_changed.day %}
{{ today == last }}

This looks pretty gnarly, but it’s pretty straightforward. We’re declaring a new binary sensor, named ‘Taken Pills’ and giving it a unique id.

For the state of that sensor we first get a string for todays date with the format YYYYMMDD, then get a string for the last time our button entity changed state (here my button entity is called ‘pillbutton_action’). Then we compare the two strings. If they’re the same, the button has been pushed today and we output ‘true’. If they’re different, the button hasn’t been pushed today.

Great, we now have an entity that shows true when the button has been pushed today, false if it hasn’t. We can now use that to display a prompt in our dashboards.

We use conditional cards here, which allow us to display a card if certain conditions are met. We use one for when our template sensor is ‘true’ to remind us that we’ve taken our pills, and another that only displays when our template sensor is ‘false’ to tell us we need to take our pills.
Here’s what those look like in the yaml editor:

cards:
  - type: conditional
    conditions:
      - condition: state
        entity: binary_sensor.taken_pills
        state: "on"
    card:
      type: markdown
      content: "**Tablets Taken Today**"
  - type: conditional
    conditions:
      - condition: state
        entity: binary_sensor.taken_pills
        state: "off"
    card:
      type: markdown
      content: "**TAKE YOUR TABLETS**"
      theme: LCARS Kronos

And that’s it really. Every morning the card showing ‘TAKE YOUR TABLETS’ appears and stays visible until I take my pills and press the button, at which point it changes to ‘Tablets taken today’.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.