Please help me enter my 1st script

I am trying to create a card that will let me exectute and test a seemingly simplle script. I am utterly failing to understand WHERE to enter the script. Half the internet says put it in configuration.yaml (mine is empty with a few basic headings) and the other half says its all gui based now.

I have installed IOT Link and also hooked it up to the MQTT add on. All good and devices discovered (my PC in this case). I want to use a script to put my PC to sleep.

I want to enter a script from this page:

The one called ā€œsuspend system hostā€

my_computer_suspend:
    alias: "My Computer - Suspend"
    sequence:
    - service: mqtt.publish
      data:
        topic: "iotlink/workgroup/my-computer/commands/suspend"
        payload: ""

But i just don’t understand WHERE to to enter this script. I’ve gon onto the script page, created a new one, then selected use YAML. I past the above in and get:

Message malformed: Unable to determine action @ data[ā€˜sequence’][0]

(I get I need to change the ocmputer name to the actual one).

Am I even trying to put the script in the right place? (to eventually link from a switch card on the main page).

I’d just like to see an example of the above script in the place it ā€œbelongsā€.

I’m totally stuck and just cant’ get my head around WHERE scripts live, and how to format them seeing as I’m bing given scripts that don’t work from the outset.

A little update, I removed the 1st line and now get:

Message malformed: Integration ā€˜ā€™ not found

alias: "My Computer - Suspend"
    sequence:
    - service: mqtt.publish
      data:
        topic: "iotlink/workgroup/RogerD-PC_SSD/commands/suspend"
        payload: ""

A web search tells me this is because the script has no trigger. Thing is, I know. I want to trigger it by pressing a card button on the main HA page.

your indentation is wrong (https://jsonformatter.org/yaml-validator to check), it should be

alias: "My Computer - Suspend"
sequence:
  - service: mqtt.publish
    data:
      topic: "iotlink/workgroup/my-computer/commands/suspend"
      payload: ""

you’re putting it in right place

for reference this is my working script that does the same:

alias: turbojeba_hibernate
sequence:
  - service: mqtt.publish
    data:
      topic: iotlink/workgroup/turbojeba/commands/hibernate
mode: single
icon: hass:sleep

edit: or like this in ui:

Thanks, and the validator is a great link.

I put the validated script in and now get

Message malformed: Unable to determine action @ data[ā€˜sequence’][0]

when trying to save

Good to know I’m at least in teh right place!

I’ve now tried the gui method you showed above and its ā€œalmostā€ working. Abandoning the yaml method for a moment.

Almost - I have a card that calls the service. when you click the card nothign happens, BUT, the script I created using the UI shows as being run seconds before. I’ve checked - it’s def running it, well, calling it. Just nothign is happening.

I’ll just have to work out why

p.s. found the ā€œrun scriptā€ option and it certainly runs it - just does nothing. Tried swapping the DNS name for the IP of my PC with no effect.

You’re going well for a beginner my friend :slight_smile:

Can you please check in the mqtt broker on the topic path that iotlink is creating.

PS I’ll try and take a look when I am home tonight and my laptop can connect to my mqtt broker.

1 Like

I took a look in my windows computer. From the start menu, iotlink, open logs file.

Then open a recent one, it is just text and should open in notepad. Near the top find the line like this

[WORKGROUP\DESKTOP-9JEI4AQ][2022-10-27 12:38:20 +13:00][SYSTEM][IOTLinkService.Service.MQTT.MQTTClient]: ALL YOUR MQTT TOPICS WILL START WITH iotlink/workgroup/desktop-9jei4aq/

At least you can check the topic your iotlink is posting to mqtt. In particular I note that mine is lower case, and yours probably is too.

For your setup to work you need 3 components

  1. Mosquitto (MQTT) broker
  2. HA connected to the mqtt broker
  3. iotlink service connected to the mqtt broker

Iotlink service runs on your pc and listens to the topic on mqtt broker. In ha the script we’re trying to make publishes a word (suspend/hibernate/etc) which triggers the iotlink service to do w/e it is configured to do when it ā€œhearsā€ the word.

You might have done all/some of the following, just check if you didn’t miss anything:

Let’s assume your MQTT broker runs at 192.168.1.123 listening to the default port 1883, change the values in following examples according to your setup if different.

In Ha go to Settings/Devices & Services. Click the Add Integration at the bottom, Search for MQTT, add MQTT, enter the ip 192.168.1.123 and port 1883. If your broker is secured with name and password enter those too.

That’s step 1. and 2. covered you have HA connected to MQTT broker.

On the PC you want to control install iotlink, go to Start/All Programs/IOT link/Open Configuration File, in the configuration.yaml it opens make sure to enter the correct MQTT info like so:

  ########################################################################
  # TCP Connection Settings
  ########################################################################
  tcp:
    enabled: true
    hostname: 192.168.1.123
    port: 1883

again, if your broker is secured enter the name/password above the TCP section. Save the file, Start/All Programs/IOT Link/IOT Link Service Stop and then IOT Link Service Start.

That covers point 3, and at this point the whole setup should be ready for the script as both HA and IOT link are connected to the same MQTT broker.


Let’s create the script then. In HA go to Settings/Automations & Scenes and go to Scripts tab at the top, click Add script at the bottom right.

  • Fill the top part (name/icon etc) to your preference
  • In Sequence there will be default Device action, click the 3 dots in it’s row and delete that.
  • Add Action / Call Service
  • In the dropdown find MQTT: Publish
  • Topic: iotlink/workgroup/RogerD-PC_SSD/commands/suspend
  • Save script

Run the script, your pc should go to sleep. If it doesn’t, assuming the ha and iotlink are connected to the mqtt broker correctly, you can use MQTT Explorer, connect that to 192.168.1.123 port 1883 and see whether both are connected, and if your topics are right like so:


the highlighted part is the topic of your pc, check that’s correct in the script.


Notes:
IOT Link is discontinued although it still works, it has issues, notably the one where you have to restart the service after pc wakes up for which you can use a Task Scheduler on wakeup task running this .bat

@echo off
C:\WINDOWS\system32\net.exe stop IOTLink
C:\WINDOWS\system32\net.exe start IOTLink

That said I still use it on 3 pcs, however I have been looking into GitHub - LAB02-Research/HASS.Agent: Windows-based client for Home Assistant. Provides notifications, quick actions, commands, sensors and more. as replacement just haven’t had time to test it and change the automations etc.

Hope you find any of the above useful and get the script working.

1 Like

You fk’n DANCER! :slight_smile:
My pc name needed changing to lower case

[IOTLinkService.Service.MQTT.MQTTClient]: ALL YOUR MQTT TOPICS WILL START WITH iotlink/workgroup/rogerd-pc_ssd/

My PC went to hibernate as soon as I changed this. I cheered. My Mrs said ā€œthat’s doesn’t look like success!ā€ - I explained, we cheered.

Thank you for such a detailed explanation - Your post is exactly what I was looking for for 2 days. It will help others undertsand how to enter a script too.

Point noted about the service - I’ll schedule the batch file on wakeup (although mine seems OK so far…)

1 Like