How to set an input_boolean - directly within a command_line sensor's value_template code snippet (if a specific substring exists)?

The title pretty much explains what I am trying to do - without having to create more automations which is really overkill, one to assign the input_boolean as on and one to assign it as off etc., etc… The most elegant way would be to assign the value to the input_boolean right within the value template code snippet for that command line sensor where I could have a line of logic or two to search the command line text received for the specific string - but I’m having a little trouble figuring out the exact syntax… I have other ridiculously long command_line value templates that read the values of other sensors to calculate the value needed, so I am sure it can be done(?)… To keep the question specific, here is the current command line sensor value, between the quotes (it’s text most of which I will use in the front end):

“Oct 5 11:36:50 kruse-pi Fan Switched OFF; CPU Temp just fell below 55 to 54.53, (range is 55-65, checked every 5 seconds)”

The embedded string from the above that should be used to determine the input_boolean value will always have either the exact characters “Fan Switched OFF” or “Fan Switched ON”.

The input boolean entity is named “input_boolean.rpi_fan_running”.

Here is the current configuration.yaml code for the command_line sensor which does work, ideally I need to put the code into the value template to assign the input_boolean.

FYI this “toggle helper” input_boolean will not be editable on the front end, if shown it would be read-only (ideally only set from the below code). I have put some comments in there to show the direction I am trying to go - (so it does work as it now stands) -

  - platform: command_line
    name: FAN_STATUS
    command: "grep 'Fan Switched' /share/syslog | tail -1 | cut -c-255"
    scan_interval: 30
    command_timeout: 5
#    value_template: >
#  something like this->?    {% input_boolean.rpi_fan_running.turn_{{ code in here for "on" or "off"? }})  %}
#  or -
#  something like this->?   {% set_state('input_boolean.rpi_fan_running') = (value.string.find("Fan Switched ON") > -1)       
#   (Below is just to indicate we want the full unchanged string passed along in the command line 
#    sensor for now, later I might strip out the "Oct  5 11:36:50 kruse-pi " part of the
#    string but I already know how to do that and will decide on that later)
#      {{ value }}
    unique_id: FAN_STATUS

(yes, the command_line is one of the few places where the unique ID can be specifically set which is very nice…)

Since I do refer to other sensors within some command_line sensor value_template code snippets I know they can be “read” - just hoping in the same place they can also be edited and are not “read-only” within this area (yikes)?

Ideas anyone? I am sure this shouldn’t be too hard… :slight_smile:

You can use

{{ "Fan Switched ON" in states('sensor.text') }}

As a binary sensor value template

1 Like

Thank you, I’m on it!

Sorry to be thick but I cannot figure out the syntax for putting this into the value template - UGH

{% “input_boolean.rpi_fan_running = (“Fan Switched ON” in states(‘sensor.fan_status’)) %}

ARGHG&Y(@&(*&#$ ??>???

Why do you want it as an input boolean?
Why not a binary sensor?

No doesn’t have to be I agree - I got rid of the input boolean - just banging my head on the syntax - but figured it ot here we go:

(my spacing is a little whackadoodle below but they are from difference sections of my configuration.yaml).

Thanks for your help!

  - platform: command_line
    name: RPI_FAN_STATUS
    command: "grep 'Fan Switched' /share/syslog | tail -1 | cut -c-255"
    scan_interval: 30
    command_timeout: 5
#    value_template: >
#      {% states(input_boolean.rpi_fan_running) = {{ "Fan Switched ON" in states('sensor.text') }} %}
    unique_id: RPI_FAN_STATUS

      # Binary sensor specifiying if the RPI fan is running
        friendly_name: RPI Fan Running
        value_template: >-
          {{ "Fan Switched ON" in states('sensor.rpi_fan_status') }}
        unique_id: rpi_fan_running