Magic Mirror Control From HA

This Is Mark 1 verson have to start somewhere

Built this verson as a package

can turn screen on/off

sensor to show if its on/off

can reboot it

or

shut it down

#=======================================================================
# Do a FIND REPLACE
# MMIPADDRESS with the the magicMirror ipaddress ???.???.???.???
#=======================================================================
homeassistant:
  customize:
    package.node_anchors:
     customize: &customize
       package: 'Magic Mirror' 

    sensor.mm_display:
      icon: mdi:monitor
      friendly_name: Mirror Display
    switch.mm_display:
      icon: mdi:monitor
      friendly_name: Mirror Display
    script.mm_reboot:
      icon: mdi:lock-reset
    script.mm_shutdown:
      icon: mdi:power

sensor:
  - platform: rest
    resource: http://MMIPADDRESS:8080/remote?action=MONITORSTATUS
    name: mm display
    value_template: '{{ value_json.monitor }}'
#=======================================================================
#
#=======================================================================
switch:
  - platform: command_line
    switches:
      mm_display:
        command_on: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITORON'"
        command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITOROFF'"

#=======================================================================
#
#=======================================================================
shell_command:
  mm_reboot: "curl -s 'http://MMIPADDRESS:8080/remote?action=REBOOT'"
  mm_shutdown: "curl -s 'http://MMIPADDRESS:8080/remote?action=SHUTDOWN'"
#=======================================================================
#
#=======================================================================
script:
  mm_reboot:
    alias: Magic Mirror Reboot
    sequence:
    - data: {}
      service: shell_command.mm_reboot
  mm_shutdown:
    alias: Magic Mirror ShutDown
    sequence:
    - data: {}
      service: shell_command.mm_shutdown   

all you need to is put above code into a file inside the package folder

do a find replace MMIPADDRESS with the IPaddress of the Magic Mirror

and the mirror MUST have This module in it

Leaning how to send message to the mirror will update when I know

the lovelace BIT

image

      - type: entities
        show_header_toggle: false
        title: "Magic Mirror"
        entities:
          - switch.mm_display
          - entity: sensor.mm_display
            name: Mirror Display 
          - script.mm_reboot
          - script.mm_shutdown

if you dont know what magic mirror is

https://magicmirror.builders/

using my old PI which was HASS got HASS on real PC now

7 Likes

Been thinking more about this. Think I need to lean some python and write my own plugin
we dont where to start tho

i did get this this working today

import json
import urllib.request
module_data = urllib.request.urlopen('http://IPADDRESS:8080/remote?action=MODULE_DATA')
modules = json.loads(module_data.read())
print(modules)
for module in modules["moduleData"]:
        print(module["name"] )
        print(module["hidden"])

which list the module i have loaded


happy with
was thinking put that into a input select and getting HA to turn hide them

can some point me down the right manual to read

added ====== 27/06/18
found this post

group_entities = hass.states.get('group.all_lights').attributes['entity_id']
all_lights = []
for e in group_entities:
    all_lights.append(e)
service_data = {'entity_id': 'input_select.timer_generico7',
                'options': all_lights}
hass.services.call('input_select', 'set_options', service_data)

run it and it worked
so …
got me thinking

import json
import urllib.request
module_data = urllib.request.urlopen('http://blabla:8080/remote?action=MODULE_DATA')
modules = json.loads(module_data.read())
my_modules = []
for module in modules["moduleData"]:
        my_modules.append(module["name"])
service_data = {'entity_id': 'input_select.mm_modules',
                'options': my_modules}
hass.services.call('input_select', 'set_options', service_data)

all I got was

:frowning:

where the manual when you need it
look like you can’t import modules

so delete the import still not working

one good thing less error lines LOL

after more read found out you can’t import :frowning:

2 Likes

Thanks a lot for this, easily got my mirror controllable from home assistant thanks to you!
Plugin would also be nice but it seems hard to make one without any experience, did you ever get further with that?

Hi @myle,

Sorry to revive an old thread. I am trying to implement this but I’m a newbie in HA and I’d like to ask for your help please.

When I try to add the Lovelace custom card, there’s the error I am receiving:

Can you (or any other member) please help me figure out what I am doing wrong?

This is my magic_mirror.yaml file:

And this is my configuration.yaml file:

let see what you did wrong

I see you have packages

then in the package folder

i have a folder mm

then In side that folder I have paskage data ( the config)

image

and the mm.yaml inside that

than that file has

#=======================================================================
# Do a FIND REPLACE
# MMIPADDRESS with the the magicMirror ipaddress ???.???.???.???
#=======================================================================
homeassistant:
  customize:
    package.node_anchors:
     customize: &customize
       package: 'Magic Mirror' 

    sensor.mm_display:
      icon: mdi:monitor
      friendly_name: Mirror Display
    switch.mm_display:
      icon: mdi:monitor
      friendly_name: Mirror Display
    script.mm_reboot:
      icon: mdi:lock-reset
    script.mm_shutdown:
      icon: mdi:power

sensor:
  - platform: rest
    resource: http://MMIPADDRESS:8080/remote?action=MONITORSTATUS
    name: mm display
    value_template: '{{ value_json.monitor }}'
#=======================================================================
#
#=======================================================================
switch:
  - platform: command_line
    switches:
      mm_display:
        command_on: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITORON'"
        command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITOROFF'"

#=======================================================================
#
#=======================================================================
shell_command:
  shell_mm_reboot: "curl -s 'http://MMIPADDRESS:8080/remote?action=REBOOT'"
  shell_mm_shutdown: "curl -s 'http://MMIPADDRESS:8080/remote?action=SHUTDOWN'"
