Command Line Sensor - Run command if State = On

Hi,

So I have the below command which gets info from my samsung dryer, and it works great, except… if the dryer is off, the HA log just gets spammed with [homeassistant.components.sensor.command_line] Command failed: curl

I have a binary sensor named binary_sensor.dryer_state which tells me if it’s on. How can I change the below to only run command if binary_sensor.dryer_state = ‘on’

  • platform: command_line
    command: >-
    curl -X GET -H “Content-Type: application/json” -k -H “Authorization: Bearer xxxxxxx” --cert ‘/home/homeassistant/ssl/cert.pem’ --insecure https://192.168.0.19:8888/devices/0/operation/
    name: Dryer Step
    value_template: ‘{{ value_json[“Operation”][“progress”] }}’

Thanks!
Cody

I’m not sure you can get a sensor to read only at predefined times.
What you may need to do is write a small python script that get the data, handles the error when the dryer is not on, and posts the value when there is no error…
Alternatively you can set your logger to ignore this component so as not to flood your log file…

1 Like

Your scenario is a bit unclear. Are you saying you have two sensors: a binary sensor named dryer_state, and the one you show in your post named dryer_step?

But assuming you’re just trying to prevent errors from the shown command_line sensor, you could try something like this:

- platform: command_line
  command: >
    curl -s -X blah_blah_blah || echo '{"Operation":{"progress":"unknown"}}'
  name: Dryer Step
  value_template: '{{ value_json["Operation"]["progress"] }}'

I haven’t actually tried this, but the idea is the -s curl option tells it not to output error messages, and the “|| echo …” part will output a valid JSON string if curl fails.

Not exactly what I’m looking for. My command line sensor works fine, and shows “Unknown” when the dryer is off, but it just errors when it’s off, and floods the log file. My plan was to create a binary sensor, which will tell me if it’s on via telnet, and if it is, then get the other info, but doesn’t seem like there is a straightforward way to accomplish this.

It’s probably showing the state as Unknown because the command_line is failing. To prevent the command_line from failing (and hence filling the log with error messages), you need to do something to prevent the command_line from returning a non-zero status code. The easiest way is to do “command || true”. But then there would be no output and the value_template part would fail. By doing “command || echo …” you can control what value_template “sees” when the curl command fails. Just put whatever you want the state to show up as (when the dryer is off) where I have “unknown” in the echo command.

Does that make more sense?

Thanks @pnbruckner for this, I’ll give it a try. And my above comment was directed @lolouk44 not you =)

1 Like

Hey @pnbruckner, so I wasn’t able to get your suggestion above to work, however, using a template sensor does what I need it to. I am having trouble with one of them though. The following works:

   value_template: >-
     {% if is_state('binary_sensor.washer_state', 'on') %}
       {{ states('sensor.dryer_step_in') }}
     {% else %}
      -
    {% endif %}

But this does not:

  value_template: >-
    {% if is_state('sensor.dryer_progress_in' == '100') %}
        Complete
    {% elif is_state('sensor.dryer_progress_in') != "unknown" %}
      {{ states('sensor.dryer_time_remaining_in') }}
    {% else %}
      "-"
    {% endif %}

Any idea on why the above doesn’t work? I am guessing it is the else if and/or I am checking the values incorrectly.

I also tried:

{% if is_state('binary_sensor.dryer_progress_in', '100') %}

Thanks,
Cody

Got it working =)

dryer_time_remaining_out:
  friendly_name: "Dryer Cycle Time Remaining"
  value_template: >-
    {%- if states('sensor.dryer_time_remaining_in') == "unknown" -%}
      -
    {%- elif states('sensor.dryer_progress_in')|int == 100 -%}
      Complete
    {%- else -%}
      {{ states('sensor.dryer_time_remaining_in') }}
    {%- endif -%}
1 Like

@codylv7 so how did You set up those sensors- those that You need to get status using curl? I have similar problem- i’m just getting temp sensor using ssh command line… and of course getting lots of errors when servers is down…

@kaczkalolo it’s been a long time since I set that up, and now I’m using Node-RED with an HTTP GET to https://<washer/dryer ip>:8888/devices/0/operation/ which returns JSON showing cycle state/progress.

Hi Cody,

do you have any advice on how to achieve this? I’m currently doing it via SmartThings integration, but I don’t like using the cloud for these things, I prefer local control if possible. Thanks for any help.