Value_template woes for command_line switch

I have a switch for turning on/off my desktop computer. On/off works fine. I have a value_template for setting it’s status based on a device_tracker through the ping service. The device_tracker works fine (ie. it is set to home/not_home if the computer is on/away). However, the status of the switch is not changed accordingly. If I enter the same snippets in Template it works fine. Can’t figure out what I’m doing wrong here.

- platform: command_line
  switches:
    computer:
      command_on: "sudo etherwake -i redacted"
      command_off: "ssh redacted@host -t 'sudo /sbin/poweroff'"
      value_template: '{{ is_state("device_tracker.computer", "home") }}'    
      friendly_name: Computer

I have also tried with, among other things, value_template:'{{ states("device_tracker.meru") == "home" }}' which also works fine in Template, but not for this value_template. I’ve also tried with if ... then templates, which also has worked out fine in Template, but – you got it – not here. Also tried all different variations of single-/double quotes for value_template that I’ve been able to think of, as well as using the >- format instead of having it on the same line. So far, nothing has helped. :-S

Thankful for any ideas.

looks like that value template requires a command_state. I looked into it and I don’t see it calling the normal template functionality. It appears to only evaluate against whatever is placed into the command_state.

You may be better off making a template switch. with 2 shell commands. One for on, one for off:

shell_command:
  myswitch_command_on: "sudo etherwake -i redacted"
  myswitch_command_off: "ssh redacted@host -t 'sudo /sbin/poweroff'"
switch:
  - platform: template
    switches:
      value_template: '{{ is_state("device_tracker.computer", "home") }}'   
    turn_on:
      service: shell_command.myswitch_command_on
    turn_off:
      service: shell_command.myswitch_command_off
1 Like

I’m having more or less similar issue.
In my case, I have a MySensors node connected to a Bosch gas heater and reading it’s data using OpenTherm. Since in MySensors API there’s no specialised sensor for water gas heater yet, I present the node as a binary door sensor. And are multiple children in the node, so i have entities named binary_sensor.boiler_monitor_2_0 and binary_sensor.boiler_monitor_2_2 (0 is for flame state, 2 is for heating state, 1 is reserved for DHW temp).
What I am trying to achieve is just change rename the ‘door sensor’ outputs to be more boiler-specific, just as mentioned in the docs: https://www.home-assistant.io/components/sensor.template/#renaming-sensor-output

I use a binary_sensor template, since I wanted to use an icon. The config is:

  - platform: template
    sensors:
      flame_state:
        friendly_name: 'Boiler flame'
        icon_template: >-
          {% if is_state_attr('binary_sensor.boiler_monitor_2_0', 'V_TRIPPED', 'off') %}
          mdi:close-circle
          {% else %}
          mdi:mdi-fire
          {% endif %}
        value_template: >-
          {% if is_state_attr('binary_sensor.boiler_monitor_2_0', 'V_TRIPPED', 'off') %}
          off
          {% else %}
          on
          {% endif %}

The issue is the same, if I enter this snippet in the Templates section of dev-tools, I can see that value_template is populated accordingly, but in the frontend the flame_state entity is always off.

This is not similar to @krissen’s issue at all. It may seem like it, but it’s not.

  1. does the binary_sensor.boiler_monitor_2_0 have a V_TRIPPED attribute? The likely hood of this object having an all caps attribute is slim to none. Attributes are dictionaries, and home assistant tries to make all attributes lower case.

  2. are you sure the result of this attribute is on/off?

Regular sensor templates support icon_templates as well. Not just binary sensors.

This may be causing problems because you are returning a string. Both strings when empty are considered false. Strings when populated are considered true. You are providing the template with 2 trues. I’d expect it to always be on.

This is what your value template should look like (assuming that attribute is correct):

        value_template: "{{ is_state_attr('binary_sensor.boiler_monitor_2_0', 'V_TRIPPED', 'off') }}"

Looks like it’s just the way MySensors work, attributes seems to be all caps and value is on/off:

And yet now it’s always off.

I don’t get it. {{...}} is Jinja’s syntax for value interpolation into the template. This expression will just yield “True”. I’ve tried putting it into the configuration file, but this doesn’t changed the result.

My mistake, I didn’t realize you had off returning off, your template should be:

        value_template: "{{ not is_state_attr('binary_sensor.boiler_monitor_2_0', 'V_TRIPPED', 'off') }}"

Can you verify that V_TRIPPED is turning on?

Thanks @petro!

Using two shell_command:s and a template switch indeed did the trick!

1 Like