I am using the entity_id for several of my template sensors. After some reading on the new sensor template approach, I am still wondering if it is safe to just remove the entity id from my sensors or if I need to do some additional changes to make sure it keeps working.
Some of the template sensors are using the “states” function, therefore the actual entities are on a string passed to the parameter of the function. I’m not sure if this may prevent the parser from figuring out which entities it should monitor for updating the template sensors.
Here are some examples of my sensors:
A sensor for keeping daily energy totals (energy splitted by 3 different moments of the day):
And finally a couple of sensors to determine if on daylight saving time and if on last day of the month:
- platform: template
sensors:
daylight_saving_time:
entity_id: sensor.time
value_template: "{{now().timetuple().tm_isdst | int > 0 }}"
last_day_of_the_month:
friendly_name: 'Last Day of the Month'
entity_id: sensor.date
icon_template: "mdi:calendar"
value_template: "{{ (as_timestamp(now()) + 86400)|timestamp_custom('%d', true) == 01 }}"
What do you make of this? Can I simply remove the entity_id from my sensors? In these last two sensor templates I’m not even using the referred entities directly on the template itself.
Just remove the entity_id field for all 4 templates. The only behavior change you will have is in your last template. It will now be updated once a minute, where as before it would update once a day. now() inside templates will always update once a minute if there hasn’t been an update in the past minute. And it should update on the minute.
There was some back and forth but it ended up that now() updates every minute on the minute, regardless of other updates.
For the template entities, it is generally safe to just remove entity_id. There were a couple of releases where workarounds were needed in some situations but that is now being handled for all but the most special cases (I didn’t see one yet).
might I invite you to a thread on the template editor itself? It seems to behave unexpectedly in certain cases, making it hard to debug incorrect templates… this being one of them
also, editing correct templates to include errors, leave the previous correct output there, so one might be tricked into believing a template is correct, while it isnt at all.
it’s not unexpected… it’s a byproduct of typing. We all knew this would happen.
The resolver (my name) trys to type the output of a template. You can see the resulting type in the template editor. If you have more than 1 output {{ }}, it will almost always resolve as a string. A single output will resolve as whatever it deems the type to be. It does not resolve on a line by line template. It resolves the entire output. It does not parse anything. It straight takes the output and applies a type to it. So if your output is 4.0, it will resolve to a number, in this case an int and return 4.
well, not exactly. we didnt know editing this template:
{%- set ns = namespace(not_off=[]) %}
{%- for s in expand('group.switches_sensors_actueel')
if s.object_id.split('_actueel')[0] not in
['inductieplaat','patchboard_zolder'] and
states(s.entity_id) not in ['0','0.0'] and
states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}
{% set ns.not_off = ns.not_off + [s] %}
{% endfor %}
{{ns.not_off|count}}
attribute_templates:
List: >
{%- set ns = namespace(not_off=[]) %}
{%- for s in expand('group.switches_sensors_actueel')
if s.object_id.split('_actueel')[0] not in
['inductieplaat','patchboard_zolder'] and
states(s.entity_id) not in ['0','0.0'] and
states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}
{%- set ns.not_off = ns.not_off + [s.name+ ': ' + s.state + ' watt'] %}
{%- endfor %}
{% set count_not_off = ns.not_off|count %}
{% set list = ns.not_off %}
{% if count_not_off == 0 %} All fine
{%- elif count_not_off == 1 %}
{{list[0]}} is still using power!
{%- elif count_not_off == 2 %}
{{list|join(' and ')}} are still using power!
{%- else %}
{{list[:-1]|join(', ')}}, and {{list[-1]}} are still using power!
{%- endif %}
to in incorrect template (I edit the group name to a non existing one)
{%- set ns = namespace(not_off=[]) %}
{%- for s in expand('group.switches_sensors_doesnt_exist')
if s.object_id.split('_actueel')[0] not in
['inductieplaat','patchboard_zolder'] and
states(s.entity_id) not in ['0','0.0'] and
states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}
{% set ns.not_off = ns.not_off + [s] %}
{% endfor %}
{{ns.not_off|count}}
attribute_templates:
List: >
{%- set ns = namespace(not_off=[]) %}
{%- for s in expand('group.switches_sensors_doesnt_exist')
if s.object_id.split('_actueel')[0] not in
['inductieplaat','patchboard_zolder'] and
states(s.entity_id) not in ['0','0.0'] and
states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}
{%- set ns.not_off = ns.not_off + [s.name+ ': ' + s.state + ' watt'] %}
{%- endfor %}
{% set count_not_off = ns.not_off|count %}
{% set list = ns.not_off %}
{% if count_not_off == 0 %} All fine
{%- elif count_not_off == 1 %}
{{list[0]}} is still using power!
{%- elif count_not_off == 2 %}
{{list|join(' and ')}} are still using power!
{%- else %}
{{list[:-1]|join(', ')}}, and {{list[-1]}} are still using power!
{%- endif %}
shows the same output (doesn’t change it), only shows which entities is listens to correctly:
Because you still don’t understand the change. Remove the yaml and it will produce the correct result. Only use the template, no extra text. Please spend 5 minutes reading my explanation.
Also, “All fine” is expected with the wrong entity_id because your iteration always returns zero when expand returns an empty list. 100% unrelated to the topic at hand.
well, I do (understand), and I did (read). And I still don’t like it. I’ll stop fighting it, and learn to live with it, but it certainly feels like a step back. sorry.
yep agree on that too. As I has already concluded myself.
maybe to the OP topic, but not in response to the 'sub discussion which had developed, being the template editor behaving unexpectedly.
Hi there, my be i have done logic mistake. could you possible to help me? I think my code problem is about entities.
Im trying to do thermostat. How can i do when i use 2 nodemcu? im using one for temperature measurement… other for controlling heater… 2 device is different point of my house. but they are in the same wireless hub… two of them working well know, but I couldnt do thermostat
This really isn’t an error. You no longer need the entity_id in your sensor or binary_sensor templates that are in your configuration files. This isn’t keeping your automation from running - there’s likely some other error in the trigger if you can run it from the developer/service tool (assuming that’s what you mean when you state you can run it manually).
To eliminate this error, simply comment them out in your config files templates - HA now figures it out when to update the sensor/binary_sensor without the added overhead of the entity_id.
Please follow @SteveDinn’s instructions by re-posting but with the requested formatting. For example, copying and pasting your provided code in the proper format looks like this:
Looking at this way shows a problem with spacing (and YAML is VERY sensitive to spacing!). Additionally, this isn’t complete - assuming it is within an automation, you need an action: section and maybe a condition: section depending on what you are trying to do.
Please post the complete automation and then we can help more (starting with the alias line through the action section.
Additionally, you can test the template in the developer/template tool - I use that almost every time…
How do you recommend removing the entity_id from this code? I know it’s using it as a reference point in the value template. I’m fairly new at this and have tried a couple things and have yet to come up with a viable solution on my own.