Send message to IP:PORT

Hi,

I have been using tasker (with autovoice) to control my iKettle; a kettle that can be controlled from an app. Thanks to this blog, I knew what to send, and to use port 2000. I set up my kettle to a static ip on my home network, and can now via tasker send commands (and, I guess, the kettle could send back its status) using the “send/expect” action.

Is there a way to do this in homeassistant? I would need to send a message to the specific ip:port, that’s it. I looked at MQTT, but wasn’t sure if this would be able to? It seems (intuitively, to me) like a simple thing that should be fairly straight forward. But after looking through all the “components” I thought could be relevant, I still can’t find a/the way.

If anyone could point me in the right direction, I’d be a happy (and able to boil some water without having to pick up or yell at my phone :slight_smile: )

shell_command component in conjunction with netcat? So your command would look like this:

echo "HELLOAPP\n" | nc IP PORT

~Cheers

Thanks, I’ll take a swing at it.

Update:
For anyone googling this…

I ended up writing in the configuration.yaml

    shell_command: !include shell_commands.yaml
    script: !include scripts.yaml

and making those files. The shell.commands.yaml contents are:

    #ikettle commands

    ikettle100: bash /home/pi/shell_commands/ikettle100.sh
    ikettle95: bash /home/pi/shell_commands/ikettle95.sh
    ikettle80: bash /home/pi/shell_commands/ikettle80.sh
    ikettle65: bash /home/pi/shell_commands/ikettle65.sh
    ikettleoff: bash /home/pi/shell_commands/ikettleoff.sh

I made each of the files referenced in the above, e.g. the ikettle100.sh (turning on the kettle, and setting the temperature to 100°C, see the link in original first post for what to substitute for other temperatures/commands):

    #!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x80\n" | nc  192.168.0.20 2000

The contents of the “scripts.yaml” are:

      ikettle100:
        alias: Set iKettle to 100 degrees
        sequence:
          - service: shell_command.ikettle100

      ikettle95:
        alias: Set iKettle to 95 degrees
        sequence:
          - service: shell_command.ikettle95

      ikettle80:
        alias: Set iKettle to 80 degrees
        sequence:
          - service: shell_command.ikettle80

      ikettle65:
        alias: Set iKettle to 65 degrees
        sequence:
          - service: shell_command.ikettle65

      ikettleoff:
        alias: Turn off iKettle
        sequence:
          - service: shell_command.ikettleoff

And it works fine. So thanks a lot @PhyberApex for the pointer! I even (almost) feel like I understand what I did :slight_smile: (I did break everything a few times on the way, so that’s a good way to learn, right…)

Hey, thanks for the update. First of all I don’t think you need the addition “bash” before every script that’s usually what #!/bin/bash is for ^^.

Also if you could post all of the scripts in here I would be glad to help you out in getting it a bit cleaner. E.g. only on script with a few ifs and data submitted by HA. Also what are your scripts for?

~Cheers

Cool, thanks. And right you are, I removed the bash.

Short term, my scripts right now are mainly for learning purposes, but this set of them are to control an electric kettle (Smarter iKettle V1). I had set it up using tasker and autovoice on my phone, but I’m trying to get everything integrated in home assistant (and just backed the MATRIX voice, so hoping I can make everything play nice before that starts shipping). I have the scripts shown in the scripts.yaml posted above (other scripts are unrelated, and just for etsting stuff). The other *.sh files referred to in the shell_commands.yaml (also see above) are:

ikettle100.sh (turn on kettle, set to 100°C):

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x80\n" | nc  192.168.0.20 2000

ikettle95.sh (turn on kettle, set to 95°C).

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x02\n" | nc  192.168.0.20 2000

ikettle80.sh (turn on kettle, set to 80°C).

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x4000\n" | nc  192.168.0.20 2000

ikettle65.sh (turn on kettle, set to 65°C).

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x200\n" | nc  192.168.0.20 2000

ikettleoff.sh (turn off kettle)

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x0\n" | nc  192.168.0.20 2000

These are to set the kettle to different temperatures (as described here), or turn it off. But at some point I also plan to include scripts to keep the kettle warm for either 5, or 10 minutes, which would look like this (this is for keeping warm for 5 minutes):

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x8005\n" | nc  192.168.0.20 2000

I think it has to be sent after turning it on at a certain temperature; it didn’t seem to work if I just added this to the end of one of the others (like e.g. this):

#!/bin/bash

    echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output 0x80\nset sys output 0x8005\n" | nc  192.168.0.20 2000

Which I think makes sense, when you look at the app layout (the “keep warm” is a separate button you can press after you press a certain temperature).

And perhaps I would want to include an alarm (there is a status message it will send if it detects that the kettle is dry), and have my lights blink or something, as a warning. That would be something about listening for the kettle putting out status messages, and I haven’t really gotten around to looking into this.

Is it a bad idea to have these as separate *.sh files? You think it would be more efficient having a single “kettle control” file/script? If so, I’d love to hear your suggestions.

