Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOT AN ISSUE, just giving something back to the community: Template to show today, coming days birtdays, events #101

Open
dannymertens opened this issue Feb 27, 2021 · 10 comments

Comments

@dannymertens
Copy link

dannymertens commented Feb 27, 2021

Hi,

With the help from Jenny and a very friendly community member hereby a template that I could create to show the today events

{% for state in states.sensor -%} {%- if state.attributes.attribution == "Sensor data calculated by Anniversaries Integration" and state.state | int <= 0 -%} {{state.attributes.friendly_name}}: {{state.attributes.date }} current {{state.attributes.current_years }} years old, wil be {{state.attributes.years_at_next_anniversary }} years old {% endif -%} {%- endfor %}

Output: event: 27-02-2021 current 0 years old, wil be 1 years old

Just change the value and state.state | int <= 1 -%} to for instance 1 to get Today and tomorrow

{% for state in states.sensor -%} {%- if state.attributes.attribution == "Sensor data calculated by Anniversaries Integration" and state.state | int <= 1 -%} {{state.attributes.friendly_name}}: {{state.attributes.date }} current {{state.attributes.current_years }} years old, wil be {{state.attributes.years_at_next_anniversary }} years old {% endif -%} {%- endfor %}

Today and next 2 days

{% for state in states.sensor -%} {%- if state.attributes.attribution == "Sensor data calculated by Anniversaries Integration" and state.state | int <= 2 -%} {{state.attributes.friendly_name}}: {{state.attributes.date }} current {{state.attributes.current_years }} years old, wil be {{state.attributes.years_at_next_anniversary }} years old {% endif -%} {%- endfor %}

and....

