Roel11
(Roel11)
May 13, 2019, 5:10pm
1
Following this guideline ( Template sensor not updating ) , I hoped this would solve my issue but I can’t seem to get it working.
I use a grabage collection sensor and use a ‘days remaining’ tile on tileboard;
days_until_recycling_plastic:
friendly_name: 'Plastic'
unit_of_measurement: 'days'
entity_id: sensor.rova_plastic
value_template: >-
{% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
{% set todays_date = (now().strftime("%s") | int ) %}
{% if todays_date > due_date %}
{% set countdown = "?" %}
{% else %}
{% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
{% endif %}
{{countdown}}
As expected , in dev-template it works well, however, it doesn’t change sensor.days_until_recycling_plastic.
Appreciate your insights !
123
(Taras)
May 13, 2019, 5:19pm
2
To understand how this works, we will need to see the configuration for sensor.rova_plastic
Roel11
(Roel11)
May 13, 2019, 5:29pm
3
I use following sensor:
- platform: rova
zip_code: 1234 AB
house_number: 1
house_number_suffix: b
name: Rova
monitored_conditions:
- bio
- paper
- plastic
- residual
This yields following states which are all correct.
sensor.rova_bio
2019-05-21T00:00:00
sensor.rova_paper
2019-05-14T00:00:00
sensor.rova_plastic
2019-05-28T00:00:00
sensor.rova_residual
unknown
and
sensor.date 2019-05-13
Hope this is what you are requesting
123
(Taras)
May 13, 2019, 5:35pm
4
Double-check the indenting.
days_until_recycling_plastic:
friendly_name: 'Plastic'
unit_of_measurement: 'days'
entity_id: sensor.rova_plastic
value_template: >-
{% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
{% set todays_date = (now().strftime("%s") | int ) %}
{% if todays_date > due_date %}
{% set countdown = "?" %}
{% else %}
{% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
{% endif %}
{{countdown}}
petro
(Petro)
May 13, 2019, 5:35pm
5
Try this, it’s not updating every day because there is nothing to update off of. So, you need to add the date time platform into your sensor section, then use that in the template.
This should update every night at midnight.
sensor:
- platform: time_date
display_options:
- 'date'
- platform: template
sensors:
days_until_recycling_plastic:
friendly_name: 'Plastic'
entity_id: sensor.date, sensor.rova_plastic
unit_of_measurement: 'days'
value_template: >-
{% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
{% set todays_date = (now().strftime("%s") | int ) %}
{% if todays_date > due_date %}
{% set countdown = "?" %}
{% else %}
{% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
{% endif %}
{{countdown}}
Roel11
(Roel11)
May 13, 2019, 5:56pm
6
Thanks @petro , i’ll give that a go and check back tomorrow
much appreciated.
Roel11
(Roel11)
May 13, 2019, 6:04pm
7
@petro , seems I can’t have duplicate entity_id in the template, so I replace
entity_id: sensor.rova_plastic
by
entity_id: sensor.date
?
petro
(Petro)
May 13, 2019, 6:06pm
8
Ah yes, never noticed that.
I’ll update the code
Roel11
(Roel11)
May 15, 2019, 11:53am
10
@petro , it seems it still doesn’t update over midnight, just checked again and adding sensor.date to entity_id didn’t have an affect.
sensor.date displays the correct date
Anything else I can look for , I’m out of ideas/talent
petro
(Petro)
May 15, 2019, 12:23pm
11
can you take a screenshot of this entity in your states page?
sensor.rova_plastic
petro
(Petro)
May 15, 2019, 3:37pm
13
I believe your calculation is off. If you restarted yesterday after noon, the calculation would remain the same until noon today. It’s because you are rounding. Every half day (12 hours) it rounds down or rounds up. It will change tomorrow. This calculation should count properly all the time, regardless of the time of day. It considers today as a full day.
sensor:
- platform: time_date
display_options:
- 'date'
- platform: template
sensors:
days_until_recycling_plastic:
friendly_name: 'Plastic'
entity_id: sensor.date, sensor.rova_plastic
unit_of_measurement: 'days'
value_template: >-
{% set dstring = states('sensor.rova_plastic') %}
{% set day = now().utcnow().replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
{% set due = strptime(dstring, '%Y-%m-%dT%H:%M:%S') %}
{{ (due - day).days }}
Roel11
(Roel11)
May 15, 2019, 7:20pm
14
Wow, many thanks @petro , I’m still trying to get my head around this date calculation and this goes beyond my knowledge.
I’ll try and report back tomorrow.
Appreciate your support so far !
Roel11
(Roel11)
May 16, 2019, 3:13pm
15
It’s hard for me to grasp what is going wrong, trying hard to learn but if logic fails to me
Here is the output of the garbage collector sensors:
running the config in dev template I get the correct number of days: 12
however, the sensor still reports yesterday’s state:
Somehow date change doesn’t trigger a state update.
I have a script running now to restart HASS overnight but there must be some other way to do this.
Eager to learn and understand
petro
(Petro)
May 16, 2019, 3:40pm
16
Is your date set properly in your system? It’s possible that the date is changing on utc time which would make the calculation not work.
Roel11
(Roel11)
May 16, 2019, 4:57pm
17
Yes it is, also date.sensor is correct in HASS
Our Cli:
$ hassio help
debug1: permanently_set_uid: 0/0
core-ssh:~# date
Thu May 16 18:54:30 CEST 2019
core-ssh:~#
petro
(Petro)
May 16, 2019, 5:00pm
18
What about in your configuration.yaml?
Roel11
(Roel11)
May 16, 2019, 5:36pm
19
Timezone is set to Europe/Amsterdam
Home location is also defined and correct
petro
(Petro)
May 16, 2019, 5:38pm
20
Well I’m stumped then. I have no idea why it’s not updating. Tomorrow, don’t restart your system and check to see when the sensor last changed or last updated. I’m wondering if it’s triggering slightly before midnight.