Sudo script/shell/switch

Has anyone got any of the above to work in HA 0.43.2. What I mean is something like the following…


set_MBBRedBot: 'sudo /usr/local/bin/magicblueshell -m C1:40:5D:6E:A3:E8 -c \"set_color {{states.input_select.magicbluecolours.state}}\"'

      command_on:  sudo /home/homeassistant/.homeassistant/  <----this command has worked for months but now doesn't work

      command_on:  sudo /home/homeassistant/.homeassistant/
contents of .sh file is...

sudo /usr/local/bin/magicblueshell -c 'set_color red' -m c1:40:5d:6e:a3:e8

tried both neither work in HA. However both commands work from the command line in ssh as a pi user, neither work in the venv as sudo is not allowed.

     command_on:  sudo "/usr/local/bin/magicblueshell -c 'set_color green' -m F2:F1:09:E6:DE:78"
Normal command_on doesn't work either.

Error logs just say the command failed.

It seems that any command with sudo in it won’t work? Does anyone have any more ideas, as I can’t think of anything else to try :frowning:

Remember that anytime you shell out from HA, you do so as the HA user. For sudo to work, you’ll have to add the HA user to your sudoers list.

Yep, I did that months ago :frowning: however you are also correct as it turns out it makes a big difference where you put the command, which I hadn’t realised till now :frowning: So for anyone else with similar problems put the homeassistant command at the end of the sudoers file…

homeassistant ALL=NOPASSWD: ALL

otherwise it takes the last use of the command which means it may not work.

1 Like

Was adding “homeassistant ALL=NOPASSWD: ALL” to the sudoers file the only method?

I’m finding certbot from let’s encrypt needs admin and the only way to get it to run it with the sudoers file.

Ref: FAQ: Do I need root (superuser) to use letsencrypt? #1741

The steps Remote Access with TLS/SSL via Let’s Encrypt do not make mention of editing the sudoers file, but the steps are not working for me without it.

Hi, I’ve tried almost everything I can think of, i’m unable to run the following commands as “homeassistant”

i switch to env, source bin/activate when running tvservice -o or tvservice -p i get the following error [E] Failed to initialize VCHI (ret=-1)

I’ve updated my /etc/sudoers to:
homeasssistant ALL = (root) NOPASSWD: /home/homeassistant/magic/, /home/homeassistant/magic/


tvservice --preferred

(homeassistant) homeassistant@raspberrypi:~/magic $ bash ./
[E] Failed to initialize VCHI (ret=-1)


  • platform: command_line
    friendly_name: “Magic Mirror”
    command_on: bash ./home/homeassistant/magic/
    command_off: bash ./home/homeassistant/magic/

All I would like to do is create a command_line switch in HA to turn off/on Magic Mirror Screen. Once it’s in HA I can create a script or scene to control it with Google Home.

Please Help :frowning:

I’m working on doing literally the exact same thing as you!! I just now figured this out, actually. Apparently you have to add the homeassistant user to the “video group.” Not entirely sure how that all works, but I did it with this command (as the pi user):
sudo adduser homeassistant video.

Now, to be perfectly honest, my command line switch still isn’t working (I think it’s because I can’t figure out how to get the command_state right) but at least I can definitely turn on/off HDMI power from the homeassistant profile!

Hey, quick update on this: I have exactly no idea what happened or why, but I had to restart my pi (for other reasons), and it works now. I’ll take it.

Here’s what I have in my config file (hass is running as a different user on the same machine as my mirror display):

  platform: command_line
      command_on: "/usr/bin/tvservice --preferred"
      command_off: "/usr/bin/tvservice --off"
      command_state: '/usr/bin/tvservice -s | grep -o "120006\|120002"'
      value_template: '{{ value == "120006" }}'

Now it looks and acts like a normal switch – the command_state and value_template parts are how it checks the HDMI status. There are a bunch of codes that it gives for different modes, so you may need to check for yourself. run tvservice --status and the line that it spits out should have “state 0x120***” which is the code you need. Run that twice: once when it’s on, and once when off. As long as you’re just shooting for on/off on the same display, you should only need those two 120xxx values, and you can update the grep and the value_template to match your tvservice codes.

Good luck!!

Hey thanks for the help, I had it working with the following code:


  • platform: command_line
    command_on: “ssh [email protected] ‘sudo tvservice -p’”
    command_off: “ssh [email protected] ‘sudo tvservice -o’”
    friendly_name: Magic Mirror

but i first had to do the following.
switch to Homeassistant user run ssh-keygen press enter for all then ssh-copy-id [email protected]

