Help with birthday python script & templates

right. I think the main issue in the config is the fact several important sensors in my setup are created by python scripts at startup, and HA creates template sensors based on those sensors. HA tries to do so before they are created.
Can’t proof it yet, but the reported issue on the date countdown script certainly seems to indicate that. At least they throw many errors saying so.

If that’s the case, add availability_templates to all those that work off python scripts.

availability_template: >
  {{ not states('sensor.xxx') in ['unknown','unavailable'] }}

And then build checking the states into the value_template.

1 Like

I have never used that before, sounds to good to be true. let me give 1 example:

      birthday_marijn:
        friendly_name: Marijn jarig
#        entity_id: sensor.birthday_marijn
        value_template: >-
          {{is_state('sensor.birthday_marijn','0')}}
        device_class: presence
        entity_picture_template: >
          {{state_attr('sensor.birthday_marijn','entity_picture')}}
        attribute_templates:
          days: >
            {{states('sensor.birthday_marijn')}}
          years: >
            {{state_attr('sensor.birthday_marijn','years')}}
          original_date: >
            {{state_attr('sensor.birthday_marijn','original_date')}}
          type: >
            {{state_attr('sensor.birthday_marijn','type')}}
          name: >
            {{state_attr('sensor.birthday_marijn','persoon')}}
          event: "{{'jarig'}}"

would only need

availability_template: >
  {{ not states('sensor.birthday_marijn') in ['unknown','unavailable'] }}

or maybe even

availability_template: >
  {{states('sensor.birthday_marijn') is not none}}

wish I had realized that was available…thanks!

I don’t know when it was added, but I noticed it around the time coronavirus hit because that’s the last time I implemented a sensor. So it’s been here at least 6 months.

ok well, I’ve added it, and will restart and am crossing fingers. Geez cant believe Ive missed that before, it would really make my day… that, and the fix Amelchio wrote, since I wasn’t the only one apparently :wink:

Meanwhile, might I ask you to have a look here, if you can find a spare moment… no hurries, need some list formatting skills in Python…

meanwhile:

2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_redacted.attributes.type}}: {{states.sensor.birthday_redacted.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_marijn.attributes.type}}: {{states.sensor.birthday_marijn.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.met_lovebirds.attributes.type}}: {{states.sensor.met_lovebirds.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.wedding_lovebirds.attributes.type}}: {{states.sensor.wedding_lovebirds.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_w.attributes.type}}: {{states.sensor.birthday_w.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_h.attributes.type}}: {{states.sensor.birthday_h.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_d.attributes.type}}: {{states.sensor.birthday_d.attributes.original_date}}
")'
2020-09-22 14:57:06 ERROR (MainThread) [homeassistant.components.websocket_api.commands] TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.birthday_g.attributes.type}}: {{states.sensor.birthday_g.attributes.original_date}}
")'
2020-09-22 14:57:10 WARNING (MainThread) [homeassistant.setup] Setup of browser_mod is taking over 10 seconds.
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')
2020-09-22 14:57:18 ERROR (MainThread) [frontend.js.latest.202009180] redacted/local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')

will try another template too, and if not, wait for the fix in the next update.

Yes, as I said in the previous post…

{% if states('sensor.xxx') not in ['unknown', 'unavailalbe'] %}
  ...

sorry, I misunderstood/stand/am confused.
Would have hoped the availability template would prevent the full template sensor from building.
What use is it, if we still have to use that template in all template options individually on each template sensor?

All the templates still get resolved so you have to fix the errors. But now you don’t need to add ‘else’ statements when there’s nothing to calculate. So simply wrap your whole template with that if statement.

let me try and understand:
old, with availability template:

      birthday_marijn:
        friendly_name: Marijn jarig
#        entity_id: sensor.birthday_marijn
        value_template: >-
          {{is_state('sensor.birthday_marijn','0')}}
        availability_template: >
          {{states('sensor.birthday_marijn') is not none}}
        device_class: presence
        entity_picture_template: >
          {{state_attr('sensor.birthday_marijn','entity_picture')}}
        attribute_templates:
          days: >
            {{states('sensor.birthday_marijn')}}
          years: >
            {{state_attr('sensor.birthday_marijn','years')}}
          original_date: >
            {{state_attr('sensor.birthday_marijn','original_date')}}
          type: >
            {{state_attr('sensor.birthday_marijn','type')}}
          name: >
            {{state_attr('sensor.birthday_marijn','persoon')}}
          event: "{{'jarig'}}"

