Template sensor value unknown

I’m trying to create a tempate sensor so i can show the value on a markdown card.

My Configurtation.yaml (part of it):

sensor: !include sensors.yaml

My Sensor.yaml

- platform: template
  sensors:
    not_there:
      value_template: "{{ (( as_timestamp(now()) - as_timestamp(strptime('06.07.2018', '%d.%m.%Y')) ) / 0 ) | round(2) }}"
      entity_id: sensor.date
      friendly_name: 'Not there'
      unit_of_measurement: "Days"

ui-lovelace.yaml:

views:
  - title: Home
    cards:
    - type: markdown
      content: >
        {{ states('sensor.not_there') }}

Whatever I do, even after restart, the card displays ‘Unknown’. What am I doing wrong?

Are you trying to get the number of days counted? This worked for me in the template editor.

{{ (((as_timestamp(now())) - (as_timestamp(strptime('06.07.2018', '%d.%m.%Y')))) / 86400 ) | int }}

I tried it and in the log:

ZeroDivisionError: float division by zero

Not sure why you’re trying to divide by zero, works when you remove that

There went something wron with copy paste of my code…

- platform: template
  sensors:
    not_there:
      value_template: "{{ (( as_timestamp(now()) - as_timestamp(strptime('06.07.2018', '%d.%m.%Y')) ) / 86400 ) | round(2) }}"
      entity_id: sensor.date
      friendly_name: 'Not there'
      unit_of_measurement: "Days"

This is my code in sensor.ymal, but still Unknown on my card.

There is no problem with how I set this up? because I use different files, and with include sensor.yaml etc.?
I have another type of sensor in the same sensor.yaml file, a command line, and that works fine. I can show that value on a card.

It’ll be unknown until the code executes, which is midnight. Also, this template won’t work in 0.115. Best to make that change now.

Also, as a side note, wouldn’t you always want this to floor? If your time was after 12pm, you’d have an extra false day added on.

- platform: template
  sensors:
    not_there:
      value_template: "{{ (( as_timestamp(strptime(states('sensor.date'))) - as_timestamp(strptime('06.07.2018', '%d.%m.%Y')) ) / 86400 ) | round(2) }}"
      friendly_name: 'Not there'
      unit_of_measurement: "Days"

Okay, maybe i used the wrong code for the sensor, I took it from the documentation:

I added a new sensor to my sensor.yaml.

- platform: template
  sensors:
    test:
      value_template: >-
        "testing 1 2 3"
      friendly_name: 'test'

To my knowledge, this sensor should just retun ‘testing 1 2 3’.

in my ui-lovelace.yaml i use

{{ states('sensor.test') }}

But still it displays ‘Unknown’

what does sensor.test show in dev tools states page ?

it shows “testing 1 2 3”…

And… I just updated the hassio os, and it did reboot. And now I see the right value on my card. That raises a next question, do i need to reboot / restart before sensor.yaml does it’s job?

And is it possible to refresh a sensor.yaml with ‘platform: template’ with an interval like:

- platform: template
  scan_interval:
    minutes: 1
  sensors:
    test:
      value_template: >-
        "testing 1 2 3"
      friendly_name: 'test'

N.B. i know refreshing a static value does not make sense, but I want to do some things with date and time in the template.

you always have to restart when adding sensors to sensors.yaml. In version 0.115 you can reload the sensors file without restarting.