The status would definitely be a great addition, wasn’t aware it was possible.

I’ve got two questions, i’ve updated my code to yours, seems our tvservice statuses are the same.

tvservice -s
state 0x120002 [TV is off]
tvservice -s
state 0x120006 [DVI DMT (83) RGB full 16:9], 1600x900 @ 60.00Hz, progressive

However in HA it doesn’t seem to show the code, a simple test would be to restart HA leaving the screen on, HA will display it as off. My screen turns on by default after a system reboot which means the status would always be wrong.

Second question, I noticed you have the Magic Mirror too, do you know the actual commands to display the modules. I would like to create scenes/scripts and access this from google home via voice interaction.

Thanks again for all your assistance.
btw can you send a screenshot of your Magic Mirror would like to see what you have on yours. I’m going to try to add HA as a iframe to display its status.

  1. From what I can tell, the key part of the status is the value_template bit. To be completely honest, I’m not entirely sure how templates work in HA – I get the idea but the syntax is something I’m still trying to wrap my head around. I basically only made it work by messing around and trying a bunch of different versions until something worked. Set up like this, I don’t actually ever see the codes, the status bit just aligns the toggle the right way. All I can think is that there’s an issue with the ssh poll? There may be a way to change the interval of its checks that might help. Weird that you can see it when you do normally, but hass won’t. Sorry I’m not more help on that one but I’m still not entirely sure why mine works.

  2. ooooh, that’s a good idea, I like that. Now, there may be a better way, but the best that I know of (and not incidentally, the way I do it), is to use the MMM-Remote-Control module. As it happens, I read on the MM forum that basically every function of the remote module actually also adds a URL endpoint for that function! I use several of these for IFTTT. This all includes a method to send notifications to various modules that can trigger their actions (showing, hiding, etc.). In fact, I originally set up HA and IFTTT to turn the display on with a GET request to http://[my domain with forwarded ports]:8080/remote?action=MONITORON. Something like that utilizing the SHOW and HIDE actions could work really well. That was really cool thinking on your part, I’m gonna look into doing that for sure.

  3. So it’s very much still a work in progress but my “mirror” is actually more of a hopefully-all-encompassing display board. Actually, right now it’s just an old monitor chilling on its side but eventually I’ll frame it and hang it on the fridge or something cool. First I gotta work on the layout/design/background/surfacing useful info. But I’ll attach a screenshot! I’ve done a lot of custom CSS, or at least, a lot for someone who doesn’t know CSS. Lemme know if you have questions on it!

i love it, looks clean. i just made a mirror, would you mind sharing your custom.css and config for the NOAA module?

The weather module on the above pic is darksky. probably not NOAA

yea that looks right. can you share the css

ive played with that module. im looking for the css, the percip graph doesnt come out like that and i havent been able to figure that out. I got everything else pretty much how i want it. i use carasoul with a few differnt modules and sensors coming from HA its nice. i like that weather alot better though…if you can share the css…


You mean the graph? if so put it inside config

enablePrecipitationGraph: true
alwaysShowPrecipitationGraph: true (default is false)

By the way, I use default weather that comes with magic mirror.

He has some custom colors, i am looking for the custom.css

dang I thought i read every single post in that forum lol. aprreciate that one!

Guessing you have a mirror? Are you doing any automations with mmm-remote? I have a few going, its probably a hack way and Im sure theres a better way to get it done, but this has been working for me. Except this alert I have been trying to add, it works fine when i test it outside HA…but i get a syntax error when i try to add it like the others…any idea?

So these work fine…
rest commands
url: ‘http://mirror:8080/remote?action=HIDE&module=module_3_MMM-Sonos

  url: 'http://mirror:8080/remote?action=SHOW&module=module_3_MMM-Sonos'

  alias: "Hide Sonos"
    - service: rest_command.hide_sonos

  alias: "Show Sonos"
    - service: rest_command.show_sonos

But Ive tried double quotes, differnet brackets, and i always get a syntax error…but again, it works outside of HA.

  url: " http://mirror:8080/remote?action=NOTIFICATION&notification=NOTIFICATION&payload={%22title%22:%22Alert%22,%22message%22:%22Test%20Test%20Test%22} "

I’m new to magic mirror. Just finish mine about a week. I do link my magic mirror with ha and control magic mirror using mmm-remote-contol by switch in ha and then intergrate with voice as well.

I use also use voice for calender. this can be done with siri, google and alexa to add appointment and display in magic mirror