Help with nightly update

Hey all,
i have a process that runs every 5 days, to find out my local waste disposal days.
eg. Recycling is due on 20th June 2019.
AS part of this, i have a template, that calculates how many days away that is.
i want to run that calculation every night, so that the days update, but i cant work out.
i can only get this to change whenever i do a reboot :frowning:

config.yaml

  - platform: scrape 
    name: worthing_garden_bin_next
    resource: https://www.adur-worthing.gov.uk/bin-day/?brlu-selected-address=100061882207&return-url=%2Fbin-day%2F
    select: 'tr:nth-child(3) td:nth-child(3)' 
    scan_interval: 432000 #once per 5days
    value_template: "{{ value.split('2019')[1] + '2019' }}"

  - platform: template
    sensors:
      garden_bin_in_days:
        friendly_name: garden due
        value_template: >-
          {% set date_in = states.sensor.worthing_garden_bin.state %}
          {% set bin = strptime((date_in), "%A %d %b %Y") %}
          {% set diff = as_timestamp(bin) - as_timestamp(now()) %}
          {% set days = ((diff / 86400)+1) | int %}
          {% if days == 0 %}
            Today
          {% elif days == 1 %}
          Tomorrow
          {% else %}
          {{ days }} days
          {% endif %}

automations.yaml

- id: update_gardening_due
  alias: "Update gardening Due every 1 miniute before/after midnight"
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      minutes: '/1'
  condition:
    - condition: time
      after: '23:55:00'
      before: '00:05:00'
  action:
    - service: homeassistant.update_entity
      entity_id: sensor.garden_bin_in_days

You need to add an entity for HA to monitor for changes. The date sensor will update every 24 hours and would be a good choice:

  - platform: template
    sensors:
      garden_bin_in_days:
        friendly_name: garden due
        entity_id: sensor.date # <---template updates when this sensor changes.
        value_template: >-
          {% set date_in = states.sensor.worthing_garden_bin.state %}
          {% set bin = strptime((date_in), "%A %d %b %Y") %}
          {% set diff = as_timestamp(bin) - as_timestamp(now()) %}
          {% set days = ((diff / 86400)+1) | int %}
          {% if days == 0 %}
            Today
          {% elif days == 1 %}
          Tomorrow
          {% else %}
          {{ days }} days
          {% endif %}

Assuming you have a date sensor

awesome, thanks a lot!

so is that the only change i need? can i now remove the automation?
i dont quite understand how it knows to update every 24hours however, sorry

The date sensor updates every 24 hours at midnight - when the date changes from one day to the next,

We have told HA to monitor this sensor for changes by including it in your template sensor config as an entity_id. When HA sees the the state of this sensor change it re-evaluates your template.

thats great, thanks so much!!
i can remove out my automations then - as these are redundant.

as a follow up, if i wanted to update this twice per day or hourly, for example, how would that work?

I can’t think of any built in sensors that change twice per day or hourly. You would probably have to create template sensors and use them. To get the template sensors to update you would probably have to use sensor.time (which updates every minute). So you might as well update the original template every minute by using sensor.time instead of sensor.date.

1 Like

thanks a lot!