What does to: do?

I am having trouble getting an automation to trigger. I have a TCP sensor that I am trying to use in an automation. If I use the “from: ‘off’”, “to: ‘on’” elements it never triggers. If I leave them out it triggers after any change. Am I using the elements wrong?

sensor:

  • platform: tcp
    name: Everyone out
    host: 192.168.1.11
    port: 8080
    payload: “get flag state 14\n”
    value_on: ‘on’
    value_off: ‘off’
    timeout: 5

automation:

  • alias: “test tts”
    trigger:
    • platform: state
      entity_id: sensor.everyone_out
      from: ‘off’
      to: ‘on’
      action:
      • service: tts.google_say
        data:
        message: “Party time!”

Are you using a tcp sensor or binary sensor? Are you sure that your payload provokes only the response “on” or “off”? Do you need to include a value template to extract the value on or off from the response? These are just a few ideas I’m having - it could be that the sensor value is never purely “off”, for example, so it will never trigger. Does it work better if you only specify to: and not from:?

Thanks for the reply. Actually the text response for the TCP Sensor is “Set” or “Clear”. But when I tried to use that on the value on: and value off: it didnt make any difference. I went back to the example that I found on the forum. Should I put that back? If I do, would the to: under the trigger be ‘Set’ or still ‘on’?

That’s your problem. Try this:

binary_sensor:
  - platform: tcp
    name: Everyone out
    host: 192.168.1.11
    port: 8080
    payload: "get flag state 14\n"
    value_on: "Set"
    timeout: 5

Note that it’s a binary sensor now, not just a sensor. “Value on” is a parameter for tcp binary sensors, not tcp sensors. Now, this state of this binary sensor will be “on” when the returned value is “Set” and off with any other value.

And your automation should look like this:

automation:
  - alias: "test TTS"
    trigger:
      - platform: state
        entity_id: binary_sensor.everyone_out
        from: 'off'
        to: 'on'
    action:
      - service: tts.google_say
        data:
          message: "Party time!"

Thanks. Unfortunately i tried it and still no luck. In fact as a binary sensor it doesnt register the correct state on the ui. Does it matter that the payload is text?

Is the returned value precisely Set? Are there quotes or anything in it?

No quotes. At least not in the docs. Is there a way I could test that?

Sorry for making you go back and forth but here’s a way to test anyway.

Change the config to:

sensor:
  - platform: tcp
    name: Everyone out
    host: 192.168.1.11
    port: 8080
    payload: "get flag state 14\n"
    timeout: 5

Now the sensor will just display whatever value is returned. So you should be able to see what the values are in both states, if you can simulate them.

Please don’t apologize!! Thanks for hanging in there with me! I did as you suggested and it comes up as Set and Clear.

Ok then so leave that sensor as it is, and set the automation to this:

automation:
  - alias: "test TTS"
    trigger:
      - platform: state
        entity_id: sensor.everyone_out
        from: 'Clear'
        to: 'Set'
    action:
      - service: tts.google_say
        data:
          message: "Party time!"

Unfortunately it still just doesn’t want to trigger. I am missing something I guess with what is being passed in the text even if it doesn’t show. I tried a template as the message so that it would say one thing if it was ‘Set’ and another if it was ‘else’. The ‘else’ always triggers no matter the value so it just isn’t matching the state value. I can set the value using the STATES in the development tools and it triggers correctly so I am confident in the template. Just at a loss on what exactly is being passed to the TCP sensor. Whatever it is, it is something that can’t be seen. maybe?

I always have fun with states and have a couple of gotos if the obvious one doesn’t work. Using your example of…

- platform: state
        entity_id: binary_sensor.everyone_out
        from: 'off'
        to: 'on'

they’d be…

- platform: template
  value_template: "{% if is_state('binary_sensor.everyone_out', 'on') %}true{% endif %}"

and

- platform: template
  value_template: '{{ states.binary_sensor.everyone_out == "on" }}'

One or other usually gets the job done.

Bobby, thanks. I will keep that one in the tool kit for sure!

I was able to look at the transactions using Telnet in Putty and did indeed find that it was putting a line feed after the text response of ‘Set’. Luckily I was able to talk to the person who wrote the interface and get a way to remove the line feed so it only passes the text. Works like a champ now… I hope!

Thanks marthocoo for putting me on the right path and all of the suggestions! Thanks Bobby for your awesome work around.

1 Like