new, with availability_template and wrapping all other templates:

      birthday_marijn:
        friendly_name: Marijn jarig
#        entity_id: sensor.birthday_marijn
        value_template: >-
          {% if states('sensor.birthday_marijn') is not none %}
          {{is_state('sensor.birthday_marijn','0')}}
        availability_template: >
          {{states('sensor.birthday_marijn') is not none}}
        entity_picture_template: >
          {% if states('sensor.birthday_marijn') is not none %}
          {{state_attr('sensor.birthday_marijn','entity_picture')}}
        attribute_templates:
          days: >
          {% if states('sensor.birthday_marijn') is not none %}
            {{states('sensor.birthday_marijn')}}
          years: >
          {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','years')}}
          original_date: >
          {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','original_date')}}
          type: >
          {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','type')}}
          name: >
          {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','persoon')}}
          event: "{{'jarig'}}"
        device_class: presence

like that?

yep but you’re missing the entire syntax needed for if statements. {% if… %} {% endif %}

1 Like

right, missed that… thanks
what a way load of code for such a small template sensor, that never threw any issues whatsoever…

      birthday_marijn:
        availability_template: >
          {{states('sensor.birthday_marijn') is not none}}
        friendly_name: Marijn jarig
        value_template: >-
          {% if states('sensor.birthday_marijn') is not none %}
          {{is_state('sensor.birthday_marijn','0')}}
          {% endif %}
        entity_picture_template: >
          {% if states('sensor.birthday_marijn') is not none %}
          {{state_attr('sensor.birthday_marijn','entity_picture')}}
          {% endif %}
        attribute_templates:
          days: >
            {% if states('sensor.birthday_marijn') is not none %}
            {{states('sensor.birthday_marijn')}}
            {% endif %}
          years: >
            {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','years')}}
            {% endif %}
          original_date: >
            {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','original_date')}}
            {% endif %}
          type: >
            {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','type')}}
            {% endif %}
          name: >
            {% if states('sensor.birthday_marijn') is not none %}
            {{state_attr('sensor.birthday_marijn','persoon')}}
            {% endif %}
          event: "{{'jarig'}}"
        device_class: presence

I just noticed the ridiculousness of this template. Almost every field is referencing itself. What’s the point of this when the python script can create it as is?

change those to check for ‘unknown’ or ‘unavailable’

with correct spelling though. Unless it’s returning None. I can’t remember this anymore.

well, now I am confused again… but its like this:

the python does create these attributes for a regular sensor:

hass.states.set(sensorName,numberOfDays,
  {
    'entity_picture': '/local/family/{}.jpg'.format(name.lower()),
    'unit_of_measurement': phrase,
    'friendly_name': '{} turning {} in:'.format(name, years),
    'persoon': name,
    'type': '{}'.format(type.title()),
    'years': years,
    'original_date' : '{}-{}-{}'.format(dateDay,dateMonth,dateYear)
  }

a template-entity-row card uses these attributes:
via the decluttering card

card:
  type: custom:template-entity-row
  entity: '[[entity]]'
  state: >
    {{states(config.entity)}} {{state_attr(config.entity, 'unit_of_measurement')}}
  secondary: >
    {{states.[[entity]].attributes.type}}: {{states.[[entity]].attributes.original_date}}

in the view:

  - type: custom:decluttering-card
    template: event_date
    variables:
      - entity: sensor.birthday_marijn

the template sensor with all attributes sensors is actually for a binary_sensor… being ‘on’ when the regular sensor (made in the python script) has state ‘0’.

because I like to show these attributes in the more-info of that binary, it references all these sensors attributes.
so I have a sensor.birthday_marijn, and a binary_sensor.birthday_marijn.

maybe I should add that to the python…?

yep

binarysensorName = 'binary_sensor.' + sensoName.split('.')[-1]
hass.states.set(binarysensorName, numberOfDays == 0, {})

@Mariusthvdb
As fascinating as this may be, can you fork this into another thread? It is only vaguely related to this topic (deprecation of entity_id).

I have a JSON file with all the Birthday’s and use a command_line sensor to show them.
Is that maybe a option?

{
  "json_calendars": {
    "birthday": {
      "2/23":"Henriette",
      "3/18":"Martine",
      "4/8":"Remon",
      "4/29":"Dennis",
      "5/11":"Peter",
      "6/22":"Tima",
      "7/6" :"Menno",
      "7/12":"Han",
      "8/19":"Kay",
      "9/14":"Home Assistant",
      "12/12":"Erik"
    },
    "holiday": {
      "1/1":"New Year",
      "2/14":"Valentine's Day",
      "4/1":"April Fool's Day",
      "4/24":"Kingsday",
      "7/4":"Independence Day",
      "12/1":"World Aids Day",
      "12/25":"Christmas",
      "12/26":"Christmas",
      "12/31":"New Year's Eve"
    }
  }
}

Birthday’s

###########################################################################################
# SENSOR COMMAND_LINE
# https://www.home-assistant.io/integrations/sensor.command_line/
###########################################################################################
---
platform: command_line
name: "Birthday"
command: "cat /config/package/json/calendar.json"
scan_interval: 21600
value_template: >-
  {% set today = now().month  ~ '/' ~ now().day  %}
  {% set birthday =  value_json.json_calendars.birthday[ today ] %}
    {% if birthday | trim == "" %}
         {% set birthday =  "No Birthday today" %}
       {% endif %}
  {{ birthday }}

Holiday’s:

###########################################################################################
# SENSOR COMMAND_LINE
# https://www.home-assistant.io/integrations/sensor.command_line/
###########################################################################################
---
platform: command_line
name: "Holiday"
command: "cat /config/package/json/calendar.json"
scan_interval: 21600
value_template: >-
  {% set today = now().month  ~ '/' ~ now().day  %}
  {% set holiday =  value_json.json_calendars.holiday[ today ] %}
    {% if holiday | trim == "" %}
         {% set holiday =  "Nothing" %}
       {% endif %}
  {{ holiday }}

so I have added that like this, to have the binary_sensors have a state ‘on’ or ‘off’ and add the attributes.

However, I cant get it to show in the frontend.
give the large amounts of this:

local/lovelace/resources/lovelace-template-entity-row/template-entity-row.js?v=1.1.0:1:2530 TypeError: null is not an object (evaluating 'i.replace')

still in the logs, added to

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 287, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'attributes'
2020-09-22 16:09:47 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{states.sensor.birthday_marijn.attributes.type}}: {{states.sensor.birthday_marijn.attributes.original_date}}

whihc, if you read the last line Must be coming from the template-entity-row card, because I dont have a jinja template like that in the config.

the declutering-card uses:

  state: >
    {{states(config.entity)}} {{state_attr(config.entity, 'unit_of_measurement')}}
  secondary: >
    {{states.[[entity]].attributes.type}}: {{states.[[entity]].attributes.original_date}}

which is the source for this error.
Could I write the secondary template here in the states() form, to prevent that error?

maybe like

  secondary: >
    {{state_attr.([[entity]],'type')}}: {{state_attr.([[entity]],'original_date')}}

?
edit

nope, it was:

  secondary: >
    {{state_attr('[[entity]]','type')}}: {{state_attr('[[entity]]','original_date')}}

learned a few things today… thanks Petro. you’ve made me cut 300 plus lines of complicated templates…

restarting now if these changes make the startup error go away…

this has been most enlightening.

The errors wouldn’t go away, and even increased… oddly enough the feedback from evaluating these templates in the template-entity-row card changed during a very long startup time.

startup took more than 10 minutes, after which several other integrations had given up too (Ikea, Weatherbit)

taking out that single card with the template entity rows causes the errors to go away (obviously) but also to make the startup process a lot quicker, in about 4 minutes. Not yet as before, but much better indeed.

That is with all sensors made by Python scripts in place, including the ones discussed in this topic.

So, seems I’ve ran into an issue with this specific custom card, which is either frustrated by the new templating engine in 115, or causing issues in that. chicken or egg…

that the yaml code itself is correct, is proven by taking out the comments for that single card, and refreshen Lovelace, which makes is appear in all its glory, with all sensors and all attributes in place.