Sensor.date not updating

Hi,

I was trying to create new sensor with current day of the week and then use it in some automations/scripts or UI.

This is my sensor configuration

        day_of_the_week:
            entity_id: sensor.date
            friendly_name: "Day of the week"
            value_template: >
              {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()] }}

Unfortunatelly it looks like it’s not updating daily. It is read once during startup and then nothing happens. So today it’s set to Tuesday even it’s Wednesday already.

Can anyone help me with this one?

Is the sensor.date not updating or only the template sensor?

How can I check that?

Go to Dev Tools/states and look for the sensors.

Have you got sensor.date in yours? I don’t. This is some prebuilt sensor:

Well, your template sensor depends on the sensor.date.
If you don’t have that sensor, you need to add it to HA.

2 Likes

I’ll give it a try. Result will be visible tomorrow… or not :slight_smile:

It’s part of the Time and Date component and you have to define it in your configuration file (or sensor.yaml file) before you can use it in template sensors, automations, etc.

Minimally, you need to add:

sensor:
  - platform: time_date
    display_options:
      - 'date'
1 Like

Thanks. I fell for the same trap, assumed it is in-built and always available. Maybe it even was in the past, I had no problem a few months ago.

Any idea how to make that clearer in the documentation here?

I can’t think of anything in the sensor domain that’s in-built. If you’re assigning something to entity_id it must be an entity. Entities are defined manually or through discovery.

I have a similar sensor on my config - but it looks like it stopped updating a couple of days ago. sensor.date and sensor.time both seem to be updating fine, but sensor.dayoftheweek, sensor.dayoftheweek_tomorrow and sensor.isfirstthursday are not :confused: Any ideas please?

#----- Day of the week sensor
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
  - platform: template
    sensors:
      dayoftheweek:
        value_template: "{{ ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'][now().weekday()] }}"
        entity_id: sensor.time
      dayoftheweek_tomorrow:
        value_template: "{{ ['tuesday','wednesday','thursday','friday','saturday','sunday','monday'][now().weekday()] }}"
        entity_id: sensor.time
      isfirstthursday: 
        value_template: '{{(now().strftime("%d")|int>=1) and (now().strftime("%d")|int <=7) and (now().strftime("%w")|int==4)}}'
        entity_id: sensor.time

entity_id as part of a template sensor is deprecated in 0.115.x. You need to remove them.
For solutions, see this topic :

2 Likes

Thank you so much - I’d missed that breaking change and the post you shared is super helpful.
Looks like this fixed the issue now. My new code looks like this:

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
  - platform: template
    sensors:
      dayoftheweek:
        value_template: >
          {% set x = states('sensor.time') %}
          {{ ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'][now().weekday()] }}
      dayoftheweek_tomorrow:
        value_template: >
          {% set x = states('sensor.time') %} 
          {{ ['tuesday','wednesday','thursday','friday','saturday','sunday','monday'][now().weekday()] }}
      isfirstthursday: 
        value_template: >
          {% set x = states('sensor.time') %} 
          {{(now().strftime("%d")|int>=1) and (now().strftime("%d")|int <=7) and (now().strftime("%w")|int==4)}}
1 Like

You can streamline the templates like this:

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
  - platform: template
    sensors:
      dayoftheweek:
        value_template: >
          {% set x = states('sensor.time') %}
          {{ now().strftime("%A") | lower }}
      dayoftheweek_tomorrow:
        value_template: >
          {% set x = states('sensor.time') %} 
          {{ ['tuesday','wednesday','thursday','friday','saturday','sunday','monday'][now().weekday()] }}
      isfirstthursday: 
        value_template: >
          {% set x = states('sensor.time') %} 
          {{ now().weekday() == 4 and (1 <= now().day <= 7) }}

In a future release, there will be a timedelta() function and then you can reduce the second Template Sensor to this:

      dayoftheweek_tomorrow:
        value_template: >
          {% set x = states('sensor.time') %} 
          {{ (now() + timedelta(days=1)).strftime("%A") | lower }}
1 Like

Thanks for your suggestion - I tried it out and I can confirm it works. However can you explain this part of the code for me as I’m not sure why/how it works?

{{ now().strftime("%A") | lower }}

https://strftime.org

Code Meaning Example
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday

all clear now…thanks a lot!!