How to replace entity_id in template sensors

The entity_id option is now depracated as of 0.61, however I’ve just started using it as part of a solution.

I have the following sensor:

- platform: template
  sensors:
    my_event_offset:
      friendly_name: "My Event offset"
      entity_id:
        - calendar.myevents
        - sensor.date__time
      value_template: >
        {% if as_timestamp(states.calendar.myevents.attributes.start_time) - as_timestamp(now()) < 21600  %}on{% else %}off{% endif %}

The attribute start_time will only update daily or weekly, so I think the sensor would only update them. However, for the maths to actually detect that the current time is within the offset period the sensor must update every minute.

This has been achieved by using the entity_id and listing the date__time sensor. How can I force the sensor to stay up to date without using the entity_id option?

It seems the solution in my case was to replace the as_timestamp(now()) with the state of a sensor, that way the sensor will be updated just like it was with the entity_id method.

So as_timestamp(now())

Becomes as_timestamp(strptime(states.sensor.date__time.state, "%Y-%m-%d, %H:%M"))

This should update the sensor every minute.

8 Likes