Command Line Switch Variables

I created a command line switch that triggers an arduino that sends a momentary pulse to open or close my garage doors. The switch setup works great except for one thing. If I trigger the switch via the UI the switch turns yellow meaning it’s on (or open in this case). If I trigger the switch off it turns off (or closes in this case) however If I trigger the Switch to open and it turns yellow in the UI but I manually trigger the garage opener via the remote or toggle in the garage the switch in the UI stays yellow (on) because it doesn’t know the garage has been shut another way.

Would it be possible using the Command Line Switch variables, such as the value_template, to have the switch immediately return back to the off position? So the switch essentially wouldn’t be an on/off switch but more like a momentary on and then back to off. If so, what might that configuration look like?

This is my current command line switch config

switch:
  platform: command_line
  switches:
    garage door 1:
      oncmd: "/usr/bin/curl -X GET http://192.168.1.23/door/0/toggle"
      offcmd: "/usr/bin/curl -X GET http://192.168.1.23/door/0/toggle"

I think that you could to it with statecmd: always return 1.

Thanks, I’ll give that a try

How exactly should this variable look in the config? I was unable to find a similar example.

Should it just be
statecmd: 1 or =1

Or does it have to reference the switch like this:
statecmd: garage door 1 =1

Thx

Is it possible to reset the state of a switch with a service_template call? If so, would the action look something like the example below? I am trying to reset a command line switch back to off if the door is actually in a closed state.

trigger:
  platform: state
  entity_Id: xxxxx
  from: '255'
  to: '0'
action:
 service_template: >
        {% if is_state('switch.garage_door_1', 'on') %}
          state.garage_door_1=='off'
        {% endif %}

I have tried multiple attempts at getting a command line switch to update after a manual trigger of the garage door switch but have yet to find a way for it to update. The latest attempt I tried was to check the sensor on the garage door to see if it was opened or closed and then try to flip the switch based on that, unfortunately that did not work either (code below, for some reason the the switch never seemed to trigger even though the sensor was opened/closed).

If anyone has any ideas please let me know - thx

  - alias: Update Garage Door 1 Switch Status
    trigger:
      platform: state
      entity_id: sensor.vision_zg8101_garage_door_detector_alarm_level_10
    action:
      service_template: >
        {%- if is_state("sensor.vision_zg8101_garage_door_detector_alarm_level_10", "255") -%}
          switch.turn_on
        {%- if is_state("sensor.vision_zg8101_garage_door_detector_alarm_level_10", "0") -%}
          switch.turn_off
        {%- if is_state("binary_sensor.vision_zg8101_garage_door_detector_sensor_10", "on") -%}
          switch.turn_on
        {%- if is_state("binary_sensor.vision_zg8101_garage_door_detector_sensor_10", "off") -%}
          switch.turn_off
        {%- else -%}
          unknown
        {%- endif -%}
      entity_id: switch.garage_door_1

Create another switch of platform: template in your configuration.yaml right under your command_line switch

What this will do is tie a switch called switch.garage_overhead_door_1 to your garage door sensor, so that if the sensor is off, this switch will display in Home Assistant as off, and if the sensor shows on, it will display in Home Assistant as on. When you turn this switch on, it will run the service to turn on your command_line switch, and if you turn this off, it will run the service to turn off your command_line switch.

Here would be all of your code:

switch:
  - platform: command_line
    switches:
      garage door 1:
        oncmd: "/usr/bin/curl -X GET http://192.168.1.23/door/0/toggle"
        offcmd: "/usr/bin/curl -X GET http://192.168.1.23/door/0/toggle"
  - platform: template
    switches:
      garage overhead door 1:
        value_template: "{{ is_state('binary_sensor.vision_zg8101_garage_door_detector_sensor_10', 'on' }}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.garage_door_1
        turn_off:
          service: switch.turn_off
          entity_id: switch.garage_door_1

Now, remove the switch.garage_door_1 from displaying in the Home Assistant frontend, and instead use switch.garage_overhead_door_1

The switch.garage_door_1 will not be seen in the interface, so it will be a hidden switch that is only activated by the switch.garage_overhead_door_1

Now, when you turn the switch on/off in Home Assistant, it will open/close your garage door, and if you open or close the garage door outside of Home Assistant, by remote or manually, the garage sensor will cause the switch in Home Assistant to react accordingly, on or off.

If you are using customize to change the name of the switch.garage_door_1, then simply replace switch.garage_door_1 with switch.garage_overhead_door_1 so it displays with the same friendly name you currently have

So a template switch maps a visual switch to a sensor so that their states always match each other.

1 Like

@jbardi Thank you so much for the suggested code. I am excited to try this out. I can’t tell you how long and how many things I have tried that just didn’t work. The one thing that I am unsure about is how to hide the switch.garage_door_1 from displaying in the front end. Is that just by adding this to the customization section?

switch.garage_door_1:
  hidden: true

edit: I was able to hide the switch.garage_door_1 by adding the above to the customize section however when I added the switch.garage_overhead_door_1 this is how it displayed (shows “unavailable”) and I am unable to turn the switch on or off. I also added this to the customize section

switch.garage_overhead_door_1:
      assumed_state: false

edit #2 turns out it was a syntax error due to a missing “)” It is now working as a switch. Unfortunately the binary.switch does not always flip from off to on so I need to combine this into an if/else to also look at the state going from 0 to 255. Thanks again for your suggestion!

