Show motor valve status in hass

I have the following problem. I have a motor valve(not electromagnetic) which provides feedback by two end switches. I have the status of the end switches in separate mqtt topics:
compressor/valve1/status/opened
compressor/valve1/status/closed
and the status payload is 0 or 1.
I also have two topics that are used for controlling the valve
compressor/valve1/set/open
compressor/valve1/set/close
I need to send payload “1” to each of these topics for 6 seconds in order to fully open or close the valves(I have already managed to do this)

So my question: How can I combine the opened and closed status so I can add it to my "state_topic"in the switch configuration
For example: If compressor/valve1/status/opened = 1 and If compressor/valve1/status/closed = 0 then set valvestatus to opened and vice versa.

The idea of the two end stops and separate topics is that in case of a problem the valve can remain neither fully opened nor fully closed and then the opened status payload will be 0 and the closed status payload will also be 0.

Using those two sensors you don’t have a binary sensor. You have three states. Open (1,0), partially open (0,0) and closed (0,1). The switch template requires a binary sensor. You can make a template for fully closed or [open or partially open] simply by testing the closed state sensor.

value_template: "{{ not is_state('sensor.closed', '1') }}"

The other way I can think of doing it is by using an automation to change the state of an input_boolean depending on the state of your two sensors. You could then use that in your template. The input boolean would not change until the endstops were hit so it might be wise to write another automation to check if both sensors are zero for longer than 7 or 8 seconds to warn you that the valve is stuck.

Another idea is to use a cover for the valve instead of a switch. It has separate topics for open and closed. This would probably be the best solution.

Thank you for your suggestions. Here is how it looks like now: compressor
I have added the end switches as binary sensors and now I will try to combine each couple and represent the result in another binary sensor which I will add to the valve’s switch config. I guess I will try to add another binary sensor which will turn on in case of valve failing to reach any of the end stops. If I cannot make it this way I will try to use cover. Can you suggest a way to combine the binary sensors? Should I use binary_sensor with template platform?

Combine them with an automation:

alias: valve_state_update
trigger:
  - platform: state
    entity_id: binary_sensor.valve_1_opened
    to: 'on'
  - platform: state
    entity_id: binary_sensor.valve_1_closed
    to: 'on'
action:
   service_template: >
     {% if trigger.entity_id == 'binary_sensor.valve_1_opened' %}
       input_boolean.turn_on
     {% else %}
       input_boolean.turn_off
     {% endif %}
  entity_id: input_boolean.valve_1 # you have to create this. Use it in your switch. 

The other automation to look for a stuck valve:

alias: valve_stuck_alert
trigger:
  - platform: template
    value_template: "{{ (states('binary_sensor.valve_1_opened') == 0) and (states('binary_sensor.valve_1_closed') == 0) }}"
    for:
      seconds: 10
action:
  notify... etc

You may have issues with this. The switch will expect the state to change in a timely manner when you command it to change. i.e. Not after 6 seconds.

I still think you will be better off with a cover.

Thank you for your help!

I’d advise you to try it before making it as the solution :slight_smile: