Can I create an empty/dummy binary_sensor?

I want to use my IP-cameras as motion detectors, but there is no official component for it, but I can through my nvr-software (Blue Iris) make http calls on motion.

But I can’t figure out any way to create an empty/dummy binary sensor. I could use input_boolean but it feels wrong, I could aslo just call a sensor in the web request that isn’t configured and HA will create it. But then that sensor won’t exist until first motion… So not an optimal solution.

This will create a new sensor, but I would prefer it that sensor was setup directly on start.

curl -X POST -H "x-ha-access: PASSWORD" -H "Content-Type: application/json" -d '{"state": "on", "attributes": {"friendly_name": "Motion Livingroom"}}' http://127.0.0.1:8123/api/states/binary_sensor.motion_livingroom

Any kind of binary sensor you create is going to actively try to determine the state of that sensor using the specific methodology for that sensor. So you COULD create a command binary sensor, but the command will be constantly running attempting to determine the state of the sensor. The HTTP sensor is exactly what you are looking for. It is only updated when the API for it is called. As you correctly identify, the sensor does not exist until it has a state change. I guess I don’t see the down side. Unless you have an automation that says “do this if absolutely nothing happens”. In any other case, when the call is made, the state changes, and any associated changes are fired.

If you want to SEE that nothing has happened, then an input boolean is the right answer. You could set the value of the boolean with your HTTP call.

1 Like

I do the same thing with my BlueIris install. So far I just let it create the sensor on first motion, but I’d like for it to pre-exist when starting HASS with a default value of false/no motion.

I created a template binary sensor for this which gets its state from a http binary sensor

binary_sensor:     
  - platform: template
    sensors:
      camera_front:
        friendly_name: 'Front camera'
        device_class: motion
        value_template: "{{ states.binary_sensor.camera_front_input.state == 'on' }}"
        entity_id: binary_sensor.camera_front_input

This is exactly what I had to do for my GogoGate2 garage opener I have which isn’t a component in HA.

I wish there was an easier way but I seem to get it to work.

So I create a command line “cover” to control the garage opener.

cover:
  platform: command_line
  covers:
    garage_door:
      command_open: curl --silent -X GET https://maker.ifttt.com/trigger/OpenMyGarage/with/key/KEY?value1=Opened
      command_close: curl --silent -X GET https://maker.ifttt.com/trigger/CloseMyGarage/with/key/KEY?value1=Opened
      command_state: /home/pi/hass.shell.commands/garage/checkG2Sensor.sh

Open and Close command is straightforward, it calls IFTTT maker to call GogoGate2 IFTTT functions to open and close.

The tricky part is the command to set the state for this “cover” component.

First, I created a template sensor to keep track of the garage opener state. This sensor is also used on my Dashboard.

  - platform: template
    sensors:
      gogogate4dash:
        value_template: >-
            {%- if is_state('sensor.gogogate2', '0') -%}
            CLOSED
            {%- elif is_state('sensor.gogogate2', '100') -%}
            OPEN
            {%- else -%}
            CLOSED
            {%- endif -%}            

As you can see,it checks the state of an HTTP sensor (sensor.gogogate2) which isn’t existed when HA starts; but that’s ok… as I treat it initially as “CLOSED” (in the last ‘else’ block), then on the first motion, it’d be updated accordingly.

Here is how it all works together:

When my garage close or open, Gogogate2 sends me a notification on the phone, I setup Tasker to detect that and call HA to update the HTTP sensor; with ‘{“state”: “0”}’ for closing and ‘{“state”:“100”}’ for opening. That would update the template sensor with proper status (CLOSED or OPEN)

Then in the command line script (that updates the cover component status listed above) will keep checking this template sensor.

#!/bin/bash
U="https://nguyenha.duckdns.org/api/states/sensor.gogogate4dash?api_password=HA_PASS"

R=`curl --silent -X GET $U | jq -r '.state'`

if [[ $R == "CLOSED" ]] ; then
   echo "0"
else
   echo "100"
fi

And voila, as HA starts, my Cover component is working with proper state (assuming the garage is actually closed, hahaha)

It looks like a complicated process but it has to be this way :slight_smile:

Hi there,

Im trying to do the same thing with my GoGogate2 device, im basing my configration off your code.

Im a newb when it comes to this stuff, please excuse me, and im running Hass.IO

I dont understand what the role of the below plays, can you help me understand how this works ?

command_state: /home/pi/hass.shell.commands/garage/checkG2Sensor.sh

Hi, yes, that is the command line script that will check the status of the HTTP sensor using “curl” command.

The code listed on my post but here it is again:

#!/bin/bash
U=“https://nguyenha.duckdns.org/api/states/sensor.gogogate4dash?api_password=HA_PASS

R=curl --silent -X GET $U | jq -r '.state'

if [[ $R == “CLOSED” ]] ; then
echo “0”
else
echo “100”
fi

