Binary Sensor Templating

Hi,

I can’t seem to wrap my head around getting the binary sensor template to work properly.

I have two binary sensors that I’d like to customize their state to Open and Closed.

  • binary_sensor.door_window_sensor_158d0001584d8d
  • binary_sensor.door_window_sensor_158d0001549402

Currently, my output looks like this…

...

  customize:
    # iconing
    binary_sensor.frame_front_door:
      icon: mdi:glassdoor
    binary_sensor.frame_back_door:
      icon: mdi:glassdoor

...

binary_sensor:
  - platform: template
    sensors:
      frame_front_door:
          friendly_name: Front Door
          entity_id:
            - binary_sensor.door_window_sensor_158d0001549402
          value_template: >-
            {%- if is_state("binary_sensor.door_window_sensor_158d0001549402", "on") -%}
            Open
            {%- else -%}
            Closed
            {%- endif -%}

      frame_back_door:
          friendly_name: Back Door
          entity_id:
            - binary_sensor.door_window_sensor_158d0001584d8d
          value_template: >-
            {%- if is_state("binary_sensor.door_window_sensor_158d0001584d8d", "on") -%}
            Open
            {%- else -%}
            Closed
            {%- endif -%}

When using the dev tools, I can get the value_template to evaluate correctly, but not to update at all. Additionally, my output panel doesn’t have the correct state names and it won’t ever update those values.

What have I done wrong here?

Thanks,

  • SN

If I’m not mistaken, a binary_sensor can only be on/off. You may have to use a template sensor instead.

2 Likes

Yup, that did it. Documentation does not specify that, wish it did. Thanks!

I use value templates to rename states in my sensors:

sensors:
     epsonprinter:
       value_template: "{% if is_state('device_tracker.epson3e7694fiosrouterhome', 'home') %}on{% else %}off{% endif %}"
1 Like

I use that approach too, but it’s not always available or possible.

BTW: I just submitted a PR to clarify the possible states of a template binary sensor in the docs.

3 Likes

@rpitera

Is there any way to customize the icon, friendly name AND the state, but keep the functionality of having my icon go yellow when the state is on?

It seems like it’s working … but I get an error on startup and my group won’t appear. :frowning:

17-03-11 22:23:37 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render icon template Back Door, the state is unknown.
17-03-11 22:23:37 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render icon template Front Door, the state is unknown.

      back_door:
          friendly_name: Back Door
          icon_template: >-
            {%- if states('binary_sensor.door_window_sensor_158d0001584d8d') -%}
            {{ states.binary_sensor.door_window_sensor_158d0001584d8d.attributes.icon }}
            {%- else -%}
            mdi:glassdoor
            {%- endif -%}
          value_template: >-
            {%- if is_state("binary_sensor.door_window_sensor_158d0001584d8d", "on") -%}
            Open
            {%- else -%}
            Closed
            {%- endif -%}

I haven’t figured out a way to do this with template sensors. It never really occurred to me to try it, but now I’m curious.

If you find out something neat, do show and tell! I’m not even sure the changing-of-icon-colors is a public function in hass. I assume it’s just done via CSS based on a state change, but we currently don’t have the ability to modify that. At least not to my knowledge!

1 Like

I remember there was a pull somewhere for this, to be able to template the icons. I’m not sure whether it’s already been merged or what.

I’ll have to look again.

OK, I knew I wasn’t crazy.

Well, not about this anyway:

Right, and that totally works … but it doesn’t seem to work on-startup for me. It tells me the state is unknown, and will not create the group.

edit … I’m a dirty liar. I had the wrong sensor names. It still doesn’t solve my want though. If I use the ugly sensor name, the icon will light up yellow/gold when the sensor is “on” and it will be the regular mdi icon when it is “off”. This functionality is not reproduced when you make a template sensor. :frowning:

1 Like