1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bash script to turn off/on LEDs with HDMI-CEC

Discussion in 'Hyperion General' started by RaPiiDe, 30 November 2019.

  1. RaPiiDe

    RaPiiDe New Member

    Messages:
    2
    Hardware:
    RPi3
    Hello guys!

    I've made a little script to turn off my LEDs when my AVR turns off and vice versa. It uses the CEC-Client, so it should work with everyone as long as your devices are HDMI-CEC compliant.

    The instructions to install it are included in the top part of the script. It's a pretty simple script so it shouldn't be a big deal to make it work.

    PHP:
    #!/bin/bash

    # Script for hyperion which allows you to turn off your LEDs when your AVR is off and vice versa.
    # Tested with Raspian on an RPi3
    # 1 - Plug your RPi with an HDMI cable on your AVR
    # 2 - Copy the script on your RPi
    # 3 - Run sudo chmod +x on the script
    # 4 - To make it run at startup, add   @reboot /your/path/yourscriptname.sh    to crontab -e
    # 5 - Enjoy

    # RaPiiDe

    remotePath="hyperion-remote"
    isOn=-1

    while :
    do
            
    # We get the CEC status
            
    status=$(echo pow 0 timeout 5 cec-client -d 1 -s)
            
    # If it's off and it wasn't already, let's turn off the LEDS
            
    if [[ "$status== *"power status: standby"* ]] && [[ "$isOn!= "0" ]]
            
    then
                    
    eval "$remotePath --priority 0 --color black"
                    
    isOn=0
            
    # If it's on and it wasn't already, let's turn on the LEDS
            
    elif [[ "$status== *"power status: on"* ]] && [[ "$isOn!= "1" ]]
            
    then
                    
    eval "$remotePath --clearall"
                    
    isOn=1
            fi

            sleep 2
    done
    Have fun!
     
    • Like Like x 4
  2. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    Hallo
    Klingt interessant, aber habe von der Materie keine Ahnung.
    Der Raspberry ist am TV angeschlossen, somit also auch Cec möglich.
    Wo müsste ich die Datei einfügen?
     
  3. RaPiiDe

    RaPiiDe New Member

    Messages:
    2
    Hardware:
    RPi3
    I don't speak German so I'm going to answer in English.

    First, you need to copy the script on the raspberry, you've got at least 2 ways to do it. Copy/paste the text in a new file via SSH (with Putty) or you could copy the file via SFTP (Winscp or Filezilla can do that).
     
  4. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    [QUOTE = "RaPiiDe, post: 30626, member: 5271"]

    Dies erfordert Sie das Skript auf die Himbeere kopieren. Sie haben mindestens zwei mögliche, stirbt zu tun. Kopieren Sie den Text in einer neuen Datei über SSH (mit Putty) oder Steuern Sie ihn über SFTP ein (Winscp oder Filezilla können das). [/ QUOTE]
    Hallo danke für die Rückmeldung.
    Wo muss denn die Datei genau hin, habe das Image Raspbian jes. Installiert.
    Benutze kein Kodi auf dem Himbeere, nur extern.

    Thank you for your answer
    I don't know where to insert the script? In which folder or path do I have to paste the script
     
    Last edited: 10 December 2020
  5. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    I saved the script in root, rights are 755, but it doesn't work. What could it be? Image is Raspbian jessie
     
  6. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Hi,
    I've had to fight a bit with it too before getting it to work !!

    First, I'm on windows and created the xxx.sh file with notepad++
    Thing is, windows uses CRLF instead of LF for line feeds...
    That's why it didn't work in the first place.
    If you use notepadd++ too, just double click on the "windows CRLF" text in the bottom-right of the window and choose "convert to UNIX LF" and save.
    Next problem was that I'm on Hyperbian, and it looks like the CEC package was not installed.
    So I ran this command in SSH :
    sudo apt-get install cec-utils

    After this, a simple reboot and it started to work !!!
    Very nice replacement for the signal detection.
     
    • Like Like x 1
  7. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    Hi there
    Thanks for the information.
    I had installed the cec package and inserted the file with wincp
    But maybe I'm doing something wrong.

    I don't understand that at all:
    # 4 - So that it will at startup, you will crontab @reboot /your/path/yourscriptname.sh
     
    Last edited: 21 December 2020
  8. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Ok.
    First, how did you "create" the .sh file ?
    if it is on windows, you'll have the line feed problem as I mentioned, and you need to fix this otherwise it won't work.
    Second, the crontab part is mandatory.
    SSH to your pi (with putty) and then execute this command :
    crontab -e

    This will open a file editor with lot of comments.
    Add this line at the end of the file :
    @reboot /path/to/your/script.sh
    and save.

    Then reboot the Pi and you're good to go.
     
    • Like Like x 1
  9. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    Hi
    It doesn't work
    I switched the TV off and the LEDs goes not off. The LEDs have the standby rainbow colours.
     
  10. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Damn I forgot one thing....
    Somewhere in the script there is "priority 0", you have to change it to "priority 1 "
    Change also "sleep 2" to "sleep 1"
     
  11. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    Hi
    There is the script, where do I have to change that? With remotePath = "PathToYourHyperion-remote" I have to enter the correct path?

    remotePath="hyperion-remote"
    isOn=-1

    while :
    do
    # We get the CEC status
    status=$(echo pow 0 | timeout 5 cec-client -d 1 -s)
    # If it's off and it wasn't already, let's turn off the LEDS
    if [[ "$status" == *"power status: standby"* ]] && [[ "$isOn" != "0" ]]
    then
    eval "$remotePath --priority 0 --color black"
    isOn=0
    # If it's on and it wasn't already, let's turn on the LEDS
    elif [[ "$status" == *"power status: on"* ]] && [[ "$isOn" != "1" ]]
    then
    eval "$remotePath --clearall"
    isOn=1
    fi

    sleep 2
    done
    Have fu

    Can you show me your Skript please
     
    Last edited: 22 December 2020
  12. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    That script is 10 lines long and you can't find where are the words "priority" and "sleep" ? really ?...
    You will find attached the modified script (with correct line feeds).
    Rename it to whatever you named yours et also change its extension to .sh, because the forum doesn't accept .sh files as attachements.
    DO NOT MODIFY ITS CONTENT, UPLOAD IT TO YOUR PI
     

    Attached Files:

    • Like Like x 1
  13. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    thank you very much !!! It works. My script didn't include switching the LEDs to black. I had already adapted the rest of the script like you did. Thank you and happy Christmas
     
    • Like Like x 1
  14. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    390
    Hardware:
    RPi3, 32/64bit
    Normally, there is no need to send Black when you disable a Led device.
    Depending on the Led type, the LEDs are set to black while switching off.
    Other led types, eg the network ones, the device is switched off via API.

    Writing black is only a solution for a subset of devices....
     
    • Like Like x 1
  15. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Thanks for the info.
    So what should we change the commands to enable/disable the led device according to CEC state instead of setting it to black ?
    Thanks in advance.

    EDIT: I mean instead of this command :
    eval "$remotePath --priority 0 --color black"
     
    Last edited: 22 December 2020
  16. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    390
    Hardware:
    RPi3, 32/64bit
    @NeeeeB Please check, if the following works with you:

    Code:
    ./hyperion-remote --disable LEDDEVICE
    
    ./hyperion-remote --enable LEDDEVICE
    The behavior is as you would disable/enable the LED-Device via the Remote-Control in the UI
     
  17. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Ah great !!
    Much better than setting it to black ofc.
    Do I have to replace "LEDDEVICE" with something (e.g name of the instance) or will it work as is ?
     
  18. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    390
    Hardware:
    RPi3, 32/64bit
    Sorry, if you using multiple instances, you can provide the instance via "--instance instancename" as an additional parameter:
    If you leave it out the default instance is used.

    Code:
    ./hyperion-remote --instance 'Nanoleaf' --disable LEDDEVICE
    To see all capabilities of hyperion-remote, just do a

    Code:
    ./hyperion-remote --help
    This might give you additional ideas, too ... :)
     
    • Like Like x 1
  19. NeeeeB

    NeeeeB Active Member

    Messages:
    237
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Thanks, greatly appreciated !
    That works like a charm, much better than setting a black color.
    Will run the --help command to see what else I could do ^^

    Thanks again !
     
    • Like Like x 1
  20. sand04

    sand04 New Member

    Messages:
    17
    Hardware:
    RPi3
    Where should the entry be inserted in the script. Could someone help me, please