How to toggle State with a single signal binary sensor

Hi everyone, I’m really new here, but I’ve tried a few days ago to solve a simple problem but I did not find anything in the forums.

I have a binary sensor that only sends one single signal, I connected it to a slide door in a way that when opening and closing the same signal is sent via mqtt.

How can I create an automation so that I can toggle the On and Off to each opening and closing using the same signal sent.

Well, you can get out of sync if home assistant restarts and the door is open. But make an input boolean to store the state, then make an automation that toggles the state when the sensor is triggered. Then you can make a binary_sensor template that says open/close as a sensor.

input_boolean:
  slide_door:
    name: Slide door status
    initial: off
- alias: toggle door
  trigger:
    - platform: state
      entity_id: binary_sensor.xxxxx
      to: 'on'
  action:
    - service: input_boolean.toggle
      entity_id: input_boolean.slide_door
binary_sensor:
  - platform: template
    sensors:
      sliding_door:
        friendly_name: Sliding Door
        device_class: door
        value_template: "{{ is_state('input_boolean.slide_door','on') }}"
1 Like

petro,

thank you for the clarity and quality of the proposed solution. Worked perfectly

I would like to make only one complement, in the sensor declaration it is important that the force_update parameter be enabled, without this the automation will only work once.

     - platform: mqtt
       name: "Slide Door Sensor"
       state_topic: "tele / sonoff-bridge / RESULT"
       value_template: '{{value_json.RfReceived.Data}}'
       payload_on: "XXXXX"
       retain: false
       force_update: true
1 Like

Hi,

I am facing the same issue. I have a sensor that send outs an signal for both open and close the same i.e. open = 101 close = 101.

Now I have 16 of these sensors that I want to use but to do all of these automations and seperate lines of code is a bit overkill.

     - platform: mqtt
       name: "Slide Door Sensor"
       state_topic: "tele / sonoff-bridge / RESULT"
       value_template: '{{value_json.RfReceived.Data}}'
       payload_on: "XXXXX"
       retain: false
       force_update: true

Is it possible to have it set in this code? Or are those automations a must? I want to keep it a simple as possible becauwse the amount of sensors.

thanks

There is an easier way as described in:

No need of automations or input booleans at all.

My config yaml for each sensor:

  - platform: mqtt
    name: "Puerta Entrada"
    state_topic: "Rfsensor/Puerta_entrada"
    value_template: "{%if is_state(entity_id,\"on\")-%}OFF{%-else-%}ON{%-endif%}"
    force_update: true
    device_class: door       

To get different state topics for each sensor I use rules on tasmota.

An example of a Rule of this kind in tasmota:

Rule1 ON RfReceived#Data=107C56 DO publish Rfsensor/Puerta_entrada ENDON ON RfReceived#Data=1420C6 DO publish Rfsensor/Sensor_prueba ENDON

Keep in mind Tasmota can only use 3 Rules sets.

Each rule set can contain up to 511 characters so depending on the legth of the state topic you would like to publish, can fit up to 9-10 sensors per rule set.

thank you I looked all day how to do this and no one knew and this work excellent for me
so thank you

Hi guys,

I was facing the same issue, having a single signal rf sensor, which is supposed to toggle an on/off state. This was my first Jinja template and the hardest part for me was converting the single line example into a multiline template. I kept getting the TemplateSyntaxError: unexpected char ‘\\’ until I finally escaped my troubles by removing the backslashes from the scalar. It took me quite some time to figure it out, but I’ve learned something and I’m happy with my template, so it was worth it and since you guys inspired all of this, I wanted to leave a big thank you here and share my newly acquired wisdom and happiness. I hope you’ll find it useful as well.

binary_sensor:
  - platform: mqtt
    name: "Door Sensor"
    state_topic: "tele/rf-bridge/RESULT"
    value_template: >-
      {%-if value_json.RfReceived.Data == "xxxxx"-%}
        {%-if is_state(entity_id,"on")-%}OFF{%-else-%}ON{%-endif-%}
      {%-else-%}
        get_state(entity_id)
      {%-endif%}
    device_class: door

A few notes:

  1. To safe yourself some trouble make sure to replace rf-bridge with your device’s topic and xxxxx with your sensor’s data.
  2. I’m not sure if all the hyphens are required or helpful, that’s something I’ll probably figure out when I’ve worked a bit more with Jinja or YAML, but since the example led with it I figured I’d stick to it.
  3. The template seemed to be working fine without the {%-else-%} get_state(entity_id), but I felt bad for letting it hang without any return value. So, as far as I’m concerned, it’s only there for good manners and style, but I’m not sure if it does any good or harm.
2 Likes

muchas gracias amigos!!! los re i love you!!! no se imaginan hace cuanto tiempo estoy buscando esta solución!!! MUCHAS GRACIAS GENIOS