Translating a lock in a switch

I do not want to double post, but I am opening this thread due to I think my problem is vice versa from the one I replied on earlier.

The problem described there:
Hi, I am having problems to understand. It might be due to the fact I am half ways understanding…

I have a gate. I have a sensor on the gate. I want the gate to be presented as a lock.

Gate binary sensor:
binary_sensor.gate_sensor_956

I have in configuration.yaml:

lock:
  - platform: template
    name: Gate
    value_template: "{{ is_state('binary_sensor.gate_sensor_956', 'off') }}"
    lock:
      service: switch.turn_off
      data:
        entity_id: switch.virtual_vswitch_6
    unlock:
      service: switch.turn_on
      data:
        entity_id: switch.virtual_vswitch_6

The virtual switch:

switch:
  - platform: virtual
    name: vSwitch_6

I created the virtual switch to give an on and off state.
on=closed
off=open

I use it in node red:

(2× flows right to the delay of 20s is to ensure the hate closes if the first one fails)

But a lot of time the lock/unlock does not work…

the gate is controlled via a momentary switch which I “call” in node-red on-off for 2 secs opens or closes it.

Where am I thinking wrongly?

Edit:
Sometimes it just “hangs” and lock/unlock just doesn’t work until I restart homeassistant.
When I use the “vswitch 6” I can correctly control the gate (open / close).

OTHER WORDS:
I want to achieve to just use a “normal” lock in home assistant via a template. Which works 3 out of 5 times correctly.

But I was not able to use the lock state in node red so I created a virtual switch (used by the lock). That virtual switch controls a node red flow to lock or unlock the gate by means of sending pulses (2sec on-off) and then check if the gate is locked or not.

Hope I am clear :-).

But sometimes it just doesn’t respond to lock or unlock…

I am absolutely a noob on templates but I think that is where the source of my problem lies…
If I try:

lock:
  - platform: template
    name: Tuin Voor Poort
    #value_template: "{{ is_state('binary_sensor.tuin_v_poort_sensor_956', 'off') }}"
    value_template: "{{ is_state('binary_sensor.tuin_v_poort_sensor_956') }}"
    lock:
      service: switch.turn_off
      data:
        entity_id: switch.virtual_vswitch_6
    unlock:
      service: switch.turn_on
      data:
        entity_id: switch.virtual_vswitch_6

I get:
image a greyed out “lock” button.
and the lock entity is unavailable (as described in the template documentation).

Edit:
if is is non-responding and after restarting home assistant all works well until… something happens and it goes wrong (but i cannot find out what).

value_template: "{{ is_state('binary_sensor.tuin_v_poort_sensor_956') }}"

This is an invalid template. is_state requires a 2nd variable and it will return true if it matches that variable, false otherwise.

value_template: "{{ is_state('binary_sensor.tuin_v_poort_sensor_956', 'off') }}"

This one is better, but will report locked based on this sensor state. You’re basically saying “if the gate is open, this lock is unlocked. If the gate is closed, this lock is locked” (assuming ‘off’ means open).

So far, this all looks normal. If the gate is ‘locked’, you can call the unlock function and it will call switch.turn_on to switch.virtual_vswitch_6. Assuming this moves the gate, the state should switch to ‘unlocked’ almost immediately as the sensor wont be reporting closed.

So, first thing…fix your value template. What you have in that last section will not work.

Next, what is the state of binary_sensor.tuin_v_poort_sensor_956 when nothing works? Open your dev tools and look. Is that binary sensor still working? Maybe it’s the sensor itself failing?

Thank you very much for taking the time and effort to reply to my question! Really appreciated!

I knew the template without the state was wrong. But since I read:

value_template template (Optional, default: optimistic)

Defines a template to set the state of the switch. If not defined, the switch will optimistically assume all commands are successful.

It was worth trying :-).

CORRECT!

CORRECT ALSO!
If I press lock it will turn to “locked” only if the sensor binary_sensor.tuin_v_poort_sensor_956 indeed has state “off”. And unlocked if “on” (its a zwave door sensor).

not really sure what you mean… this I have now and it also works 3/5 times correctly:

lock:
  - platform: template
    name: Tuin Voor Poort
    value_template: "{{ is_state('binary_sensor.tuin_v_poort_sensor_956', 'off') }}"
    lock:
      service: switch.turn_off
      data:
        entity_id: switch.virtual_vswitch_6
    unlock:
      service: switch.turn_on
      data:
        entity_id: switch.virtual_vswitch_6

I will monitor this!

Hence… I think a combination of working with node-red (restarting) and homeassistant (not yet restarted) make 2/5 times the gate (lock) non-responsive. I can press lock/unlock, it “lights” up but nothing happens. I can however control the gate when using the virtual switch then and the lock equally shows the correct corresponding state (locked/unlocked) thus the sensor binary_sensor.tuin_v_poort_sensor_956 reports correctly else it should not show this…

Only thing to get the “lock entity” going to work is restart home assistant…??

I will continue to monitor.

Ok, it happended again…

state is correct:

I really cannot control any of this buttons:
image

I can only “physically” control the switch.virtual_vswitch_6 and then the lock locks and the gate closes…

EDIT:
I am 100% sure the template “thing is non responsive”. Not opening and not closing (the switch). Restarting home assistant instslantly restores the functionality.

So you can’t open the service tab and call the service to do it? Or you mean you have to go push a physical button on the device itself? Seems odd.

What do the logs show in home assistant? And what does the template lock state show in the same states browser?

It’s likely due to the switch.turn_on taking a long time to report its state. Looking at the template lock code, it will take the lock/unlock command and build a “Script”. And, by default, scripts are set to only run a single time until finished. Because your virtual switch is running some node_red flow, it probably doesn’t report success/fail right away…especially with the pulse on/off check loop it does.

Can you show the config for your virtual switch? Is it a template switch or something? This will rely on the template switch state to report to correct position, and it will wait forever until the switch reports it has changed states.

This is the virtual switch component config. It is built on:

switch:
  - platform: virtual
    name: vSwitch_7
    initial_value: 'off'

I now think I see whats wrong… restarting home assistant sets value to “off”…

I need to find a “virtual switch” that remembers the state upon restart… something to find out tomorrow…