Commandline Cover State Issues


I am trying to create a commandline cover for my garage so that I can take it off mqtt, I prefer commandline because of random misfirings I get from mqtt, the components that are on commandline never misfire, but those in mqtt do misfire. My garage is currently on mqtt cover and I am changing it to commandline cover, the problem is that I can’t get the state to indicate whether the garage is open or close on homeassistant frontend. The figure below shows the mqtt cover and commandline cover for the same door, notice how the top one can show the state and the commandline bottom one does not.


Typing “” on the url results in the json result {“POWER2”:“OFF”} or {“POWER2”:“ON”}, and for commandline cover these values must be converted into either 100 or 0 for open and closed respectively, how can I modify my value template to register 100 when the Json result is ON and 0 when the Json result is OFF. my cover config is shown below, I know its probably something small, but I cant get it to work:

- platform: command_line
        command_open: "/usr/bin/curl -X POST"
        command_close: "/usr/bin/curl -X POST"
        command_stop: "/usr/bin/curl -X POST"
        command_state: "/usr/bin/curl -X GET"

        friendly_name: Garages   

        value_template: >
          { % if '{{value_json.POWER2}}' == 'ON' % }
          { % elif '{{value_json.POWER2}}' == 'OFF' % }
          { % endif % }                   


        value_template: "{{ 100 if value_json.POWER2 == 'ON' else 0 }}"

Thanks, I’ve tried it, it assumes a constant state value, either open all the time or close all the time depending on whether I use “100 else 0” or “0 else 100” , the former shows it’s always closed and latter shows always open, meaning it is not sending updated values for Power2 or something else, does it always run the the commandstate request in the background or does it just assume the value retained? The mqtt values behave as expected still.

I also notice the stop and open buttons don’t really work as well :(:see_no_evil:

There should be at least INFO messages in the log showing what it’s doing, and possibly ERROR messages if things are failing. What do you see in the log (by which I mean home-assistant.log)?


Here are excerpts of the error log, it seems “value_json” is not recognized, also the X GET command does not seem to be going through, it seems I still have a long way to go:

Update for cover.garages fails 
‎8‎:‎47‎ ‎PM components/cover/ (ERROR) 
Error parsing value: 'value_json' is undefined (value: None, template: {{ 0 if value_json.POWER2 == 'ON' else 100 }}) 
‎8‎:‎47‎ ‎PM helpers/ (ERROR) 
Command failed: /usr/bin/curl -X GET 
‎8‎:‎47‎ ‎PM components/cover/ (ERROR) 

and for the non-functional buttons, which is confusing because the same command below works for opening the garage…

Command failed: /usr/bin/curl -X POST 
‎8‎:‎52‎ ‎PM components/cover/ (ERROR) 

I just looked at the code and if the command succeeds, then value_json should be defined (assuming the returned value is valid JSON.)

Where are you seeing this? On the Info page, or inside home-assistant.log? If the former, then check the latter. The home-assistant.log file should also contain the attempted command in an INFO message (with “Running state command:”) just before the ERROR message. Of course it’s also shown in the “Command failed:” ERROR message.

I’m confused. You say it works, but you show it as failing. Also, that command is not in your original post.

Maybe you should change /usr/bin/curl to just curl.

Thanks, I will look at the log, I was using info, I also changed the toggle to on in the other command thinking it will help, the buttons for open, stop, and close all use one command, which was initially toggle, I just changed that to on but it did not help as only the open one works, will pick it up tomorrow again

It’s a bizarre sonoff problem, manipulating the power2 switch from sonoff results in the desired states in the home assistant dashboard, the mqtt seems to pick the correct Reed switch state, the command line on the other hand seems to operate on some status that’s independent from the Reed switch state, I will raise this issue on the relevant forum, the command 100 or else 0 for Jason value works, thanks a lot…


I figured out what the problem was, the value for Power2 is not the one that I need, I needed to extract the value for switch2 (reed switch), this value is obtained by sending this command:

which results in:


however when I try to use a similar logic that you gave me yesterday on the nested json:

value_template: "{{ 100 if value_json["StatusSNS"]["Switch2"] == 'ON' else 0 }}"

I get this error when validating config:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/cover.yaml", line 31, column 9
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/cover.yaml", line 45, column 48 

I also sorted the issue with the buttons that were not working, the problem was the additional “?20” just before the “Toggle” as you can see below:


I think after the nested json parsing is sorted the values will reflect the true status of the garage.

You’re not nesting quote characters properly. It should be:

value_template: "{{ 100 if value_json['StatusSNS']['Switch2'] == 'ON' else 0 }}"

or even better:

value_template: "{{ 100 if value_json.StatusSNS.Switch2 == 'ON' else 0 }}"

thanks a lot, it checks out on the config:


and I tested it on the garage as well and it works perfectly :smile::smile::smile::smile: thanks for staying with me on this one.

In terms of the nesting, I got that from homeassistant page, I will try and edit it so that others do not fall into the same trap.: