Sensor to trigger automation that shows when pets are fed

I have been searching the forums without much luck and I was wondering if someone can help point me in the right direction. I am trying to use a contact sensor to report to HA that our animals have been fed. I was hoping to have an automation that changed a picture of our pets from b&w to color or even just change text to notify they have been fed. I guess that really wouldn’t even work to well as they are fed twice a day so maybe have a card that displays the times the sensors have opened. I have the sensors already set up and the images accessable is HA. I’m guessing I need an automation to tell me when the sensor was opened and then have it reset nightly.

Ok I was able to get a template sensor made from the contact sensor that displays the date and time, now I am just having a problem getting it to out put a more readable format currently I have

  - platform: template
    sensors:
      dollars_food:
        friendly_name: Dollar Fed
        value_template: '{{ state_attr("binary_sensor.wyzesense_dollars", "timestamp") }}'

and this displays 2019-12-30T09:37:49.796000, what can I add to get the date and time more readable?
preferably looking like this 12-30-2019 09:37:49

 {% set t = '2019-12-30T09:37:49.796000' %}
 {% set u = as_timestamp(strptime(t,'%Y-%m-%dT%h:%m:%s')) %}
 
 {{ u | timestamp_custom("%m-%d-%Y %-I:%M %p") }}

Obviously adjust accordingly.

Also, this thread is great for helping w/ time manipulations: The EPIC Time Conversion and Manipulation Thread!

Thank you, I will try this when I get home tonight!

I cant wrap my head around it, the example you gave me does convert that specific time the right way

but I am trying to get it to change the format of the sensor every time its updated.

{{ state_attr("binary_sensor.wyzesense_dollars", "timestamp") }}

I cannot figure out how to input the sensor in the template, that thread has some really great info most of it beyond my level of skills and it seemed hopeful with some of the examples but nothing worked for me.

This is the value_template output I get from that sensor

and here is the card I am using for now to see what time they have been fed

image

Try this:

{% set t = state_attr('binary_sensor.wyzesense_dollars', 'timestamp') %}
{% set u = as_timestamp(strptime(t,'%Y-%m-%dT%h:%m:%s')) %}
 
{{ u | timestamp_custom("%m-%d-%Y %-I:%M %p") }}
1 Like

That’s it! I am sure I tried it like that but I bet the format was off somewhere.

Thank you for your time.

1 Like

Here is the config

  - platform: template
    sensors:
      dollars_food:
        friendly_name: Dollar Fed
        icon_template: mdi:dog
        device_class: timestamp
        value_template: >
          {% set t = state_attr('binary_sensor.wyzesense_dollars', 'timestamp') %}
          {% set u = as_timestamp(strptime(t,'%Y-%m-%dT%h:%m:%s')) %}
          {{ u | timestamp_custom("%-I:%M%p %m-%d-%Y") }}

  - platform: template
    sensors:
      bennys_food:
        friendly_name: Benny Fed
        icon_template: mdi:cat
        device_class: timestamp
        value_template: >
          {% set t = state_attr('binary_sensor.wyzesense_bennys', 'timestamp') %}
          {% set u = as_timestamp(strptime(t,'%Y-%m-%dT%h:%m:%s')) %}
          {{ u | timestamp_custom("%-I:%M%p %m-%d-%Y") }}

And here is the result

image

1 Like