Skip to content

Tag: Home Assistant

Restart MM form HA

From time to time the photo module in MagicMirror hangs. I used to restart it manually by logging into the Raspberry and using the command “pm2 restart mm”. Now I want to make it so that it automatically restarts when I scan the NFC tag stuck to the back of the monitor.

So I divided big problem to smaller ones:
1. Restart MM,
2. Connection HA to MM machine,
3. Automation when HA apk scan tag,
4. Stick the NFC tag to the monitor xD

  1. Restart MM – Easy
pm2 restart mm

2. Connection HA to MM mashine

HA needs to let MM know to shut down for a while, so I guess the easiest way to do this is with mqtt broker.
I have used before Mosquitto, so I will use it.

MM site:
I created 2 files in my home directory: and

#!/usr/bin/env python

sleep 20
python /home/pi/ & basen on mqtt-screen-power project:

mqtt_host = ""
mqtt_port = 1883
mqtt_topic = "/screen/restart"
mqtt_username = "xxxxxxxx"
mqtt_password = "xxxxxxxx"
power_on_command = "pm2 restart mm"
power_off_command = " "

import paho.mqtt.client as mqtt
import subprocess

def on_connect(client, userdata, flags, rc):
    # The callback for when the client receives a CONNACK response from the server.
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.

def on_message(client, userdata, msg):
    # The callback for when a PUBLISH message is received from the server.
    print(msg.topic+" "+str(msg.payload))
    if str(msg.payload) == "ON":
                         shell=True, stdout=subprocess.PIPE)
                         shell=True, stdout=subprocess.PIPE)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_host, mqtt_port, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.

Both files chown is 755. Autostart with command “crontab -e” – “@reboot sh /home/XXXXX/”

HA site:

HA configuration.yaml

  - platform: mqtt
    name: "MM restart"
    command_topic: "/screen/restart"


- alias: turn_off_restart_mm
    platform: state
    entity_id: switch.mm_restart
    to: 'on'
      seconds: 1
    service: switch.turn_off
    entity_id: switch.mm_restart

3. Automation when HA apk scan tag,

HA automations.yaml:

- alias: mm_tag
    platform: event
    event_type: tag_scanned
      tag_id: kitchen_tag
    service: switch.turn_on
    entity_id: switch.mm_restart

4. Stick the NFC tag to the monitor. Formality.

Changing the hours of light in HA automations

I wanted the lights above my kitchen counter to light up faster in the evening and later in the morning, I did it this way:

- alias: Kitchen_leds_on
  initial_state: true
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00031c90e3
    from: 'off'
    to: 'on'
    condition: or
      - condition: numeric_state
        entity_id: 'sensor.illumination_34ce00fb6930'
        below: 100
      - condition: time
        after: '19:00:00'
      - condition: sun
        after: sunset
        after_offset: '-01:00:00'
      - condition: sun
        before: sunrise
        before_offset: '01:00:00'
    service: light.turn_on
    entity_id: light.kitchen_leds

Magic Mirror on Raspberry Pi 3B+



To have such an information monitor, you need to have some unnecessary things, like a monitor and a raspberry, and buy a monitor holder and an hdmi-dvi adapter. I had nothing, so I bought parts. For the monitor I went to a computer store and asked about an old monitor, they gave me a 17″ for free.




Steps in few words:

  1. Install Raspberry Pi OS with desktop
  2. Install MagicMirror
  3. Install modules:
    • MMM-homeassistant-sensors – sensors from Home Assistant (temp, pressure, humidity, pm2.5, etc.)
    • MMM-Spotify – shows some music playing on Spotify
    • MMM-Wallpaper – based on reddit or bing wallpaper changer
    • MMM-GooglePhotos – shows your albums
    • MMM-HTMLBox – shows weather from
    • MMM-SystemStats – shows rpi stats
  4. Autostart MagicMirror

If you don’t have money for a big memory card or you use a recycled one, you have to watch the memory so that MagicMirror logs don’t eat all the memory. I solved this by installing logrorate.

with this settings: