Grep command getting status from a camera

Hi,

I have made this work with my Vivotek cameras, but i struggle how to use the grep command to get the status i need.
Anyone who know how i can use the grep to get the right info?

From the URL: http://root:[email protected]/cgi-bin/admin/getparam.cgi?privacymask_c0_enable
I get two anwers:

Privacy mask off: privacymask_c0_enable=‘0’
Privacy mask on: privacymask_c0_enable=‘1’

Anyone that could help me with the correct grep command?

- platform: command_line
  switches:
    vivotek_switch:
      command_on: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=0"'
      command_off: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=1"'
      command_state: 'curl -k --silent "http://root:[email protected]/cgi-bin/admin/getparam.cgi?privacymask_c0_enable" | grep -oP "(?<=isEnable>).*?(?=</isEnable>)"'
      value_template: '{{ value == "1" }}'

You could pipe the output of curl to sed "s/privacymask_c0_enable='\([0-9]\)'/\1/" to extract the number in single quotes.
If you really wanted to use grep, you could do something like grep -q "privacymask_c0_enable='1'" && echo 1 || echo 0. Please note that this will echo 0 even if the real value is e.g. 4.

Thank you for the reply :slight_smile:
So if i was gonna use the grep command, what would the value_template look like?

- platform: command_line
  switches:
    vivotek_switch:
      command_on: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=0"'
      command_off: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=1"'
      command_state: 'curl -k --silent "http://root:[email protected]/cgi-bin/admin/getparam.cgi?privacymask_c0_enable" | grep -q "privacymask_c0_enable='1'" && echo 1 || echo 0"'
      value_template: '{{ value == "1" }}'

I would just avoid messing with the quotes altogether and put the command on a separate line.

- platform: command_line
  switches:
    vivotek_switch:
      command_on: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=0"'
      command_off: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=1"'
      command_state: >-
        curl -k --silent "http://root:[email protected]/cgi-bin/admin/getparam.cgi?privacymask_c0_enable" | grep -q "privacymask_c0_enable='1'" && echo 1 || echo 0
      value_template: '{{ value == "1" }}'

As for the value_template, I think the whole thing could be significantly simplified. According to Command line Switch - Home Assistant, we can use grep’s exit code.
Maybe something like

- platform: command_line
  switches:
    vivotek_switch:
      command_on: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=0"'
      command_off: 'curl -k "http://root:[email protected]/cgi-bin/admin/setparam.cgi?privacymask_c0_enable=1"'
      command_state: >-
        curl -k --silent "http://root:[email protected]/cgi-bin/admin/getparam.cgi?privacymask_c0_enable" | grep -q "privacymask_c0_enable='1'"

(I have not tested this.)

I will test them, thank you :slight_smile: So far the switch just keeps going back to the state it was before i pressed it. After like 1-2 seconds.
But ill see what i figure out. This gave me a mile ahead of where i was :slight_smile:

Found this, is your problem similar? Does the switch eventually go to the correct state? (Maybe you need to set scan_interval.) We need to make sure the command is actually working as expected.

I actually realized when i added my password back that i marked out as “XXXX” i wrote it wrong.
So when i changed that it does actually work as intended :slight_smile:

On when on, and off when off. And it changes the privacy mask on the camera too.

And yes, it changes even state if i manually turn things on and off in my browser. Just a little slow.