Value_template not reacting to state of 'CH_STATUS 106 LOCAL'

Hi everyone,

i have a tcp sensor that returns what my tivo box is doing. The state that it returns contains spaces and special characters. I am basically trying to detect whether my Virgin box is on. I control it using telnet.

Using the template editor, if i run {{ states(“sensor.tcp_sensor”) }} it returns ‘CH_STATUS 106 LOCAL’ - perfect. Lets use this in an if statemen…

   {% if states("sensor.tcp_sensor") == 'CH_STATUS 0106 LOCAL"' %}
      on
    {% else %}
      off
    {% endif %}

but it doesnt work. Iv tried loads of different variations. Is it because of the space, special characters?

Iv tried putting a wildcard in the template (*) but nada.
image

Any ideas?

Why do you say that the returned string contains special characters? Do you know where they are in the string and what their hex value(s) are? You can create a string with special characters using \xnn where nn is the hex value of the character. So, e.g., "abc\x32def" would be equivalent to "abc2def".

Also, if the sensor is just going to provide ‘on’ and ‘off’ values, then you’re probably better off using a TCP Binary Sensor. E.g., if "CH_STATUS \x01\x06 LOCAL" represented ‘on’, then you could do this:

binary_sensor:
  platform: tcp
  name: whatever
  host: IP_ADDRESS
  port: PORT
  payload: "whatever"
  value_on: "CH_STATUS \x01\x06 LOCAL"
1 Like

Thanks for the reply. Unable to tinker atm tho.

What would be ideal is if it just detected “Ch_status*” then have a wild card on the end? Do you know how that would look?

After I wrote the post I managed to get the above working but had to create another sensor which called the tcp_sensor above. Once I did this it reacted to the state.

It’s a bit long winded tho. I’ll try the above.

Once I have this working I can attach it to all my other automations which control the virgin box. As there is only a single power button for virgin, often my automations would turn the box off, thinking it had turned it on. If that makes sense…

Replying on my phone so can’t check the details but you should be able to use a template (value_template I think) to check the value startswith that string.

Think iv nailed it, just need to work out the syntax for .startswith to replace the CH_STATUS 0141 RECORDING

automation tv lights on:
  alias: TV on - TV lights /Virgin on
  trigger:
    platform: state
    entity_id: media_player.sony_tv
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: light.tv_lights
    - service: homeassistant.turn_on
      data_template:
       entity_id: >-
         {% if is_state('sensor.virgin_status', 'CH_STATUS 0141 RECORDING') %}    

         # nothing here - will that work?  As the box is on do nothing...

         {% else %}
           switch.virgin_power
         {% endif %}

There is a status when the box is off, meaning i dont need to use startswith anymore…

automation tv lights on:
  alias: TV on - TV lights /Virgin on
  trigger:
    platform: state
    entity_id: media_player.sony_tv
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: light.tv_lights
    - service: homeassistant.turn_on
      data_template:
       entity_id: >-
         {% if is_state('sensor.virgin_status', 'COMMAND_TIMEOUT') %} 
           switch.virgin_power
         {% else %}
         # do nothing
         {% endif %}

I suspect the # do nothing part is causing errors. Yes, effectively it won’t do anything, but that’s not the best way. You should use a condition step instead. Like this:

automation tv lights on:
  alias: TV on - TV lights /Virgin on
  trigger:
    platform: state
    entity_id: media_player.sony_tv
    from: 'off'
    to: 'on'
  action:
    - service: light.turn_on
      entity_id: light.tv_lights
    - condition: state
      entity_id: sensor.virgin_status
      state: 'COMMAND_TIMEOUT'
    - service: switch.turn_on
      entity_id: switch.virgin_power

When the trigger comes, it will always do the first action. But it will only turn on switch.virgin_power if sensor.virgin_status is 'COMMAND_TIMEOUT'.

Sweet that looks better. Thanks

What if i want to do nothing when the state not = COMMAND_TIMEOUT?

I need this to work both ways if possible,

I have a few switches i no longer use and do nothing, maybe i just trigger one of those

I assume you mean in a different automation?

You could either specify a different state which would be true when you want to do something, or you could use a template condition, like:

  action:
    ...
    - condition: template
      value_template: "{{ not is_state('sensor.virgin_status', 'COMMAND_TIMEOUT') }}"
    ...