Command_State or Value_Template issue for command line switch

Hey all,

It’s gone 3am and I’m stuck so I’m hoping one of you fine people can help me out here.

I have a command line switch called aircon as follows:

      aircon:
    command_on: 'irsend send_once /etc/lirc/lircd.conf COOL_17'
    command_off: 'irsend send_once /etc/lirc/lircd.conf POWER_OFF'
    friendly_name: 'Air Con'
    command_state: sensor.tp_link_watts
    value_template: "{{ states('sensor.tp_link_watts') | int > 150 }}"

This works and displays the state of the ac unit by detecting if the smart plug it’s plugged in to is reading over 150 watts. However, this spams my log with errors:

Log Details (ERROR)
Mon Jul 09 2018 03:34:56 GMT+0100 (British Summer Time)

Command failed: sensor.tp_link_watts

I think I need to change the “command_state” portion of my configuration but I’m not sure what to change it to.

Ideally I’d like to keep the button looking like this (same as the rest of my buttons) image instead of this image.

Any help will once again be gratefully received, and thanks for your time so far.

Some more thinking and I wonder if I should be querying /api/states/sensor.tp_link_watts instead?

First time doing all of this and I can’t seem to get curl working right (locally - going out to my duckdns, SSL’d-via-letsencrypt address works fine) :frowning:

The new “working” config that doesn’t spam errors in home-assistant.log:

      aircon:
    command_on: 'irsend send_once /etc/lirc/lircd.conf COOL_17'
    command_off: 'irsend send_once /etc/lirc/lircd.conf POWER_OFF'
    friendly_name: 'Air Con'
    command_state: 'curl -X GET -H "x-ha-access: PASSWORD" -H "Content-Type: application/json" https://DOMAIN.duckdns.org/api/states/sensor.tp_link_watts'
    value_template: "{{ value_json.state | int > 150 }}"

Now I just need to work out how to get this to work locally instead of going out to the internet and back in again…

It would seem these guys had the same issue but never resolved the issue of keeping it local:

I think you just need to remove the command_state line from your original configuration. According to the docs, it is optional, and it doesn’t sound like you need to use it in your case.

If I remove command_state I get the lightning bolt buttons which don’t seem to reflect the state of the sensor this is monitoring.

Hmm. Ok. Well, I’ve never used this component, and I haven’t read the code. But my guess would be, based on what you said, that you do need it. But you don’t care what it does or returns, since you get the state from another sensor. How about using a valid command (like true, which is a Unix command that just sets the return status to 0)? That should cause it to evaluate value_template.

So:

    command_state: 'true'
    value_template: "{{ states('sensor.tp_link_watts') | int > 150 }}"
4 Likes

I can confirm that’s done the trick and kept everything local. Thank you very much for your help!

1 Like