Template Binary Sensor - ignore unwanted values

Hi!

I created a custom binary sensor based on template entity. Using the is_state function I can check the values of a number entity.

The Numbers have a range from 1 to 64. The binary sensor state should be true when the number entity has a certain value (12,22,32). For one value - 20 in my case - the state of the sensor should switch to false/off/0.

All other values should be ignored. But in my case the binary sensor switches to off for all numbers except 12,22,32. What can I do to ignore the other values?

My current template config:

{% if is_state('number.szene_wohnbereich', ['12', '22' ,'32']) %} 1
{% elif is_state('number.szene_wohnbereich', '20') %} 0 
{% elif is_state('switch.schlafen_wohnzimmer', 'off') %} 0
{% endif %}

Thx,
Sebastian

Hi!

I want to use a template binary sensor to trigger an action. The trigger for the sensor is number entity. Based on certain states of the number entity the binary sensor should have state on or off.

But the number entity can have states that I don’t want to use as a trigger. The binary sensor should ignore these values, do nothing.

The template for binary sensors seems to use every value. If one occurs, that doesn’t match an if-condition for TRUE, it will set the sensor to OFF.

What can I do to ignore or filter these values?

In other words, the template should report the binary_sensor’s existing state.

this.state is the binary_sensor’s existing state. Modify your template so that it reports this.state when it encounters values (from the number entity) that you want to ignore.

1 Like

Please don’t create duplicate topics. Merged.

Hi and thank you, Taras, I tried your way. This is how my state template is looking currently:

{% if is_state('number.szene_wohnbereich', ['11', '12', '13', '14', '21', '22', '23', '31', '32' ,'33']) %} 

  1
{% elif is_state('number.szene_wohnbereich', '20') %} 
  0
{% elif is_state('switch.schlafen_wohnzimmer', 'off') %} 
  0
{% else %} 
  {{ this.state }}
{% endif %

}

The else statement is completely ignored, as it seems to me. If input value switches to 15 (value is not in if clause), while the sensor state is ‘on’, it turns off.

Am I doing something wrong?

Hi!

I checked it again and found an error in my statement. The else-statement was never reached. Without querying the state of sleep switch (second elif) it is working correctly now.

This is my solution:

{% if is_state('number.szene_wohnbereich', ['11', '12', '13', '14', '21', '22', '23', '31', '32' ,'33']) %} 
  on
{% elif is_state('number.szene_wohnbereich', '20') %} 
  off
{% else %} 
  {{ this.state }}
{% endif %}