Binary Sensor Template Not Working

templates
Tags: #<Tag:0x00007fd0765036a8>

#1

I am trying to create a new message on PIR Sensor ON & OFF. But its not working. Here I how I am putting it.

binary_sensor:
  - platform: mqtt
    name: "Dressing Room PIR"
    state_topic: "home/nehulroom/dressingroom/pir/status"
    payload_on: "on"
    payload_off: "off"
    value_template: >-
        {% if value == "on" %}
        Inside
        {% else %}
        Away
        {% endif %}

Where I am making mistake in value_template because its not working. And from example I am checking it looks almost same.

Also I want to control a switch with the PIR sensor. So how can I do that? Do I have to create automation for it? Is there any direct way of doing it without automation?

Thank you!


#2

A binary sensor is either on or off, i.e. your value_template can’t yield any other string.
If the MQTT message of this topic already is on or off, then the use of a value_template isn’t necessary.

And, yes, you normally create an automation to control lights via motion detected.
Depending of the technology used, you may be able to have direct control, e.g. Zwave has associations to control such behaviour without an automation. I believe Philips Hue could do the same within their hub/app.


#4

Is AUTOMATION the only way to control the switch (created in my home assistant entity id switch.dressing_room_light) on the basis of PIR sensor (which is my binary sensor attached in my home assistant with entity id binary_sensor.dressing_room_pir).

Can I also accomplish this with a switch template?


#5

53%20PM

I am not sure where I am making any mistake. As I just try the code pattern you have provided. According to that as well, It’s not working.

binary_sensor:
  - platform: mqtt
    name: "Dressing Room PIR"
    state_topic: "home/nehulroom/dressingroom/pir/status"
    payload_on: "on"
    payload_off: "off"
    value_template: >-
        {% if is_state('binary_sensor.dressing_room_pir','on') %}
        Inside
        {% elif is_state('binary_sensor.dressing_room_pir','off') %}
        Away
        {% else %}
        Unknown
        {%-endif-%}

Is there any other suggestion?


#6

Yes, that is the standard way in Home Assistant.

No, a template switch just generates a switch and gives you a lot of freedom on how to configure the control. But this doesn’t switch based on the state of a PIR sensor.

If you are just trying to control a light based on the state of a PIR, then you are on the wrong path. A binary sensor such as a PIR can either be on or off.
Have you tried setting up the binary sensor for the PIR just without the value_template? If this is an MQTT based sensor, and its payload is already on or off then there should be no need for a value_template expression.

If you want a neater representation of the PIR sensor’s state then I would first recommend setting the device_class parameter - maybe to motion or presence?
Alternatively, you can create a template sensor that translates on into Inside and off into Away. But that won’t get you anywhere closer to a light automation.
A template sensor configuration would look something like this:

sensor:
  - platform: template
    sensors:
      dressing_room_presence:
        value_template: >-
          {% if is_state('binary_sensor.dressing_room_pir','on') %}
          Inside
          {% elif is_state('binary_sensor.dressing_room_pir','off') %}
          Away
          {% else %}
          Unknown
          {% endif %}

#7

Amazing sir, you solved my problem in just one go! I love the amazing explanation and great details. It cleared my concept for this to core.

Thank you!


#8

I have this template binary sensor configured:

binary_sensor:
  - platform: template
    sensors:
      brana_otev:
        device_class: door
        entity_id:
        - sensor.gate_1
        - sensor.gate_2
        value_template: >-
            {% if is_state('sensor.gate_1', 'OFF')
               or is_state('sensor.gate_2', 'OFF')  %}
              off            
            {% else %}
              on
            {% endif %}

Or second version:

binary_sensor:
  - platform: template
    sensors:
      brana_otev:
        device_class: door
        entity_id:
        - sensor.gate_1
        - sensor.gate_2
        value_template: >-
            {{ is_state('sensor.brana_sensor', 'OFF')
               or is_state('sensor.brana_2', 'OFF')  }}

It changes its state to “on” or “off” or “true”/“false” depending on sensor.gate_1 and sensor.gate_2 states but device_class icon remains unchanged.
Do I have anything wrong in my config or should I make another template to chane icon?