Undefined variables in templates

Hi

I’m gettings these errors

  • Template variable warning: ‘None’ has no attribute ‘state’ when rendering ‘{{ states.sensor.date_short.state }}’
  • Template variable warning: ‘None’ has no attribute ‘state’ when rendering ‘{{ states.sensor.temperature.state }}°C’
  • Template variable warning: ‘None’ has no attribute ‘state’ when rendering ‘{{ states.sensor.time.state }} {{ states.sensor.date_long.state }}’
  • Template variable warning: ‘dict object’ has no attribute ‘timestamp’ when rendering ‘{{ value_json.timestamp }}’

I know this relates to 2021.4: For our advanced users ❤️ - Home Assistant

However i’m not sure how to fix it?

Do those entities show up in Dev->States? You should use states('sensor.date_short') instead to better handle unavailable entities (see this), but it looks like you have specific problems with specific entities. Where are sensor.date_short, sensor.date_long, and sensor.temperature defined?

Maybe you lost this from your config at some point?

Hi
Thx for your reply
Yes those entities show up.

They are defined under sensors:

sensor:
  - platform: time_date
    display_options:
      - time
      - date
      - date_time
  - platform: template
    sensors:
      date_long: 
        friendly_name: 'Datum'
        value_template: >
          {% set months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "July", "Augustus", "September", "Oktober", "November", "December"] %}
          {% set month = months[now().strftime('%m') | int -1] %}  
          {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}
        icon_template: mdi:calendar-clock    
  - platform: template
    sensors:
      date_short: 
        friendly_name: 'Datum Kort'
        value_template: >
          {% set months = ["Jan", "Feb", "Maa", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"] %}
          {% set month = months[now().strftime('%m') | int -1] %}  
          {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}
        icon_template: mdi:calendar-clock          
  - platform: template
    sensors: 
      temperature: 
        unit_of_measurement: °C
        value_template: "{{ state_attr('weather.thuis', 'temperature') }}"

Somehow those entities don’t exist at the time when you’re referencing them. Can you share the script or automation where you’re using the templates in the first post? The sensor definitions work fine for me, but at least the date/time ones rely on now() with no other entities referenced, so they’ll only update every minute.

If you’re interested, the Template Sensors can be defined like this:

sensor:
  - platform: time_date
    display_options:
      - time
      - date
      - date_time
  - platform: template
    sensors:
      date_long: 
        friendly_name: 'Datum'
        value_template: >
          {% set month = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "July", "Augustus", "September", "Oktober", "November", "December"][now().month-1] %}
          {{ now().day }} {{ month }} {{ now().year }}
        icon_template: mdi:calendar-clock    
      date_short: 
        friendly_name: 'Datum Kort'
        value_template: >
          {% set d = states('sensor.date_long').split() %}
          {{ d[0] }} {{ d[1][:3] }} {{ d[2] }}
        icon_template: mdi:calendar-clock          
      temperature: 
        unit_of_measurement: °C
        value_template: "{{ state_attr('weather.thuis', 'temperature') }}"

It may not eliminate the warning messages you are getting (I cannot replicate your results) but it’ll streamline your Template Sensor configuration.

Thx, cleared it up

I dont think its used in an automation or script
I only use it with markdown to show in the frontend.

For example
cards:
- type: markdown
content: |
{{ states.sensor.date_short.state }}

The same for date long, temperature…

Glad to hear it helped to fix the problem.

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a check-mark below your first post that leads to the solution post. All of this helps users find answers to similar questions.

Hi
I think you misunderstood me, i cleared up the code like you told me to, but its not fixed !
The error is still being shown!

You’re right, I did misunderstand you. I thought “Thx, cleared it up” meant the problem had been “cleared up”.

It’s challenging to provide you with additional assistance because I cannot replicate the conditions that produce the warning messages you have.

Do you know where in your system you are using the following template? Is it in a Template Sensor?

{{ states.sensor.date_short.state }}

The warning message is:

'None' has no attribute 'state' when rendering '{{ states.sensor.date_short.state }}'

It means when it tried to get the state value of the Template Sensor entity sensor.date_short, that entity did not exist yet (‘None’) and so it has no state value.

If that template is used by a Template Sensor, do you know if it is defined (in the configuration) before or after sensor.date_short is defined?

I’m using it in a view
See Home-Assistant/Home.yaml at 5a45ed0ca2f8894b81001d72214595bc64412e98 · skank01/Home-Assistant · GitHub

My guess is your Template Sensors haven’t finished loading when the Lovelace UI is rendering cards. As a consequence, references to Template Sensors, at that point in time, cannot be resolved (because they don’t exist yet).

I haven’t experienced this problem on either of the two systems I have but then neither use the custom cards you are using (standard Lovelace cards don’t support Jinja2 templating).

Maybe its cause of the modbrowser?
Not sure…

I’m also getting an error there
[548062345440] Received invalid command: browser_mod/connect
17:23:12 – (FOUT) Home Assistant WebSocket API

I am also getting these errors which is flooding my error log file.

WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'None' has no attribute 'state' when rendering '{{states.sensor.avg_outside_temp.state}}°C'

The problem is that sensor.avg_outside_temp doesn’t actually exist in my environment. It is an old template sensor I had which I deleted some time ago. I cannot find any reference to it in developer tools nor in any of my yaml files.

Any ideas of how to find where it might be?

Yes, I just ran into this problem as well (errors about no longer existing templating). I had error messages about a template that resulted in None and consequently taking an attribute failed. I changed the code (inside a lovelace card) to fix it. It seems to work correctly, also when tested in developer tools/templates.
Yet, when I restart the server these error messages, literally showing the “old” code keep appearing. Something must be cached somewhere, but I cannot find it.

Just discovered something interesting. I had a hunch, so I manually deleted the HomeAssistant log file and then restarted. This made the error referencing no longer existing template code go away.

The strange thing was that the error was always being reported with a time stamp for the latest restart. I suspect there is code to scan the logs for errors that has a bug and labels what it found with the wrong timestamp. After I removed the log, it could no longer find these messages and it went away.

In case it helps someone else, I had similar errors due to old code I had left in the Template Editor under Developer tools.