Use for instance with a TTS notification (https://github.com/rt400/ReversoTTS-HA)

image

Templates for in your config

image

Took me some time to figure this out so maybe helpful for other to?

Thanks again Jenny for the great integration, work and support to the community!

@dannymertens dannymertens changed the title Template to show today, coming days birtdays, events NOT ISSUE: Template to show today, coming days birtdays, events Feb 27, 2021
@dannymertens dannymertens changed the title NOT ISSUE: Template to show today, coming days birtdays, events NOT AN ISSUE, just giving something back to the community: Template to show today, coming days birtdays, events Feb 27, 2021
@dimitrijp
Copy link

for some reason this doesn't want to work for me :(, I cant get the formatting for the template right. Can you maybe post full template code? Thank you.

@MPCatnip
Copy link

Thanks! I've modified it a bit to add comma's between the birthdays if there are multiple on one day. Will be using this to send a message if someone has a birthday.

{% set count = namespace(value=0) %} Hoera! {%- for state in states.sensor -%} {%- if state.attributes.attribution == "Sensor data calculated by Anniversaries Integration" and state.state | int <= 0 -%} {%- if count.value != 0 %},{% endif %} {{state.attributes.friendly_name}} word vandaag {{state.attributes.years_at_next_anniversary }} {%- set count.value = count.value + 1 -%} {%- endif -%} {%- endfor %}

The result: (in Dutch..)

Hoera! Name Lastname word vandaag 2, Name Lastname word vandaag 22, Name Lastname word vandaag 21

@rononline
Copy link

@MPCatnip If I try this to put in a lovelace template card, or in the Development Tools then it says: "UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'attribution'". Is this not working anymore? Or am I doing something wrong?

@woodmj74
Copy link

Try adding the selectattr filter on your for line:

{%- for state in states.sensor | selectattr('attributes.attribution','defined')-%}

I believe its failing because some sensors won't have an attribute of attribution assigned, therefore fails when these are evaluated. This way you remove those by only selecting ones with the attribution attribute so you can then evaluate them.

@rononline
Copy link

rononline commented Aug 23, 2022

Here is a template to show the birthdays for coming week and today, i put this on my frontpage. With help from a community member:

        {%- for item in expand(integration_entities('anniversaries')) %}
          {%- set d_h = item.attributes.get('days_until_half_anniversary', 999) %}
          {%- set h = d_h < item.state | int %}
          {%- set d = [item.state | int, d_h] | min %}
          {%- set y = item.attributes.get('current_years') %}
          {%- set y = y + (0.5 if h else 1 if d != 0 else 0) if y is not none %}
          {%- set ns.a = ns.a + [ dict(n=item.name, d=d, y=y, h=h)] %}
        {%- endfor %}
          {%- for item in ns.a | sort(attribute='d') -%}
          {%- set n, d, y, h = item.n, item.d, item.y, item.h %}
          {%- set fixed_icons = {
                                  'Eerste date': '💕',
                                  'Sinterklaas': '🎁'
                                } %}
          {%- set icons = ['🥳', '🎈', '🍰', '🎊', '🎉'] %}
          {%- set icon = fixed_icons[n] | default(icons | random, true) %}
          {%- if d == 0 %}
           {{ icon }} Vandaag: {{ n }} {%-if y %}({{ y }} jaar){%-endif %}
          {%- elif d < 7 %}
            {{ icon }} Over {{ d }} dag{{'en' if d > 1 }}: {{ n }} {%-if y %}({{ y }} jaar){%-endif %}
          {%- endif %}
        {%- endfor %}```

It's also sorting the output :-) 

@JonGilmore
Copy link

JonGilmore commented Sep 16, 2022

@rononline thanks for posting this up, any chance you've copy/pasted it with a mistake? When I paste it into the template window, I get TemplateRuntimeError: cannot assign attribute on non-namespace object

edit: hm, got it working with this:

{%- set ns = namespace(a=[]) %}
{%- for item in expand(integration_entities('anniversaries')) %}
  {%- set d_h = item.attributes.get('days_until_half_anniversary', 999) %}
  {%- set h = d_h < item.state | int %}
  {%- set d = [item.state | int, d_h] | min %}
  {%- set y = item.attributes.get('current_years') %}
  {%- set y = y + (0.5 if h else 1 if d != 0 else 0) if y is not none %} 
  {%- set ns.a = ns.a + [ dict(n=item.name, d=d, y=y, h=h)] %}
{%- endfor %}
{%- for item in ns.a | sort(attribute='d') -%}
  {%- set n, d, y, h = item.n, item.d, item.y, item.h %}
  {%- set fixed_icons = { 'First date': '💕', 'Christmas': '🎁' } %}
  {%- set icons = ['🥳', '🎈', '🍰', '🎊', '🎉'] %}
  {%- set icon = fixed_icons[n] | default(icons | random, true) %}
  {%- if d == 0 %} {{ icon }} Today: {{ n }}
    {%-if y %}({{ y }} year){%-endif %}
  {%- elif d < 45 %} {{ icon }} {{ d }} day{{'s' if d > 1 }}: {{ n }}
    {%-if y %}({{ y }} year){%-endif %}
  {%- endif %}
{%- endfor %}

@pinkywafer
Copy link
Owner

Thanks for these examples. I'm going to leave them here in case they may help someone else.

@CumpsD
Copy link

CumpsD commented May 7, 2023

Little addition, because calendar.anniversaries has state off which fails the for loop:

{%- set ns = namespace(a=[]) %}
{%- for item in expand(integration_entities('anniversaries')) | selectattr("state", 'is_number') %}
  {%- set d_h = item.attributes.get('days_until_half_anniversary', 999) %}
  {%- set h = d_h < item.state | int %}
  {%- set d = [item.state | int, d_h] | min %}
  {%- set y = item.attributes.get('current_years') %}
  {%- set y = y + (0.5 if h else 1 if d != 0 else 0) if y is not none %} 
  {%- set ns.a = ns.a + [ dict(n=item.name, d=d, y=y, h=h)] %}
{%- endfor %}
{%- for item in ns.a | sort(attribute='d') -%}
  {%- set n, d, y, h = item.n, item.d, item.y, item.h %}
  {%- set fixed_icons = { 'First date': '💕', 'Christmas': '🎁' } %}
  {%- set icons = ['🥳', '🎈', '🍰', '🎊', '🎉'] %}
  {%- set icon = fixed_icons[n] | default(icons | random, true) %}
  {%- if d == 0 %} {{ icon }} Today: {{ n }}
    {%-if y %}({{ y }} year){%-endif %}
  {%- elif d < 45 %} {{ icon }} {{ d }} day{{'s' if d > 1 }}: {{ n }}
    {%-if y %}({{ y }} year){%-endif %}
  {%- endif %}
{%- endfor %}

@bedbug1226
Copy link

bedbug1226 commented Oct 27, 2023

My slight edit.
I run it every day at 8:30 and just wanted to know the holidays coming up in the next week and not to send a message otherwise.

    {%- set ns = namespace(a=[]) %}
    {%- for item in expand(integration_entities('anniversaries')) |
    selectattr("state", 'is_number') %}
      {%- set d_h = item.attributes.get('days_until_half_anniversary', 999) %}
      {%- set h = d_h < item.state | int %}
      {%- set d = [item.state | int, d_h] | min %}
      {%- set y = item.attributes.get('current_years') %}
      {%- set y = y + (0.5 if h else 1 if d != 0 else 0) if y is not none %} 
      {%- set ns.a = ns.a + [ dict(n=item.name, d=d, y=y, h=h)] %}
    {%- endfor %}

    {%- for item in ns.a | sort(attribute='d') -%}
      {%- set n, d, y, h = item.n, item.d, item.y, item.h %}
      {%- set fixed_icons = { 'First date': '💕', 'Christmas': '🎁' } %}
      {%- set icons = ['🥳', '🎈', '🍰', '🎊', '🎉'] %}
      {%- set icon = fixed_icons[n] | default(icons | random, true) %}
      {%- if d == 0 %}{{ icon }} Today : {{ n }}
        {%-if y %}({{ y }} year){%-endif %}{{"\n"}}
      {%- elif d < 8 %}{{ icon }} {{ d }} day{{'s' if d > 1 }} until: {{ n }}
        {%-if y %}({{ y }} year){%-endif %}{{"\n"}}
      {%- endif %}
    {%- endfor %}

@freddeh
Copy link

freddeh commented Dec 20, 2023

In case anyone needs it, I built myself a sensor to define todays anniversary to use in an automation(to select right music) based on the above code;

  - sensor:
      - name: "Anniversaries Today"
        unique_id: anniversaries_today
        state: "{{expand(integration_entities('anniversaries')) | selectattr(\"state\", 'is_number') | selectattr(\"state\", 'match', '0') | map(attribute='name') | list | join(\", \")  }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants