amaximus / garbage-collection-card

Custom Lovelace card for Garbage Collection custom component
MIT License
128 stars 24 forks source link

Today/Tomorrow not shown on Card #28

Closed achilleus68 closed 4 years ago

achilleus68 commented 4 years ago

After updating from 1.4.0 to 1.5.0 the state is shown correctly, until collection day is tomorrow/today, then it just shows 'undefined'

image

This is the code in my ui-lovelace.yaml

 - type: custom:garbage-collection-card
          entity: sensor.date_pmd
          icon_size: 35px
          icon_color: green
          hide_date: false
          hide_before: 4

and this is my sensor template

  sensors:
    date_pmd:
      friendly_name: 'PMD'
      value_template: >-
        wordt {% if ((  as_timestamp(strptime(state_attr("sensor.afval_pmd","date"), "%Y-%m-%d")) 
          - as_timestamp(now()) ) / 86400 ) | round(0,"ceil") < 1 %}
          vandaag  
        {%- elif ((  as_timestamp(strptime(state_attr("sensor.afval_pmd","date"), "%Y-%m-%d")) 
          - as_timestamp(now()) ) / 86400 ) | round(0,"ceil") < 2 -%}
          morgen
        {%- else -%} over {{ ((  as_timestamp(strptime(state_attr("sensor.afval_pmd","date"), "%Y-%m-%d")) 
          - as_timestamp(now()) ) / 86400 ) | round(0,"ceil") }} 
          dagen 
        {%- endif %} geleegd.
      attribute_templates:
        next_date: >-
          {{ state_attr("sensor.afval_pmd","date") }}
        verbose_state: >-
          'true'
        days: >-
          {{ ((  as_timestamp(strptime(state_attr("sensor.afval_pmd","date"), "%Y-%m-%d")) - as_timestamp(now()) ) / 86400 ) | round(0,"ceil") }}

This is the state of the sensor image

And there is an error in the logfile,

2020-03-17 19:45:32 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.date_pmd fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 281, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 459, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 224, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 222, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 440, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 814, in strptime
    return datetime.strptime(string, fmt)
TypeError: strptime() argument 1 must be str, not None
amaximus commented 4 years ago

Please note that you have issues with the strptime conversion used by the value template (as the stack trace showed). It has nothing to do with the custom card. The card simply checks the state value and if it's not a number will presume verbose_state=true (of the sensor of garbage-collection custom component) and will simply display that string. In this case the state of date_pmd cannot be calculated, throwing error in the log.

I'd check the state of sensor.afval_pmd as that seems unknown.

achilleus68 commented 4 years ago

Thanks for your quick response. I've been playing with the States tool and found the following.

The current settings of sensor.date_pmd are:

next_date: '2020-03-18'
verbose_state: '''true'''
days: '1'
friendly_name: PMD
icon: 'mdi:recycle'
state: 'wordt morgen geleegd'

This results in the following card image

As you can see, the state is shown as undefined

When I change only 'days' in the settings, like

next_date: '2020-03-18'
verbose_state: '''true'''
days: '2'
friendly_name: PMD
icon: 'mdi:recycle'
state: 'wordt morgen geleegd'

it results in the following card image

So now the state is shown as defined

Apparently when 'days=1' the card does not display the State properly.

amaximus commented 4 years ago

Reproduced the fault. Added a fix in the newest release. Please update the card (once it becomes available via HACS) and get back to me with results.

achilleus68 commented 4 years ago

Hi, thanks again for your quick response.

I updated to 1.5.1 image

~But the results are the same.~ After multiple(!) restarts of HA, it seems to be working OK As of today, days = 0, state is shown as expected

Another sensor now has days=2 and is shown correctly. I will monitor what happens tomorrow

achilleus68 commented 4 years ago

Update: In the HA app (not Ariela but https://play.google.com/store/apps/details?id=io.homeassistant.companion.android) it is still shown as undefined, maybe for mobile a different view is used?

amaximus commented 4 years ago

Maybe some sort of cache refresh would be needed for that application (I do not know how it updates the states of sensors).

On more thing I forgot to mention: the card doesn't use verbose_state (as that's an attribute of the garbage-collection custom component sensors), so you could get rid of it in the attribute_templates above.

achilleus68 commented 4 years ago

Ok, everything is working fine, both in web interface and App. Thanks