Binary sensor template help needed

Hi, I would like change the output text of a binary sensor. If binary sensor “on” then show “test123” otherwise “testabc”.

I tried several ways and searched here in forum but I haven’t found something what helps.

{{ is_state(states(‘binary_sensor.test’,‘on’), ‘test123’, ‘testabc’) }}

{{ iif(states(‘binary_sensor.test’) = ‘on’, ‘test123’, ‘testabc’) }}

It would be great, if someone can help.

Thanks in adcance!!!

If a sensor has custom states it is no longer a binary_sensor. Binary sensors are always on or off, and the only way to change what they show is to set a device class. So if you want any texts that are not consistent with device classes (these are translated to your language), use a normal sensor.

Device classes available to binary sensors:

template to create a sensor to show arbitrary texts:

{{ 'test123' if is_state('binary_sensor.test', 'on') else 'testabc' }}

You might want to also include an availability template to make the sensor unavailable if the source sensor is unavailable.

The iif version you had would also almost work, as long as you keep it a sensor and not an binary sensor. But the = should be == because = is an assignment and == is an equals test:

{{ iif(states(‘binary_sensor.test’) == ‘on’, ‘test123’, ‘testabc’) }}

or:

{{ iif(is_state(‘binary_sensor.test’, ‘on’), ‘test123’, ‘testabc’) }}

Always test your templates in developer tools first.

2 Likes