Blockquote
Hi, yes, that is the command line script that will check the status of the HTTP sensor using “curl” command.
The code listed on my post but here it is again:

I cant seem to get the script to run properly ??

Im getting a load of errors in the log when enabling the sensor, could you check if im doing this right ?

Log Error Output :

ValueError: invalid literal for int() with base 10: 'None'
2017-08-20 21:08:22 ERROR (SyncWorker_2) 
[homeassistant.components.cover.command_line] Command failed: 
/config/gogogate/check_garage_Sensor.sh
2017-08-20 21:08:22 ERROR (MainThread) [homeassistant.helpers.entity] Update for 
cover.garage_door fails
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 225, in 
async_update_ha_state
yield from self.hass.async_add_job(self.update)
File "/usr/lib/python3.6/asyncio/futures.py", line 331, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.6/asyncio/tasks.py", line 244, in _wakeup
future.result()
File "/usr/lib/python3.6/asyncio/futures.py", line 244, in result
raise self._exception
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/lib/python3.6/site-packages/homeassistant/components/cover/command_line.py", 
line 139, in update
self._state = int(payload)
ValueError: invalid literal for int() with base 10: 'None'
2017-08-20 21:08:22 ERROR (SyncWorker_11) 

[homeassistant.components.cover.command_line] Command failed: 
/config/gogogate/check_gate_Sensor.sh
2017-08-20 21:08:22 ERROR (MainThread) [homeassistant.helpers.entity] Update for 
cover.gate_door fails
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 225, in 
async_update_ha_state
yield from self.hass.async_add_job(self.update)
File "/usr/lib/python3.6/asyncio/futures.py", line 331, in __iter__
yield self  # This tells Task to wait for completion.
File "/usr/lib/python3.6/asyncio/tasks.py", line 244, in _wakeup
future.result()
File "/usr/lib/python3.6/asyncio/futures.py", line 244, in result
raise self._exception
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/lib/python3.6/site-packages/homeassistant/components/cover/command_line.py", 
line 139, in update
self._state = int(payload)

Configuration.yaml:

sensor: 
- platform: template
   sensors:
     check_garage_Sensor:
       value_template: >-
        {%- if is_state('sensor.garage_gogogate2', '0') -%}
        CLOSED
        {%- elif is_state('sensor.garage_gogogate2', '100') -%}
        OPEN
        {%- else -%}
        CLOSED
        {%- endif -%}

- platform: template
  sensors:
    check_gate_Sensor:
      value_template: >-
        {%- if is_state('sensor.gate_gogogate2', '0') -%}
        CLOSED
        {%- elif is_state('sensor.gate_gogogate2', '100') -%}
        OPEN
        {%- else -%}
        CLOSED
        {%- endif -%}            

cover:
  - platform: command_line
    covers:
       garage_door:
       command_open: "curl --silent -X GET https://maker.ifttt.com/trigger/Garage_Opened/with/key/MYKEY?value1=Opened"
    command_close: "curl --silent -X GET https://maker.ifttt.com/trigger/Garage_Closed/with/key/MYKEY?value1=Opened"
    command_state: "/config/gogogate/check_garage_Sensor.sh"
    friendly_name: 'Garage Door'


 - platform: command_line
     covers:
        gate_door:
        command_open: "curl --silent -X GET https://maker.ifttt.com/trigger/Gate_Opened/with/key/MYKEY?value1=Opened"
    command_close: "curl --silent -X GET https://maker.ifttt.com/trigger/Gate_Closed/with/key/MYKEY?value1=Opened"
    command_state: "/config/gogogate/check_gate_Sensor.sh"
    friendly_name: 'Gate Door'

check_garage_Sensor.sh : I put this file into /config/gogogate/

#!/bin/bash
U="http://10.0.4.6/api/states/sensor.garage_gogogate2?api_password=MYPASSWORD"
R=`curl --silent -X GET $U | jq -r '.state'`
if [[ $R == "CLOSED" ]] ; then
echo "0"
else
 echo "100"
fi

Can you run this command and see what you get?

curl --silent -X GET "http://10.0.4.6/api/states/sensor.garage_gogogate2?api_password=MYPASSWORD"

Next thing I’d check is if your system has “jq” installed? that’s a little utility to parse json for linux.
Look here:
Redirecting to jqlang.github.io and tutorial how to use it here Redirecting to jqlang.github.io

In the script I was using it to parse the json result from the above command to look for the “state”.

Hi, really appreciate your replies to my cries for help here. Just wanted to get that out of the way first.

OK, I can confirm i have jq installed, when issuing the jq command in the console, im getting the syntax.

Also ive figured out a couple things wrong

U="http://10.0.4.6/api/states/sensor.garage_gogogate2?api_password=MYPASSWORD"

My template sensor is actually called:

check_gate_Sensor

Corrected syntax :