Hey this sounds like a new component to me. Sadly I am not able to do that myself. So yes I would suggest you switch all those different scripts into one script. First step would be

#!/bin/bash
echo -e "HELLOKETTLE\nset sys output 0x4\nset sys output $1\n" | nc  192.168.0.20 2000

Call it something like “ikettleon_off.sh”

and call it with different shell commands like this:

ikettle100: bash /home/pi/shell_commands/ikettleon_off.sh "0x80"
ikettle95: bash /home/pi/shell_commands/ikettleon_off.sh "0x02"
etc.

Then you could replace your script with the following:

ikettle:
  alias: Set iKettle to $temp degrees
  sequence:
    - service_template:>
      {% if temp == 100 %}
        shell_command.ikettle100
      {% elif temp == 95 %}
        shell_command.ikettle95
      ...
      {% else %}
        //NOTIFY YOURSELF because this should not happen
      {% endif %}

and call this script like so in automations:

service: script.turn_on
data:
  entity_id: script.ikettle
  temp: YOURTEMP

This way you do the mapping from number to hexcode in HA which e.g. allows you to use a slider with your kettle :slight_smile:

~Cheers

That does look cleaner, but there’s something about this that homeassistant doesn’t like. I edited the entry in the scripts.yaml to:

    ikettle:
      alias: Set iKettle to $temp degrees
      sequence:
        - service_template:>
          {% if temp == 100 %}
            shell_command.ikettle100
          {% elif temp == 95 %}
            shell_command.ikettle95
          {% elif temp == 80 %}
            shell_command.ikettle80
          {% elif temp == 65 %}
            shell_command.ikettle65
          {% else %}
            //NOTIFY YOURSELF because this should not happen
          {% endif %}

and, I get the following error:

17-04-03 01:38:25 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: expected a dictionary @ data['script']['ikettle']['sequence'][0]. Got 'service_template:> {% if temp == 100 %} shell_command.ikettle100 {% elif temp == 95 %} shell_command.ikettle95 {% elif temp == 80 %} shell_command.ikettle80 {% elif temp == 65 %} shell_command.ikettle65 {% else %} //NOTIFY YOURSELF because this should not happen {% endif %}'. (See /home/homeassistant/.homeassistant/configuration.yaml, line 149). Please check the docs at https://home-assistant.io/components/script/
17-04-03 01:38:25 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.

I have another script above in the scripts.yaml (that sends a message to the IFTTT maker channel) that still works.

the line 149 in configuration.yaml that’s referred in the error is (as you might have guessed):
script: !incllude scripts.yaml

First of all I guess that second “l” in the include is a typo as it should’t work otherwise. The error might be because there is a space missing…so it should be:

- service_template: >

instead of

- service_template:>

Sorry I did not test this, just wrote it down to give you a general idea :slight_smile:

~Cheers

Yes of course, and don’t worry, there’s only one “l” in that line in the actual configuration.yaml.

It kept complaining about the line with {% if temp == 100 %}, so I tried with a few more spaces to align with the “shell_command” lines, and it seemed to work. At least I now get it listed as a script (that of course doesn’t really do anything yet, since the script alone doesn’t have an input for the variable when running from the front end, but I’ll look at that part later) when writing the following in the “scripts.yaml”:

  ikettle:
    alias: Set iKettle to $temp degrees
    sequence:
      - service_template: >
          {% if temp == 100 %}
          shell_command.ikettle100
          {% elif temp == 95 %}
          shell_command.ikettle95
          {% elif temp == 80 %}
          shell_command.ikettle80
          {% elif temp == 65 %}
          shell_command.ikettle65
          {% else %}
          //NOTIFY YOURSELF because this should not happen
          {% endif %}

So thanks for the pointers. Still have a bit to play with (sliders/buttons for the different temperatures, including in automations, etc.), but you definitely showed me a sleeker way to set this up! (Though I think I’ll also keep the “old” script to turn on, 100°C until I have figured out how to implement the new script properly :slight_smile: )

1 Like

Hi Aephir - did you ever come right with the slider ? would love so see how this works. I am doing something similar with a QuinLED module (ESP8266 LED Lighting: Revisit and history of QuinLED - Intermittent Technology)

at this point I have all the .sh scripts working to control brightness, but now need to turn this into a “switch” with a slider.

Thanks everyone

Sorry, can’t help here. This got scrapped as I got closer to the deadline of my PhD thesis (since I had the 100°C working, and that was 99% of what I used). And now I’m re-starting in a new home, so it might be a while before I get to look at it again.

THanks,

@ PhyberApex wonder if you can help point me in the right direction ?

What kind of kettle exactly are you using?

Well, this was a while ago, but my kettle is the iKettle by smarter.am.

Mine was the first generation, so something that looks more like this (with the larger base):

You can’t get these anymore, and I don’t know how the newer versions work (if the same approach is applicable).