Command line switch with curl works/ command_state not

I have a switch (=toggle switch) that can be turned on , but the state won’t work.
When I put in safari browser http://192.168.0.141:8080/#entry/0/4 I can see that the light is on or off like this: when the light is on it shows colored in grey and off is white.
Any idea how to fix it?
I’ve been searching for some time to resolve it…

switch:
  - platform: command_line

tablet:
        command_on: "curl 'http://192.168.0.141:8080/login/validate/' \
-XPOST \
-H 'Cookie: PHPSESSID=xxxxxxxxxx' \
--data 'login_username=xxx&login_password=xxx' -L http://192.168.0.141:8080/action/6640/"

        command_state:  "curl 'http://192.168.0.141:8080/#entry/0/4/' \
-XGET \
-H 'Cookie: PHPSESSID=xxxxxxxxxx' \
-H 'Accept: text/html, */*; q=0.01' \
-H 'Accept-Language: nl-be' \
-H 'Host: 192.168.0.141:8080' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15' \
-H 'Referer: http://192.168.0.141:8080/' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Connection: keep-alive' \
-H 'X-Requested-With: XMLHttpRequest'"

        
       
        value_template: '{{ value == "on" }}

I feel your pain. I’ve created a command-line PIR occupancy sensor. Documentation says its state can be “On” or “Off”. My sensor returns precisely “On” or “Off”, yet Home Assistant doesn’t ever see any change in the sensor. FYI, I’ve tried the values “ON” and “on” as well as “True” and “true” and those did not change anything. With a numeric value, it’s simple. With a Boolean who knows?

same here, I’ve tried also many values without luck…So frustrating

What is the output of your command_state curl command if you enter it in a console?

1 Like

@VDRainer In my particular case, this is my configuration.yaml config text:

#
# PIR Occupancy Sensors
#
binary_sensor:
  - platform: command_line
    name: Sunroom Occupancy
    scan_interval: 5
    command: "curl http://192.168.1.37"
    command_timeout: 3
    value_template: '{{ value }}'

And the output of that curl command is:

< !DOCTYPE HTML>< html>
Off
< /html>

I added a space after the left-angle-bracket/less-than symbols so that it displays properly here. I’d guess I need to update my value_template line to include the html and /html tags? OR simply drop all the HTML encoding from the sensor. Which seems to work just fine for my two RPi/Python sensors that return decimal values for Lux illumination sensors.

try this as the value template:

value_template: "{{ value | regex_findall_index('<html>(.*)</html>') }}"

1 Like

Thank you, @finity. I wound up dropping the HTML tags and now the sensor is displaying ‘On’ or ‘Off’ without them, although the value is still not translating into HA. :frowning: FYI, this is the updated output of the curl command:

fred@hassio:~$ curl http://192.168.1.37/
On
fred@hassio:~$

try just creating a “sensor” instead of a “binary sensor”.

It should return the same “On” or “Off” state and then you can use that state in any automations as is and you don’t have to worry about it not being in the exact correct format for a binary sensor.

Thank you! I just added the following two lines to my configuration, and now the sensor appears to be reading status correctly:

payload_on: 'On'
payload_off: 'Off'

Now I need to see about holding the ‘On’ status higher for a bit longer, and insuring the lamp automation follows it correctly. :grinning:

with terminal output is some strange characters but have a http200 so it is received well

In safari with the developmenttool I have this where you can see I have a http200 so it is received:

And take a look at the li-negative when the light is on:

If the output on the HA console is the same, how should HA determine the state of your light?
That would be AI if it can.
You need something other for the state command, maybe a Scrape Sensor, or something.

@VDRainer
Do you mean that the sensor can scrape for the negative color to see if the light is on?
I will give it a try but any idea how to describe the value_template in the scrape sensor ? Something like:
Thanks for the support.

value_template: '{{ value.negative = on }}'

I just tried this without succes :

 - platform: scrape
    resource: http://192.168.0.141:8080/entry/0/4
    name: ikke1
    select: "Led vensters"
    value_template: '{{ Li-negative }}'
    # Request every hour
    scan_interval: 10
    headers:
      -H 'Cookie: PHPSESSID=1sudbnm3ipdea31uv89sp734n0' \
      -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
      -H 'Upgrade-Insecure-Requests: 1' \
      -H 'Host: 192.168.0.141:8080' \
      -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15' \
      -H 'Accept-Language: nl-be' \
      -H 'Accept-Encoding: gzip, deflate' \
      -H 'Connection: keep-alive'"

anyone experience with this scrape sensor?

I forgot to mention that I can see the sensor but with the unknown status

Sorry, i have absolutely no clue about scraping websites.
Maybe post the whole html in another topic and ask for help.
Which kind of lights are these? Isn’t the any other query to get the state?

No need for sorry,
These are lights from my home domotica system that I can acces true a webserver where I have to log in.
I spend a lot off time to get them work into HA with curl (= because of the credentials needed to acces the webserver). So I can control all the lights with HA but without getting the states of the switches I can make no automations and the other cool stuff that HA is made for :wink:

I think I just found something to work on:
When I enter in safari : http://192.168.0.141:8080/entry/0/4 I get this for the states:

But my curl command in HA to turn on a light have a number in comparisation for the above pic like:
For example action/6640 is my light called Led venster like you can see in the screenshot above

command_on: "curl 'http://192.168.0.141:8080/action/6640"

So for the command state :

command_state: "curl 'http://192.168.0.141:8080/entry/0/4/action/6640"

I think I just need the right description for the value_template and this one is not working:

value_template: '{{ value == "light_on" }}'

Am I on the right track here or is this impossible?

Reviewing the ‘dev-state’ (’< >’ icon on the sidebar) I realized the final source of my issue. After removing the HTML, after latching the occupancy status returned for X minutes, it still was not firing the automation. The issue was the value in my automation.yaml file. I used sentence-case ‘On’ and HA expects ‘on’ in lowercase. I’d have thought HA would force lowercase before comparing, but oh well, I’ve learned yet another feature of HA. :wink:

That’s why I advised to create a sensor instead of a binary sensor since the results of a sensor can be anything but the binary sensor needs a specific on/off state.