#=======================================================================
#
#=======================================================================
script:
  mm_reboot:
    sequence:
    - data: {}
      service: shell_command.shell_mm_reboot
  mm_shutdown:
    sequence:
    - data: {}
      service: shell_command.shell_mm_shutdown   

if i remember correctly you have to drop alias: out of the scripts part

do you config check if you get the green light reboot and it should be there

Hello @myle,

I also want to shutdown the MM pi via an automation.

I configured this via the rest_commands but when I run the service calls then nothing happens.

I had also looked up the api page on my install http://ip-of-your-mirror:8080/api/docs where I can also test the calls.

What I noticed is that also there they don’t work unless I authorize them with the API key that I have in my MM config. My guess is that I need to authorize HA with the API key to allow it to call the service.

I also tried removing the API key from my MM config and then I could run things like monitor on and off but not shutdown

Shutdown It gives me the error that no API key is provided in confg. So I need to use the API Key

Do you have any idea where I need to do that in the HA config? I did not see anything relating to API key in your code.

did you read this

Yes, but did not read the API Readme at least not with a fresh mind yesterday. Now I found the answer and pass the API key along and yes it works.

rest_command:
turn_off_mm_screen:
url: http://192.168.xx.xx:8080/api/monitor/off?apiKey=xxxx

Thanks again for the inspiration with your setup, will tinker with it a bit more but now it works in general that’s awesome

@myle
sorry to bump this but I was having a bit of struggle getting some entities to show up as well as:

an error that plagued me for a few days now and I can’t seem to find out what it can’t see the shell commands.
“Unable to find service shell_command.mm_reboot”

image

I have been able to run all the commands in the HA terminal addon as well as learned how to setup a few SSH keys. I ran into the problem where the SSH commands were all timing out from the HA terminal so I tried to go back to curl commands which now work. However, they only work from the terminal.

Any help or tips would be greatly appreciated!
-JJ

did you add the shell command to your config file

#=======================================================================
shell_command:
  shell_mm_reboot: "curl -s 'http://MMIPADDRESS:8080/remote?action=REBOOT'"
  shell_mm_shutdown: "curl -s 'http://MMIPADDRESS:8080/remote?action=SHUTDOWN'"
#=======================================================================

Looks like the rules have change

need to read this

should look something like this

command_line:
  - switch:
      name: mm_display
        command_on: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITORON'"
        command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITOROFF'"

Got it working with this code:

#=======================================================================
# Do a FIND REPLACE
# MMIPADDRESS with the the magicMirror ipaddress ???.???.???.???
#=======================================================================
homeassistant:
  customize:
    package.node_anchors:
      customize: &customize
        package: "Magic Mirror"

    switch.mm_display:
      icon: mdi:monitor
      friendly_name: Mirror Display
    switch.mm_reboot:
      icon: mdi:reload
      friendly_name: Mirror Reset
    switch.mm_shutdown:
      icon: mdi:power
      friendly_name: Mirror Shutdown

# rest:
#   - resource: "http://MMIPADDRESS:8080/remote?action=MONITORSTATUS"
#     scan_interval: 15
#     sensor:
#       - name: "MM Status"
#         value_template: "{{ value_json.success }}"
# #=======================================================================
# #
# #=======================================================================
command_line:
  - switch:
      name: mm_display
      command_on: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITORON'"
      command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITOROFF'"
      command_state: "curl -s 'http://MMIPADDRESS:8080/remote?action=MONITORSTATUS'"
      value_template: "{{ value_json.success }}"
      # scan_interval: 30
  - switch:
      name: mm_reboot
      command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=REBOOT'"
  - switch:
      name: mm_shutdown
      command_off: "curl -s 'http://MMIPADDRESS:8080/remote?action=SHUTDOWN'"
# #=======================================================================
# #
# #=======================================================================

And for the tile card I had to change the bottom two to buttons as it only has a command_off because the two icons bothered me.

type: vertical-stack
cards:
  - type: markdown
    content: '# Magic Mirror'
  - type: vertical-stack
    cards:
      - type: entities
        entities:
          - entity: switch.mm_display
      - show_name: true
        show_icon: true
        type: button
        entity: switch.mm_reboot
        icon: ''
        show_state: false
        icon_height: 25px
      - show_name: true
        show_icon: true
        type: button
        entity: switch.mm_shutdown
        icon: ''
        show_state: false
        icon_height: 25px

image

Hope this helps anyone else.

I had some issues with turning off the display for the pi and had to change the driver over in the configuration otherwise I wasn’t able to put the monitor to sleep.

These are my commands and why I had to change the value_json.monitor to value_json.success in the above status call you can see that the changed variable was the success flag from false to true.

monitorStatusCommand

xrandr --query -d :0 | grep -q "HDMI-1 connected primary [0-9]"

monitorOnCommand

xrandr -d :0 --output HDMI-1 --auto

monitorOffCommand

xrandr -d :0 --output HDMI-1 --off

Before the driver change from the Raspberry Pi Configration the monitor was showing as WAYLAND (?I believe is the spelling) as the xrandr -d :0 response. After that it showed up as HDMI-1.

change the driver via these commands (change from wayfire to x11):

sudo raspi-config

6. Advance Options

A6 Wayland

W1 X11

One other feature I had to turn off was screen blanking also otherwise the monitor would shut off without the mouse moving or other input after the driver change.

1 Like