How to set state to Binary sensors?

Hi there.
I have some Broadlink door sensors, and they work fine. To change the state names (translation) and also the pictures, I added the following to my sensors.yaml:

  - platform: template
    sensors:
      janela_sala:
        friendly_name: Janela Sala
        entity_id: sensor.broadlink_s1c_janela_sala
        value_template: "{% if is_state('sensor.broadlink_s1c_janela_sala', 'open') %}Aberta
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'closed') %}Fechada
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'tampered') %}Violada       
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'unknown') %}Desconhecido
                         {% endif %}"
        entity_picture_template: "{% if is_state('sensor.broadlink_s1c_janela_sala', 'open') %}/local/windowopen.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'closed') %}/local/windowclosed.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'tampered') %}/local/exclamacao.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'unknown') %}/local/interrogacao.png
                                  {% endif %}"

I just purchased a sonoff DW2 wifi door sensor… It is working, but I also wanna change the state names and pics. The problem is: It is not a regular sensor, it is a binary sensor. (entity_id: binary_sensor.sonoff_10011a340a) and I don´t know to add the code to my sensors.yaml to make it work. Any ideas? (Even being binary - open and closed… it has “unavailable”, and I think that’s the problem… or not)

Sem tĂ­tulo

You can do the same thing (create a template sensor) but instead of using “sensor.roadlink_s1c_janela_sala” in the template use “binary_sensor.sonoff_10011a340a”.

you will end up with a regular sensor (in addition to the existing binary sensor) but in the end it won’t really matter because it will display the way you want and not the “default” way it is now.

It didn´t work.
Here´s what I did:

  - platform: template # customização dos sensores de janelas, com tradução de open/closed para aberta e fechada, e ícones dinâmicos
    sensors:
      janela_sala:
        friendly_name: Janela Sala
        entity_id: sensor.broadlink_s1c_janela_sala
        value_template: "{% if is_state('sensor.broadlink_s1c_janela_sala', 'open') %}Aberta
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'closed') %}Fechada
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'tampered') %}Violada       
                         {% elif is_state('sensor.broadlink_s1c_janela_sala', 'unknown') %}Desconhecido
                         {% endif %}"
        entity_picture_template: "{% if is_state('sensor.broadlink_s1c_janela_sala', 'open') %}/local/windowopen.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'closed') %}/local/windowclosed.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'tampered') %}/local/exclamacao.png
                                  {% elif is_state('sensor.broadlink_s1c_janela_sala', 'unknown') %}/local/interrogacao.png
                                  {% endif %}"
      
      janela_escritorio:
        friendly_name: Janela EscritĂłrio
        entity_id: binary_sensor.sonoff_10011a340a
        value_template: "{% if is_state('binary_sensor.sonoff_10011a340a', 'Open') %}Aberta
                         {% elif is_state('binary_sensor.sonoff_10011a340a', 'Closed') %}Fechada
                         {% elif is_state('binary_sensor.sonoff_10011a340a', 'Unavailable') %}Violada       
                         {% endif %}"
        entity_picture_template: "{% if is_state('binary_sensor.sonoff_10011a340a', 'Open') %}/local/windowopen.png
                                  {% elif is_state('binary_sensor.sonoff_10011a340a', 'Closed') %}/local/windowclosed.png
                                  {% elif is_state('binary_sensor.sonoff_10011a340a', 'Unavailable') %}/local/exclamacao.png
                                  {% endif %}"

It generates an entity - sensor.janela_escritorio … but it still doesnt work :frowning:
I guess I cannot mix sensor and binary_sensor with the same template…

Capturar2
Capturar3

That’s because the state of a binary sensor is only ever ‘on’, ‘off’ or sometimes ‘unavailable’.

you have the states as “Open”, “Closed” & “Unavailable”.

those are the displayed states but they aren’t the actual states in the dev tools states page.

change those and it should work.

And also the “entity_id: xxx” in the sensor definition has been gone for a while now. remove those lines.

tks man, it worked! It was really because of open/closed instead of on/off. I didn’t know that there are “visual states” and “actual states”…

thank u very much.

1 Like