Fetch and format data from a device with a REST sensor

Dear community,

I am trying to connect my surveillance Camera with my HA. I so far managed to use a RESTful Sensor that fetches the state of the camera. and for that I send the following GET command:

platform: rest
resource: http://myInstarIPCameraAddress/cgi-bin/hi3510/param.cgi?cmd=getioattr
name: Camera

And as expected and also detailed in the camera CGI commands documentation, it returns the following, exactly in this format:

var io_enable="0";
var io_flag="1";

when I simulate the command in an API tester, it returns the same and that the Content-Type is text/html.
Where I am blocked is how to extract the 0 or 1 value and put it in a usable sensor in HA.

thanks in advance for the help,
Amir.

I’ve done a similar thing but I don’t use the rest sensor. I use a command line sensor.

I’m not sure if you can use the rest sensor for what you want because it’s not returning json values.

Here is the code for my camera motion detection sensor:

- platform: command_line
  name: "Kitchen Camera Motion"
  command: 'curl -k --silent "http://192.168.1.51:8001/get_status.cgi?user=xxxxx&pwd=yyyyy" | grep -oP "(?<=alarm_status=).*?(?=;)"'
  value_template: >-
    {%- if value == "0" -%}
      None
    {%- elif value == "1" -%}
      Detected
    {%- endif -%}
  scan_interval: 3 
  friendly_name: 'Kitchen'

Here is the result of that command when i run it from my browser:

var id='00626E52160D';
var sys_ver='11.37.2.65';
var app_ver='2.0.10.15';
var alias='Kitchen';
var now=1361908851;
var tz=28800;
var alarm_status=0;
var ddns_status=0;
var ddns_host='fi3139.myfoscam.org';
var oray_type=0;
var upnp_status=0;
var p2p_status=0;
var p2p_local_port=23849;
var msn_status=0;
var wifi_status=0;
var ppcn=0;
var temperature=0.0;
var humidity=0;
var tridro_error='';

the ‘grep’ function searches for the next thing after “alarm_status=” And before the next “;” so it will return a 0, 1 or 2 depending on the motion detected status.

You didn’t say which 0 or 1 you wanted to extract, but you could also try using value_template w/ the regex_findall_index filter, something like this:

value_template: >
  {{ value|regex_findall_index('var io_flag="[01]";')
          |regex_findall_index('[01]') == '1' }}

THanks for the reply!

Thanks! it works.

Need help with hassio . I was using the same format to get motion from my foscam when i was using homeassistant but now i cannot make it work for hassio running on ubuntu docker. I can run same curl from ssh and it gives result but on hass it gives me error.

Command failed: curl -k --silent “http://192.168.xx.xxx:xxxx/get_status.cgi?user=xx&pwd=xxx” | grep -oP “(?<=alarm_status=).*?(?=;)”

components/sensor/command_line.py (ERROR)

Any ideas how to make it work ?

the problem is that by default (and design) the hassio docker container can’t run commands on the host which is required to run the command you need.

The general procedure below will give your container access to the host command-line. It was based on a procedure written by @juan11perez (https://hastebin.com/sojasolite.sql) that I mostly followed aside from a couple of exceptions:

1 - Need to modify the host user privileges to skip typing your password with sudo

    sudo visudo

    hostuser ALL=(ALL) NOPASSWD:ALL

2 - create the following directory:

    $ mkdir /home/finity/docker/sshkey/.ssh

    Then mount this volume in HA container to preserve the sshkey generated from the HA container and 
    used to execute shell commands. Key will then persist through reboot or upgrades.

    -v /home/hass/finity/sshkey/.ssh:/root/.ssh

3 - login to container via portainer or

    $ sudo docker exec -it home-assistant /bin/bash

4 - generate sshkey. - https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

    $ ssh-keygen -t rsa (press enter, enter, enter)

5 - copy the sshkey to your host ***

    $ ssh-copy-id [email protected] (type password when prompted)

    *** this won’t work if you have an existing authorized_keys file. You have to copy the key manually into the file. Put one key per line with no lines between them.
1 Like

thank you i will give it a try :slight_smile: