I’m trying to create a trash pickup schedule that will look like the picture below.
I live in sweden, and here we can choose the weekly pickup interval. We also have separate pickup for combustible garbage and food waste.
In my situation, they pickup combustible garbage every 4 weeks and food wasteevery 2 weeks.
I started with a couple of datetime inputs to hold the next pickup date for each type of garbage.
input_datetime:
garbage_burn_date:
has_date: true
has_time: false
initial: '2020-02-27'
garbage_food_date:
has_date: true
has_time: false
initial: '2020-02-27'
Next I created a couple of template sensors that will calculate how many days left. It shows “Today”, “Tomorrow” or the # of days.
# Template sensors
sensor:
- platform: template
sensors:
garbage_burn:
friendly_name: 'Brännbart'
icon_template: mdi:trash-can-outline
value_template: >
{% set datediff = (((states.input_datetime.garbage_burn_date.attributes.timestamp - as_timestamp(now())) / 24 / 60 / 60) | round(0,'ceil')) %}
{% if datediff == 0 %}
Idag
{% elif datediff == 1 %}
Imorgon
{% elif datediff < 0 %}
Datum passerat
{% else %}
{{datediff}} dagar
{% endif %}
entity_id: input_datetime.garbage_burn_date,sensor.time
- platform: template
sensors:
garbage_compost:
friendly_name: 'Kompost'
icon_template: mdi:trash-can
value_template: >
{% set datediff = (((states.input_datetime.garbage_food_date.attributes.timestamp - as_timestamp(now())) / 24 / 60 / 60) | round(0,'ceil')) %}
{% if datediff == 0 %}
Idag
{% elif datediff == 1 %}
Imorgon
{% elif datediff < 0 %}
Datum passerat
{% else %}
{{datediff}} dagar
{% endif %}
entity_id: input_datetime.garbage_food_date,sensor.date
This works just as it should. However, once the date has passed, it needs to be updated. I created a automotation that will update the date few minutes after midnight each day if the date has passed.
This will work, however, after a reboot it will reset back to the initial date and will need X amount of days to catch up again.
So I changed to a template trigger that will trigger the automation if the date has passed, but I cannot get it to fire. The actual automation works if you trigger it manually.
I previously used now()
in the value_template
to calculate the date differance but I read that it wont fire, so I changed to sensor.date
. But still wont work, perhaps I used it wrong?
automation:
- alias: 'Garbage Update Date: Brännbart'
trigger:
- platform: template
value_template: (((states.input_datetime.garbage_burn_date.attributes.timestamp - as_timestamp(strptime(states('sensor.date'),'%Y-%m-%d'))) / 24 / 60 / 60) | round(0,'ceil')) < 0
condition: []
action:
entity_id: input_datetime.garbage_burn_date
service: input_datetime.set_datetime
data_template:
date: '{{ ((states.input_datetime.garbage_burn_date.attributes.timestamp + 2419200) | timestamp_custom("%Y-%m-%d", true)) }}'
- alias: 'Garbage Update Date: Kompost'
trigger:
- platform: template
value_template: (((states.input_datetime.garbage_food_date.attributes.timestamp - as_timestamp(strptime(states('sensor.date'),'%Y-%m-%d'))) / 24 / 60 / 60) | round(0,'ceil')) < 0
condition: []
action:
entity_id: input_datetime.garbage_food_date
service: input_datetime.set_datetime
data_template:
date: '{{((states.input_datetime.garbage_food_date.attributes.timestamp + 1209600) | timestamp_custom("%Y-%m-%d", true))}}'