Problem with command_line sensor

Hello,

I try to make a sensor that gets the aviation weather data from de closest airport.
I made 2 sensors that give me my latitude & longitude: sensor.erik_latitude & sensor.erik_longitude
No problem so far.

When I create a command_line sensor like this it works and I receive the correct data:

  - platform: command_line
    name: close_metar
    command: >-
      curl 'https://api.checkwx.com/metar/lat/51.12345/lon/4.12345' -H 'X-API-Key: xxxx'
    value_template: "{{ value_json['data'][0] }}"

Now I want to receive the data with variable lat & lon like this:

- platform: command_line
  name: close_metar
  command: >-
    curl 'https://api.checkwx.com/metar/lat/{{states.sensor.erik_latitude.state}}/lon/{{states.sensor.erik_latitude.state}}' -H 'X-API-Key: xxxx'
  value_template: "{{ value_json['data'][0] }}"

In the HASS log I have this ERROR:

[homeassistant.components.sensor.command_line] Command failed: curl https://api.checkwx.com/metar/lat/51.12345/lon/4.12345 -H X-API-Key: xxx

It looks like the ’ are lost. I already tried to use ".

Any ideas?

thank you,
E.

Try:

  command: >-
    curl {{ "'https://api.checkwx.com/metar/lat/" ~ states.sensor.erik_latitude.state ~ "/lon/" ~ states.sensor.erik_latitude.state ~ "' -H 'X-API-Key: xxxx'" }}

What about this?:

command: >-
  'curl "https://api.checkwx.com/metar/lat/{{states.sensor.erik_latitude.state}}/lon/{{states.sensor.erik_latitude.state}}" -H "X-API-Key: xxxx"'

Negative, same ERROR

Thank you anyway for the reply!!

Already tried that. Same Error

Thx!

Weird. That’s the format I use for my shell commands:

cinema_kodi_input_back: 'curl -u "kodi:xxxxx" --header "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"Input.Back\"}" "http://10.1.1.3:8080/jsonrpc"'

The only other thing I can think of is escaping the ’ with another '. Like this:

So where you have single quotes use two single quotes. But I don’t think this is the case here as you are not using a single quoted '.

Then try putting it all on one line like this:

command: "curl 'https://api.checkwx.com/metar/lat/{{states.sensor.erik_latitude.state}}/lon/{{states.sensor.erik_latitude.state}}' -H 'X-API-Key: xxxx'"
1 Like

Yeah, did that too :slight_smile:

E.

And it’s still not working? If so, then there’s something weird going on. What is the error message when you use my last suggestion?

EDIT:

Try putting this in the template editor and let me know what you get:

{{ '>' ~ states.sensor.erik_latitude.state ~ '<' }}
{{ '>' ~ states.sensor.erik_longitude.state ~ '<' }}

EDIT2:

D’oh! I just noticed a typo that was in your original template that I’ve been inadvertently copying. You had states.sensor.erik_latitude.state for both lat & lon!

So, try:

command: "curl 'https://api.checkwx.com/metar/lat/{{states.sensor.erik_latitude.state}}/lon/{{states.sensor.erik_longitude.state}}' -H 'X-API-Key: xxxx'"

When I do this I get the right coordinates between the > <

I already catched the typo myself but this has no effect on the outcome:

Command failed: curl https://api.checkwx.com/metar/lat/51.0509735/lon/4.3440158 -H X-API-Key: xxxx

I’m trying to find the problem for about 7 hours now… :wink:

E.

Ok, I’m going to give a similar command a try and see if I can work out the issue. Stay tuned…

Ok, figured it out. Seems the command_line sensor first splits the command from the arguments by doing a split(’ ', 1), then whatever was to the right of the first space becomes the arguments, and only the arguments are evaluated as a template. So:

command: curl "'https://api.checkwx.com/metar/lat/{{states.sensor.erik_latitude.state}}/lon/{{states.sensor.erik_longitude.state}}' -H 'X-API-Key: xxxx'"
 ERROR (SyncWorker_6) [homeassistant.util.yaml] mapping values are not allowed here   in "/home/homeassistant/.homeassistant/sensor.yaml", line 505, column 150

Wow. It just keeps getting weirder. FWIW, I tried this which works fine for me:

sensor:
  platform: command_line
  command: curl "'https://api.sunrise-sunset.org/json?lat={{ states.zone.home.attributes.latitude }}&lng={{ states.zone.home.attributes.longitude }}'"
  value_template: "{{ value_json.results.day_length }}"

Obviously it’s a different website, and I’m using different entities for the lat & lon, but it works. Oh, and this is in a package file, hence the sensor: keyword.

EDIT: And this is what I see in my log:

2018-07-26 09:38:36 INFO (Thread-14) [homeassistant.components.sensor.command_line] Running command: curl 'https://api.sunrise-sunset.org/json?lat=REDACTED&lng=REDACTED'

Can you show exactly what you have now? This sounds more like a YAML issue than a command_line sensor issue.

  - platform: command_line
    name: close_metar
    command: "curl 'https://api.checkwx.com/metar/lat/{{ states.sensor.erik_latitude.state }}/lon/{{ states.sensor.erik_longitude.state }}' -H 'X-API-Key: xxxx'"
    value_template: "{{ value_json['data'][0] }}"

result in HASS log:

ERROR (SyncWorker_10) [homeassistant.components.sensor.command_line] Command failed: curl https://api.checkwx.com/metar/lat/51.1234567/lon/4.1234567/ -H X-API-Key: xxxx

Keep in mind that this is perfectly working:

  - platform: command_line
    name: close_metar
    command: "curl 'https://api.checkwx.com/metar/lat/51.1234567/lon/41.1234567' -H 'X-API-Key: xxxx'"
    value_template: "{{ value_json['data'][0] }}"

So strange…

Hi,

I found an other website to use and all works like a charm.
Thank you for you time!

E.

It was still failing because you didn’t fix it like I suggested. You still have the first double-quote character in the wrong place. It needs to be right before the beginning of the arguments, not before the curl command. I.e., not this:

command: "curl 'https://api.checkwx.com/metar/lat/{{ states.sensor.erik_latitude.state }}/lon/{{ states.sensor.erik_longitude.state }}' -H 'X-API-Key: xxxx'"

but this:

command: curl "'https://api.checkwx.com/metar/lat/{{ states.sensor.erik_latitude.state }}/lon/{{ states.sensor.erik_longitude.state }}' -H 'X-API-Key: xxxx'"

Notice where the first " is.

BTW, the reason the other one works as-is (i.e., the one with the fixed lat & lon), is because it’s not using a template. For the command_line sensor, if you want to use a template, it has to be for the command arguments part only (everything after the first space.)

No, I tried that but it was’t working.

Thank you anyway!!

What did you see in the log (where it says, “Running command: …”)?

Did you see my earlier post where I first recommended this? I showed what I saw in the log, and the quotes were there. I don’t understand why it wouldn’t work for you, too.