Sometimes it’s necessary to control “things” on a remote linux device.
(possible a RPI, and certainly NOT your device that is running HA)
In this posting, you will learn how to perform these tasks on the remote device:
* Control the volume
* “Move” the mouse to a predefined position, and perform a “click”. (also choose which mouse button)
* Make a screenshot (even in baremetal kiosk mode)
* Transfer the screenshot
* Turn display on/off
* Turn display on/off for Raspberry Pi
* Shutdown / reboot / turn on (via Wake-on-lan)
Most of these commands also work on MacOS !
For a Windows alternative, I advise you to have a look at IOT Link: https://iotlink.gitlab.io/
*** This tutorial assumes that you already followed this tutorial ***:
shell commands to RPI or other Linux machines, without the need to enter passwords !
1. Control the volume
This explanation only works when you use ALSA soundcard drivers on your remote machine.
I’m sure a similar method exists for other soundcard drivers, so google a bit around for them π
To set the volume from a command-line, you should use:
amixer -D pulse sset Master xxx% (where xxx is the numerical representation of the volume-percentage)
So, to use this on a remote machine, you should use the following shell-command in your configuration.yaml
(as usual, replace username & remote_ip)
set_volume: 'ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no username@remote_ip amixer -D pulse sset Master {{ states.input_number.volume.state }}%'
Within shell commands, you can use entities …
input_number.volume represents an entity with a slider, that can move between 0 and 100.
When you create an automation that triggers when this value is changed,
you can automatically adjust the volume on your remote device …
alias: "AUTOMATION - SET VOLUME"
description: ""
trigger:
- platform: state
entity_id:
- input_number.volume
condition: []
action:
- service: shell_command.set_volume
data: {}
mode: single
2. βMoveβ the mouse to a predefined position, and perform a βclickβ
First, we need to install 2 small tools:
– xdotool : this tool is used to set the location of the mouse, and perform a mouse-click.
– unclutter: We can (un)hide the mousepointer with this little tool.
type sudo apt-get install xdotool on the remote machine, to install the first tool.
type sudo apt-get install unclutter on the remote machine, to install the second tool.
To click with the left mouse-button in the middle of a 1920×1080 screen of a remote machine,
add this shell command into your configuration.yaml :
click_middle_of_the_screen: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip "export DISPLAY=:0 ; xdotool mousemove 960 540 click 1"
– click_middle_of_the_screen is the name of the shell command.
– username is the users’ name to login on the remote machine via SSH.
– remote_IP needs to be replaced by the IP (or DNS name) of the remote machine.
– 960 is the X-position to move the mouse to (1920 / 2).
– 540 is the Y-position to move the mouse to (1080 / 2).
– 1 is the left mouse-button to click.
– Without the export DISPLAY=:0 , xdotool will not work ! (this will tell xdotool which X-interface to use)
– xdotool can combine multiple command on one line, like we do here with the mousemove and click.
– xdotool can do a lot more then mousemove and click, type xdotool –help to get a list of commands.
– unclutter can (auto)(un)hide the mousepointer ! type unclutter –help to know how to use this.
3. Make a screenshot
Taking a screenshot of the GUI display of a remote machine, allows you to see what is going on, on this remote machine,
and gives you the possibility to have clear screenshots for documentation purpose.
type sudo apt-get install scrot on the remote machine, to install the scrot tool.
Next, you can make screenshots of the GUI of the remote machine,
by using the following shell command in your configuration.yaml :
screenshot: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip scrot /home/username/screenshot.png
– screenshot is the name of the shell command.
– username is the users’ name to login on the remote machine via SSH.
– remote_IP needs to be replaced by the IP (or DNS name) of the remote machine.
– /home/username/ is the location to put the screenshot.
– screenshot.png is the name of the file that will containt the actual screenshot picture.
– There are possibilities for taking screenshots of dualhead monitors and delay etc … check scrot manpages !
4. Transfer the screenshot
Of course, we want to have the screenshot available on our main machine (or any other main computer)
We can use SCP to transfer the file to another computer.(SCP is part of SSH, and also works on Windows & MacOS machines)
Run the following command, at the machine you want to have your screenshot available:
scp username@remote_ip:/home/username/screenshot.png c:\screenshot.png
The above command, copies the file from the remote machine, at the previous defined location,
to your C-folder on your windows machine. Simply replace c:\screenshot.png with /location/screenshot.png
(Location is of course a valid (linux) folder location)
5. Turn display on/off
For power-saving purpose, it can be handy to turn remote screens on or off.
I use this, coupled on FP1 and motion sensors. When at least one person is in a room, the display in this room gets turned on. (and turned off again, when nobody is in the room)
Command shell to turn screen off: (add this to your configuration.yaml )
screen_off: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip "xset -display :0.0 dpms force off"
Command shell to turn screen on: (add this to your configuation.yaml )
screen_on: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip "xset -display :0.0 dpms force on"
6. Turn display on/off for Raspberry Pi
To turn a display on/off for Raspberry Pi, the command is slightly different.
First of all, you need to edit the config.txt on the RPI. This file is located at /boot/config.txt while running the RPI,
or simply in the root of the boot partition, when you edit this file while the SD is connected to your PC.
– Make sure you first do a rpi-update / update / full-upgrade on your RPI !
– Replace dtoverlay=vc4-kms-v3d with dtoverlay=vc4-fkms-v3d in the config.txt file.
On a RPI4, we have 2 HDMI connections, here are 4 commands:
vcgencmd display_power 0 1 #HDMI 1 off
vcgencmd display_power 1 1 #HDMI 1 on
vcgencmd display_power 0 2 #HDMI 2 off
vcgencmd display_power 1 2 #HDMI 2 on
If you want a shell command that turns off HDMI2 on a remote RPI4, you need this in your configuration.yaml
rpi_screen_off: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip "vcgencmd display_power 1 2"
7. Shutdown / reboot / turn on (via Wake-on-lan)
Shutting down or rebooting a remote machine, is pretty simple π
Shell command for remote reboot, insert into configuration.yaml :
remote_reboot: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip sudo reboot
Shell command for remote shutdown, insert into configuration.yaml :
remote_shutdown: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' username@remote_ip sudo poweroff
Turning on a remote device is a bit more complicated, but do-able π
First you need to make sure that your remote device supports “wake-on-lan”.
Mostly, you still need to turn on this feature in the BIOS of the device. There are so many different devices, so also to many different ways, to turn this on. Please search on google or look in the manual of your device. Turning this option on, is a must ! it will not work without this setting activated.
A RPI does NOT support wake-on-lan. I use a zigbee power-plug, to turn of the power after a few seconds after issueing
a poweroff command to the RPI. When turning the power back on with this power-plug, the RPI automatically boots up again.
Make sure you have the MAC-address of the remote device.
If you have access to the remote machine, simply do ifconfig , you will get a list of all available network connections
on the machine. in the field ether , you will find the MAC-address for that specific network connector.
If you need to find out the MAC-address of a remote machine, install arp-scan first: sudo apt-get install arp-scan
Next, run the following command: sudo arp-scan remote_ip , You will get the MAC address in the “answer”,
next to the IP-adress.
Next, put wake_on_lan: in the general section of your configuration.yaml
After this, you can add a wake-on-lan section in the configuration.yaml
Replace the AA:BB:CC:DD:EE:FF with your given mac-address (and also change it’s DeviceName)
And finally, you need to reboot your HA instance.
switch:
- platform: wake_on_lan
mac: AA:BB:CC:DD:EE:FF
name: "DeviceName"
You will find an entity switch.devicename in HA, when you turn this on, your remote device should also turn on …