Hi, in a template is it possible to filter/exclude 2 states of an entity?
sensor:
- name: "Heating State"
unique_id: "heating_state"
icon: mdi:fire
state: >
{% if is_state_attr('climate.smarther_thermostat','hvac_action','idle') %}
INACTIVE
{% else %}
ACTIVE
{% endif %}
When the entity “climate.smarther_thermostat” becomes unavailable or unknown, the template returns the ACTIVE result.
Is it possible to ignore these 2 states in the template? Or alternatively, that the unknown/unavailable state returns INACTIVE instead of ACTIVE.
Thanks
The best practice is to define an availability:
sensor:
- name: "Heating State"
unique_id: "heating_state"
icon: mdi:fire
state: >
{% if is_state_attr('climate.smarther_thermostat','hvac_action','idle') %}
INACTIVE
{% else %}
ACTIVE
{% endif %}
availability: "{{ has_value('climate.smarther_thermostat') }}"
But, if you prefer it to return INACTIVE
when the climate
entity is unavailable, you can use the in
test against a list:
sensor:
- name: "Heating State"
unique_id: "heating_state"
icon: mdi:fire
state: >
{% if state_attr('climate.smarther_thermostat','hvac_action') in ['idle', 'unavailable', 'unknown', none] %}
INACTIVE
{% else %}
ACTIVE
{% endif %}
1 Like
Ok, thanks.
Can I also use the second solution, setting the availability as well?
Or is it not necessary?
You can, but it is redundant… the availability template is checked first, if that passes then the state template is rendered.
1 Like
Thanks, it works perfectly.
Is it also possible to add a hysteresis to this sensor? For example if ACTIVE for 3 seconds…
Not as it is currently configured.
Options:
- Switch to a trigger-based sensor and use triggers that have defined
for
values.
- Switch to a binary sensor so you have access to the
delay_on
configuration variable.
I tried binary sensor, but it is not suitable (I need named states as I say).
I have to study the sensor with the trigger, I’ve never made one. What do you recommend?
The first thing to do is to create a sensor that mirrors the value of the hvac_action
attribute of your climate
entity. This is necessary because attributes do not have an independent way to track when they change.
sensor:
- name: "Smarther HVAC Action"
unique_id: "smart_ther_hvac_action_0001"
icon: mdi:fire
state: "{{ state_attr('climate.smarther_thermostat', 'hvac_action') }}"
Then you create a trigger-based sensor that watches that sensor’s state and with a 3 minute duration on the “ACTIVE” trigger:
trigger:
- id: INACTIVE
platform: state
entity_id: sensor.smart_ther_hvac_action
to:
- idle
- unavailable
- unknown
- ~
- id: ACTIVE
platform: state
entity_id: sensor.smart_ther_hvac_action
not_to:
- idle
- unavailable
- unknown
- ~
for: "00:03:00"
sensor:
- name: "Heating State"
unique_id: "heating_state"
icon: mdi:fire
state: "{{ trigger.id }}"