Command Line Switch and ESP8266


I’m having problems getting the state of a pin on my ESP8266. I am using the command line switch component to successfully turn it on and off, no issues.

However I don’t know how to return the state of the switch/ pin…
I have got this far:
curl -k,gpio,0
(my light is connected to GPIO - 0)
The command returs:
“log”: “”,
“plugin”: 1,
“pin”: 0,
“mode”: “output”,
“state”: 1

Or when the switch is off: “state”: 0
What I don’t know how to do is abstract the 1 or 0 which I presume is what HA needs returned for the state?

YAML file is like this:

  - platform: command_line
        command_on: /home/john/
        command_off: /home/john/
        command_state: /home/john/
        value_template: '{{ value == "1" }}'

Any help much appreciated!


I think value_template: '{{ value.state == "1" }}' should work.

You can play with json in Dev Tools / Templates.

{% set value = {
"log": "",
"plugin": 1,
"pin": 0,
"mode": "output",
"state": 1
} %}
{{ value.state }}

I suggest for the state you use the MQTT sensor, not a shell script.

Thanks for the suggestions. I went to a command line switch as as soon as I configured the GPIO pin as a “switch” to control via mqtt, the votage dropped to about 1.5 volts from 3 volts… So the leds that it is powering dimmed to about half. As soon as I removed the switch the pin goes high again to 3v - and bright lights… Command line switch was a work around to mqtt not working.

As show below, as soon as I enable this device the voltage drops:

This seems to work, but the switch when the state_command is enabled doesn’t work. As soon as I comment out this line it “works” but HA doesn’t know the state. ideally I would use MQTT. But as I said, the voltage drops and the lights dim- am I configuring the esp8266 incorrectly?!

'{{ value.state == 1 }}' ?
The json returns a number.

Yes. The Jason returns a number. But the switch “switches”, then toggles back to the on state almost immediately.

I’m sorry, I hadn’t checked the logs. It looks like the json doesn’t return a number. I think this is the record in the logs:

2018-03-04 19:56:21 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 140522072987632: Sending {‘success’: True, ‘type’: ‘result’, ‘id’: 11, ‘result’: None}

So it looks like the switch is turned off again by the state_command being checked after the command to turn it on:

2018-03-04 19:56:21 INFO (Thread-17) [homeassistant.components.switch.command_line] Running command: /home/john/
2018-03-04 19:56:21 INFO (Thread-18) [homeassistant.components.switch.command_line] Running state command: /home/john/
2018-03-04 19:56:21 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=140523295211136-9>
2018-03-04 19:56:21 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=140523295211136-8>
2018-03-04 19:56:21 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 140522072987632: Sending {‘success’: True, ‘type’: ‘result’, ‘id’: 11, ‘result’: None}

Thanks for any more help.