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 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. 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.