I’ve been using Home Assistant for years and have found templating to be generally trivial in getting things done but I have an absolute ton of zigbee sensors of various types.
I have created a formulaic approach which setups a named template sensor that is subsequently used by the meta sensors. However, it occurs to me variables could be used to make sweeping changes and then i would not need to worry about copy and paste 16 or so times per file. I have also seen that trigger templates for sensors will remove the unknown and unavailable messages from graphs.
So the plan is:
Create variables in template
Use variables to create better named trigger template sensors
Use these trigger sensors in template sensors to deal with missing information like pressure.
template:
- variables:
sensorname: newbathroom
location: Bathroom
- trigger:
- platform: state
entity_id: sensor.{{sensorname}}_pressure
not_to:
- unknown
- unavailable
sensor:
- name: absPressure{{location}}
state: '{{ trigger.to_state.state }}'
unit_of_measurement: '%'
- sensor:
- name: "{{location}} Pressure"
unit_of_measurement: "%"
state: >
{% if states('sensor.absPressure{{location}}')| is_number %}
{% set tem = states('sensor.absPressure{{location}}') | float %}
{% else %}
{% set tem = 0.0 %}
{% endif %}
{{ tem | round(1) }}
So is this even possible?
I found this in my searching but I get formatting errors when trying to do it. So I think there is probably a way to do if you have been deep enough.
Those variables are not related to any of your sensors, they are their own list item.
State triggers do not support templating, so that’s a not going to work either.
Even if your variables were in the same list item as your trigger and sensor, the variables block is rendered after the trigger fires, so the values wouldn’t be available to set up the trigger listener.
Removing the hyphen and adding a space would rememdy the listing issue but if the variables are not replaced until after the trigger is fire I’m knackered even with that. Nevermind I wanted to ask because it would have made things so much more easier.
Would it be possible if the sensorname variable was not used like this?
template:
variables:
location: Bathroom
- trigger:
- platform: state
entity_id: sensor.newbathroom_pressure
not_to:
- unknown
- unavailable
sensor:
- name: absPressure{{location}}
state: '{{ trigger.to_state.state }}'
unit_of_measurement: '%'
- sensor:
- name: "{{location}} Pressure"
unit_of_measurement: "%"
state: >
{% if states('sensor.absPressure{{location}}')| is_number %}
{% set tem = states('sensor.absPressure{{location}}') | float %}
{% else %}
{% set tem = 0.0 %}
{% endif %}
{{ tem | round(1) }}
No, now variables isn’t part of any item, so it will probably cause the template integration to fail … and it still wouldn’t have a value in either of your sensor’s configurations.
Also, I missed it in your first post, but you cannot nest templates, the following is incorrect:
{% if states('sensor.absPressure{{location}}')| is_number %}
Assuming you moved the definition of location somewhere accessible, you would just use the variable and conctenate it with the rest of the entity ID string:
{% if states('sensor.absPressure'~location )| is_number %}