Hey guys!
I did not find a solution to this, so please have mercy. Maybe it is a obvious thing.
In the following only the latter sensor will show up. It seems like if I define triggers for the sensor, the template in the name won’t be accepted. The same sensor (with triggers) without a template in the name works. I’ve been dealing with it for two days, but couldn’t find a solution.
- trigger:
- platform: time_pattern
# This will update every night
hours: 0
minutes: 0
sensor:
# Keep track how many days have past since a date
- name: "test123_{{ now().ctime()[:3] }}"
state: "{{ now().ctime() }}"
- sensor:
- name: "test1234_{{ now().ctime()[:3] }}"
state: "{{ now().ctime() }}"
you can’t template name but you can template friendly_nmae in attributes.
- trigger:
- platform: time_pattern
# This will update every night
hours: 0
minutes: 0
sensor:
# Keep track how many days have past since a date
- name: "test123"
state: "{{ now().ctime() }}"
attributes:
friendly_name: "test123_{{ now().ctime()[:3] }}"
But somehow this works? Only if there’s a trigger defined.
I rather would work around the friendly name. The point is, not to type entity id‘s for all 120+ devices. I definitely spent more time now on this than just typing it down but that’s how it his by nature with coding I guess
This is because templates are only resolved when the template is executed. When you use a trigger, it will only resolve the template on the trigger. Without the trigger, the template is resolved based on the states & objects inside the trigger.
So at restart, that triggerless sensor will update immediately, then once a minute on the minute (defined by using now() in your template).
The time pattern trigger will only update once a night, therefore the template will only be resolved when that trigger occurs.
Woah… somehow you got the same line as me!
Na, joking yes I’ve seen your post and used it for simplification here. But it wasn’t 100% clear to me that I could really only template friendly names.
Thank you for helping out
However, it’s probably not ideal for your application because you want the template to be evaluated exclusively at midnight in order to produce a count of total days. You might want to try it just to confirm the point that a trigger must occur in order to generate the sensor’s name.
I don’t believe this trigger works for template resolution as the event occurs, then the system reloads, effectively never getting the trigger. It’s possible I was using the wrong event though when I was doing this a few weeks ago.
This is my actual code now. It’s for battery powered zigbee devices for a simple, functional „offline alarm“. I guess I need to go with the friendly name.
- trigger:
- platform: state
entity_id: input_boolean.on_off_laden
to: "on"
- platform: time_pattern
hours: /1
sensor:
- name: "xy sensor"
state: >
{% set entity = 'bewegungsmelder_b1_linkquality' %}
{% set ls = as_timestamp(state_attr('sensor.' ~ entity, 'last_seen')) | int %}
{% set n = as_timestamp(now()) | int %}
{% if (n - ls) > 86400 %}
Offline
{% elif 43200 < (n - ls) < 86400 %}
Demnächst prüfen
{% elif (n - ls) < 43200 %}
Online
{% endif %}