U="http://10.0.4.6:8123/api/states/sensor.check_gate_Sensorapi_password=MYPASSWORD"

And, since i havent enabled external access via HTTPS, the port number of hass.io needed to be specified.

I thought this was enough to fix it, but unfortunately still getting errors in the log:

ERROR (SyncWorker_16) [homeassistant.components.cover.command_line] Command 
failed: /config/gogogate/check_gate_Sensor.sh
2017-08-21 20:46:11 ERROR (MainThread) [homeassistant.helpers.entity] Update for 
cover.gate_door fails

I ran the curl command from the console :slight_smile:

➜  / curl --silent -X GET "http://10.0.4.6:8123/api/states/sensor.garage_gogogate2dash?
api_password=MYPASS"
{"attributes": {"friendly_name": "garage_gogogate2dash"}, "entity_id": 
"sensor.garage_gogogate2dash", "last_changed": "2017-08-21T10:45:52.483130+00:00", 
"last_updated": "2017-08-21T10:45:52.483130+00:00", "state": "CLOSED"
}#

getting closer, not quite there. Any ideas ?

So the cover’s code:

cover:
platform: command_line
covers:
garage_door:
command_open: curl --silent -X GET https://maker.ifttt.com/trigger/OpenMyGarage/with/key/KEY?value1=Opened
command_close: curl --silent -X GET https://maker.ifttt.com/trigger/CloseMyGarage/with/key/KEY?value1=Opened
command_state: /home/pi/hass.shell.commands/garage/checkG2Sensor.sh

You can see the “command_state” will take whatever value that script returns, in this case, it will return either 0 or 100 (for open and close)

At least you will see that? on the UI, what is the state for that cover element?
Also, not sure why your script fails if you can run that curl command.

Can you run the script though? I mean manually, just run “/config/gogogate/check_gate_Sensor.sh” … to see what error it actually throws.

The cover entity status is “unknown” so the script isn’t executing for some reason.

I can run it manually in the shell using bash /config/config/gogogate/MYSCRIPT.sh

But when I do this the return is empty, so I’m not sure if it executed.

Been looking around the forums, others have reported it could be permissions on the file. I’ve chmod the file to 777, still no dice.

Log keeps throwing errors.

I forgot to mention that on the server first load, it will be UNKNOWN then after your open or close the garage for the first time, it will save the status properly.

See if you could try that?

OK, ive figured this out.

There was actually nothing wrong with the code, or yaml config.

The reason for the script failing to execute was simply because the file was edited in Notepad ++ and was encoded with ‘Windows CR LF’.

So anyone else who has issue with script execution, make sure your .sh file is ’ UNIX (LF)’ formatted if your using Notepad ++

Heres how:
Notepad++. From the “Edit” menu, select “EOL Conversion” -> “UNIX/OSX Format”

Otherwise if your a Linux guru, then use VI, and you wont have this problem. Nor would you of noticed it in the first place.

So now how do i update the sensor externally via android Tasker again ?

I think ive worked out the way to update the cover using the RestAPI POST method.

However, what seems to be happening is the cover isnt changing state. even though the sensor has been updated.

I can verify in the UI, that the sensors entities has the correct state, and is updating based on when i update it with state: 100, or 0 :

The cover entity state seems unchanged no matter what the sensor value is; above screenshot the sensor is 0 - closed. but the screenshot below of the cover says 100 - open
image

This is also reflected in the UI, Sensors say Closed, Cover says Open ??

I can verify the HTTP sensor is updating using the script i made run off the terminal on hass.io

Code to update the HTTP sensor :

curl -X POST -H "x-ha-access: PASSWORD" \
   -H "Content-Type: application/json" \
   -d '{"state": "100"}' \
   http://10.0.4.6:8123/api/states/sensor.gate_gogogate2

Changing the HTTP sensor changes the state of the HTTP sensor and the Dash Sensor, but the actual cover itself isnt changing at all ??

No errors in the logs i can see.

Any ideas ?

Im stuck now, dont know why the command_state: command wont work.

It seems to be running the script, and its returning the correct values when the sensor is updated.

Ive tried all sorts of things, one thing I noticed is that the command_line cover seems to only pickup this part of the script.

else
   echo "100"
fi

i think this could be a bug

Ive spent few days now, might give up in another day if i cant get this working. Any other ideas ?

I have this working now :slight_smile:

didnt use the command_state: used API to update the cover status directly.

use Homekit to display door open/close

Thanks for your help

Been away for a while… bug glad you got it working!!!

Hey guys. Before i even get to this part to integrate with GGG2 with HA, my remote access is wonky.

So a slightly off topic question, how stable is your remote access? Mine works 2-3 times out of 10 times i tried. Most of the time it says request timeout. I’ve forwarded ALL ports and use static IP but it’s still the same. Without a stable remote access = no IFTTT. :frowning:

Any help on why this is happening?