Since the binary sensor for the garage door doesn’t always change from “Off” to “On” I also have to check the sensor state going from 0 to 255. Typically one or the other will always change when opening or closing. In example 1 below the switch works as expected except when the binary sensor doesn’t change so to check for both sensors I tried the config in example 2 however I get a “missing action for switch.garage_overhead_door_1” message and the switch isn’t created. I assume it’s my syntax is incorrect however any thoughts?

example 1

  - platform: template
    switches:
      garage_overhead_door_1:
        value_template: "{{ is_state('binary_sensor.vision_zg8101_garage_door_detector_sensor_10', 'on' }}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.garage_door_1
        turn_off:
          service: switch.turn_off
          entity_id: switch.garage_door_1

example2:

  - platform: template
    switches:
      garage_overhead_door_1:
        value_template: >-
                {%- if is_state('binary_sensor.vision_zg8101_garage_door_detector_sensor_10', 'on') -%}
                  turn_on:
                    service: switch.turn_on
                    entity_id: switch.garage_door_1
                {%- elif is_state("sensor.vision_zg8101_garage_door_detector_alarm_level_10.state", "255") -%}
                  turn_on:
                    service: switch.turn_on
                    entity_id: switch.garage_door_1
                {%- else -%}
                  turn_off:
                    service: switch.turn_off
                    entity_id: switch.garage_door_1
                {%- endif -%}   

Remove .state from the end of your is_state line for checking for 255

is_state needs the entity, not the entities state:

{% elif is_state(“sensor.vision_zg8101_garage_door_detector_alarm_level_10.state”, “255”) -%}

Should be:

{% elif is_state(“sensor.vision_zg8101_garage_door_detector_alarm_level_10”, “255”) -%}

The turn_on and turn_off, being part of the template switch layout, can not be included in the value_template, they have to be stand alone.

Your value template should be an OR

{%- if is_state(‘binary_sensor.vision_zg8101_garage_door_detector_sensor_10’, ‘on’) or is_state(‘sensor.vision_zg8101_garage_door_detector_alarm_level_10’, ‘255’) -%}

This may not work properly since the template sensor should ALWAYS be on or off, but checking against 2 separate sensors that may not always coincide, you may end up with mixed results, where by it may show off if the sensor shows off or the alarm_level shows 0, and it may show on if either the sensor is on or the alarm_level is 255, which may not be the actual state of the door.

The template switches should always be tied to only 1 sensor, so the problem we should be trying to figure out is “what is wrong with the garage door sensor”, because that is your issue, all the rest of this is trying to find a work around to the problem, which is not what the answer is.

I am assuming this binary door sensor is a Zwave device, only because of the _10 at the end usually denotes a Zwave node. If that is the case, I think your problem is that your Zwave network mesh may be inadequate. I don’t know how many powered Zwave devices you have, but garage door sensors tend to be a problem for a lot of people since most people’s garage are on one side of the house or even separate from the house, therefore they are out of range of the Zwave mesh, and only turn on/off intermittently. Powered Zwave devices, that is plugged in the wall, serve as a relay device for the mesh network, but battery operated devices do not serve this same purpose, therefore, if you have no wall powered devices between your garage door sensor and the Zwave hub device, then it may be too far away to update accurately and instead is intermittently updating its state.

Your sensor should be always on or off and always coinciding with the actual state of the door. If it is not, then that has to be addressed, because a door sensor that can not accurately do the one thing it was designed for, which is to sense a binary on or off state, is no longer a door sensor and therefore should not be a part of your home automation system.

It is either broken or not communicating with the Zwave hub device, in which case it should not be relied upon as a state sensor.

I first tried adding the Or statement to the Template checker under Dev tools and it threw an error about “is_state”. I assumed it didn’t like the additional Or statement because without it there was no error.

{%- if is_state(‘binary_sensor.vision_zg8101_garage_door_detector_sensor_10’, ‘on’) or is_state(‘sensor.vision_zg8101_garage_door_detector_alarm_level_10’, ‘255’) -%}

You are right that ideally the binary sensor should work in all instances however I don’t think it’s because of the z-wave range. I have 7 zwave switches between the controller and the garage door sensors, 2 of which are actually in the garage and all work flawlessly. They are monoprice door/window sensors and have always acted this way when sometimes the binary sensor will show switching and sometimes the state will show switching. The state seems to be the more stable one almost always working but on occasion it decides not to. I haven’t figured out how or if that can be corrected. I’ll try checking the state in the value_template as that seems to be more reliable. Thanks again for your help on this!

Not a problem, I hope it can all be figured out.

As for the OR throwing an error, I’ll have to check into that as well, haven’t come across it myself. You can try forgoing the is_state and simply check the state directly against a value and see if the “or” works in that case:

{% if states.binary_sensor.vision_zg8101_garage_door_detector_sensor_10.state == ‘on’ or states.sensor.vision_zg8101_garage_door_detector_alarm_level_10.state == ‘255’ %}

When doing it with is_state, you only use the entity_id:

sensor.vision_zg8101_garage_door_detector_alarm_level_10

but when comparing the state directly, without using the is_state function, you have to reference “states” + the entity_id + the value to test against, which is “state”, so states + entity_id + state, thus

states.sensor.vision_zg8101_garage_door_detector_alarm_level_10.state

Just helps to keep these in mind for future :smiley: