Use sensor actual value to set new one

Hello,
Little intro: I have a sliding gate with 2 Reed switch to read Open and Close states, that works fine. When reed sensor 1 is ON and reed sensor 2 is OFF the gate is Closed and vice versa.
I’m trying to ‘fake’ opening and closing states with this template:

template:
  - sensor:
     - name: "Estado Porton"
       unique_id: estado_porton
       state: >-
        {% if is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'off') %}
          Close
        {% elif  is_state('binary_sensor.porton_sensor_porton_1', 'off')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
          Open
        {% elif  is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
          {% if is_state('sensor.estado_porton', 'Close') %}
            Opening
          {% elif is_state('this', 'Open') %}
            Closing
          {% endif %}
        {% endif %}

What I’m trying to do is when reed sensors are non aligned, based in previous state, set the opening or closing state.
But when both reed sensors are ‘on’ the {{states('sensor.estado_porton')}} goes empty.
As you can see I tried with is_state('sensor.estado_porton', 'Close') and with is_state('this', 'Open') but none work.

I am doing anything wrong? do you use something similar?

thanks.

I see at least 2 issues:

  1. You are using “if … elif … elif …” w/o “else” (i.e. may not cover all cases).
  2. The “is_state('this', 'Open')” is wrong, should be “this.state == 'Open'” or “is_state(this.entity_id,'Open')”.

Thanks for answering! I tried both of the suggested fixes this are the results:
I also add another case based on this post:
Template sensor: using “this” variable

{% if is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'off') %}
  Close
{% elif  is_state('binary_sensor.porton_sensor_porton_1', 'off')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
  Open
{% elif  is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
  {% if is_state('sensor.estado_porton', 'Close') %}
    Opening
  {% elif is_state(this.entity_id, 'Open') %}
    Closing
  {% elif is_state(this.state, 'Open') or  is_state(this.state, 'Close') %}
    state_A
  {% else %}
    state_1
  {% endif %}
{% else %}
  state_2
{% endif %}

When the gate is either Opening or Closing the sensor get the “state_1”. Its like the sensor goes “blank” while running the template ?¿

I haven’t chekced the rest of your code, but the “string_1” problem is because you don’t use it as a template variable, it’s just a string.

{% else %}
  {{ state_1 }}
{% endif %}

But where do you define these? You don’t set them anywhere?

And on a side note, you should also set a device_class for your template sensor. I’m not entirely sure if the device_class wouldn’t be a solution at all, where you wouldn’t need to set the “open” and “close” state yourself. :thinking: Anyway, it should be set: see here

I think the issue is that the template is always being evaluated when any of the entities change so it updates at strange times and the state gets confused.

try using a triggered template sensor.

the triggers will be the two gate switches so that the template is only evaluated when one of those changes state and it will ignore the state change of the “self” sensor.

I think.

1 Like

Finally I get this to work.
Thanks everyone for the answers and help.

Answering to @paddy0174 not always need to use {{ state_1 }} You can use just state_1 to set that string to the sensor value. Like this:
image

And about the triggers that @finity mentions was going to be my plan b.

To make this work I use this code

state: >-
{% if is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'off') %}
  Close
{% elif  is_state('binary_sensor.porton_sensor_porton_1', 'off')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
  Open
{% elif  is_state('binary_sensor.porton_sensor_porton_1', 'on')  and is_state('binary_sensor.porton_sensor_porton_2', 'on') %}
  {% if is_state('sensor.estado_porton', 'Close')  or is_state('sensor.estado_porton', 'Opening') %}
	Opening
  {% elif is_state(this.entity_id, 'Open') or is_state('sensor.estado_porton', 'Closing') %}
	Closing
  {% endif %}
{% endif %}

I have to add those or in the second if. I realized that looking at the Logbook. Its looks like the template is called 2 times for each sensor change.