Template Sensor no longer updating?

Seems something broke in the latest update - just want to confirm it’s not just me?

I have been using the following template sensor for ages, if I post the script in “Templates” tool it displays correctly, but the actual template sensor only displays the initial value the first time it changed - and persists this value?

alarm_open_zones:
      friendly_name: "Alarm Open Zones"
      value_template: "{%- for state in states.binary_sensor -%}
                      {%- if state.state == 'on' and state.name != 'Electricity available is low' -%}
                      {{ state.name }} status is {{ state.state }}.{{- '\n' -}}
                      {%- endif -%}
                      {%- else -%}
                      Nothing is open
                      {%- endfor -%}"

always read the breaking changes before updating.

1 Like

just add the sensor.time to the entity and that will update every minute

alarm_open_zones:
      friendly_name: "Alarm Open Zones"
      entity_id: sensor.time
      value_template: "{%- for state in states.binary_sensor -%}
                      {%- if state.state == 'on' and state.name != 'Electricity available is low' -%}
                      {{ state.name }} status is {{ state.state }}.{{- '\n' -}}
                      {%- endif -%}
                      {%- else -%}
                      Nothing is open
                      {%- endfor -%}"

Edit: This won’t update when one of the states update though. If you want that you need to list the sensors out instead of using the template.

1 Like

Could you tell me what entity to add in the following configuration in order to make this work.

Basically I track the last movement of my PIR sensor through this template sensor

last_move_at:
  friendly_name: "Last Movement"
  icon_template: "{mdi:ninja}" 
  value_template: "{{ relative_time(states.binary_sensor.movement.last_changed) }}"

binary_sensor.movement

1 Like
last_move_at:
  friendly_name: "Last Movement"
  entity_id: binary_sensor.movement
  icon_template: "{{ mdi:ninja }}" 
  value_template: "{{ relative_time(states.binary_sensor.movement.last_changed) }}"

Check the logs! If a template sensor is listed that HA can not decode a entity to track it will issue a warning in the logs at startup…

Sadly I already tried that( entity_id: binary_sensor.movement ) along with sensor.time but still it’s not getting updated.

Are you restarting? If so, last_changed does not persist through shutdown. You’ll actually have to restart and then force movement.

It only says the following

2018-10-31 00:24:33 WARNING (MainThread) [homeassistant.components.sensor.template] Template sensor last_move_at has no entity ids configured to track nor were we able to extract the entities to track from the icon template(s). This entity will only be able to be updated manually.

Yes I did restarted the HA service multiple time, This time even I rebooted my raspberry pi ( Which is hosting HA), did a movement in front of it to update it forcefully even though it is not updating.

image

Ah, now that I know what relative_time does, you will want this:

last_move_at:
  friendly_name: "Last Movement"
  entity_id: sensor.time
  icon_template: "{{ mdi:ninja }}" 
  value_template: "{{ relative_time(states.binary_sensor.movement.last_changed) }}"

But you need to make sure you have the sensor.time implemented.

1 Like

Petro this is interesting; why would it not update by specifying the binary_sensor.movement itself; I guess because it’s wrapped in relative time and we’re comparing the binary_sensors last_changed against current time? The binary_sensor is not changing; current time is…

But should it be listed so if it changed we also update to compare against current time?

It would, its just that the way relative_time works is that it calculates the time since the last change. I.E. if it updated 35 minutes ago, the relative_time returns “35 minutes”. Last_changed will always be 0 seconds it the template because it only updates when the device updates. Essentially, now() - now() is zero. So, you’d want it to update every minute because it would actually be now()-last_updated, where last_updated does not equal now().

Ah - thanks - usually skim through it - but didn’t pick this up, I’ll give it a try this evening

I tried everything, even tried both the entity ids together, but still not able to get it to work. :frowning:

last_move_at:
  friendly_name: "Last Movement"
  entity_id: binary_sensor.movement, sensor.time
  icon_template: "{mdi:ninja}"
  value_template: "{{ relative_time(states.binary_sensor.movement.last_changed) }}"

Yes, but do you actually have sensor.time in home assistant?

And your icon template is wrong. That should be done in the customize section.

homeassistant:
  customize:
    sensor.last_move_at:
      icon: mdi:ninja

and

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      last_move_at:
        friendly_name: "Last Movement"
        entity_id: sensor.time
        value_template: "{{ relative_time(states.binary_sensor.movement.last_changed) }}"

And to top it off, you need to move in front of your binary_sensor.movement sensor after you restart.

2 Likes

Thank you so much brother !! :blush:

I didn’t had the sensor.time in my home assistant at the first place. Added the same like you advised and it is working fine now :slight_smile:

1 Like

Is there a way to delete/archive these sort of posts? The answers are no longer applicable because the template platform has been deprecated as a sensor.

it’s not deprecated.