Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use without API-password? #7

Open
olskar opened this issue Dec 1, 2018 · 34 comments
Open

How to use without API-password? #7

olskar opened this issue Dec 1, 2018 · 34 comments

Comments

@olskar
Copy link

olskar commented Dec 1, 2018

Since api password is being removed from Home Assistant, How to use without API-password?

@Kugelfang666
Copy link

I can certainly double that, would be great to get some more instruction on how to set this plugin up for a token authentication. Where do I have to put the token to?

@aserramonner
Copy link
Owner

I have not tried that, but maybe using whitelisting (https://www.home-assistant.io/docs/authentication/providers/) may work for a magic mirror use case, since all api accesses will come from the same IP address.

@Kugelfang666
Copy link

Kugelfang666 commented Dec 6, 2018

I did get it to work! I had to extra file containing the environmental variable which actually the starts the mm.sh script

I created a magic mirror.config.js in the same folder as the mm.sh

module.exports = {
  apps : [
      {
        name: "MagicMirror",
        script: "./mm.sh",
        watch: true,
        env: {
          "HASSIO_TOKEN": "XXXXXXXXXXXX“
        }
      }
  ]
}

then I started the MM with

pm2 reload magic mirror.config.js

it works quite brilliantly

EDIT1: Changed the rotation mark according to chrismag1c (see below)

@prosonik
Copy link

Sorry, I'm a newish to Magic Mirror. I've tried the HA Magic Mirror addon, and then running magic mirror from a Docker. I'm not sure how to approach the subject above. Where would i find the mm.sh?

@ivanovd
Copy link

ivanovd commented Dec 20, 2018

I did get it to work! I had to extra file containing the environmental variable which actually the starts the mm.sh script

I created a magic mirror.config.js in the same folder as the mm.sh

module.exports = {
apps : [
{
name: "MagicMirror",
script: "./mm.sh",
watch: true,
env: {
"HASSIO_TOKEN": „XXXXXXXXXXXX“
}
}
]
}

then I started the MM with

pm2 reload magic mirror.config.js

it works quite brilliantly

Can you please post your config.js
I tried that but I couldn't get it to work.

@Kugelfang666
Copy link

Kugelfang666 commented Dec 24, 2018

{
                 module: "MMM-HASS",
                 header: "Sensors",
                 position: "top_right",
                 config: {
                   host: "PUT IP HERE",
                   port: "8123",
                   apipassword: false,
                   hassiotoken: true,
                   https: false,
                   devices: [
                     { deviceLabel: "WM",
                         deviceReadings: [
                         { sensor: "sensor.wm", icon: "fa fa-plug", suffix: "W"}
                          ]
                     },
                     { deviceLabel: "3D-Printer",
                         deviceReadings: [
                         { sensor: "sensor.printer", icon: "fa fa-print", suffix: "%",}
                           ]
                     },
                     { deviceLabel: "Plant A",
                         deviceReadings: [
                         { sensor: "sensor.a", icon: "wi wi-humidity", suffix: "%"}
                          ]
                     },
                     { deviceLabel: "Plant B",
                         deviceReadings: [
                         { sensor: "sensor.b", icon: "wi wi-humidity", suffix: "%"}
                          ]
                     },
                     { deviceLabel: "Plant C",
                         deviceReadings: [
                         { sensor: "sensor.c", icon: "wi wi-humidity", suffix: "%"}
                          ]
                     },
                     { deviceLabel: "Plant D",
                         deviceReadings: [
                         { sensor: "sensor.d", icon: "wi wi-humidity", suffix: "%",}
                           ]
                     }
                  ]
                }
}

@bachoo786
Copy link

@Kugelfang666 so I created the mirror.config.js file and copied your code and saved the file in the directory where the mm.sh is i.e. my home folder.

I then added your above code to my config.js and when I restart my mm using "pm2 restart mm" I get invalid config.

What have you done differently to mine?

@Kugelfang666
Copy link

@bachoo786:

  • did you put the IP address of your HASSIO instance into the config code?
  • did you put an actual long lasting token into the mirror.config.js?

also I just realized I missed one } in the code I posted. please check your config

@bachoo786
Copy link

Is this for hassio or long lived tokens?

@Kugelfang666
Copy link

I created a user within the home assistant GUI dedicated for the magic mirror. For this very user I retrieved the long lived token and put it into the environmental variable list

@bachoo786
Copy link

So you are not using hassio?

@Kugelfang666
Copy link

I'm using hassio on a raspberry pi

@bachoo786
Copy link

Ok I am running HA on docker

@Kugelfang666
Copy link

ok, but does it make a difference on how this plugin interacts with home assistant?

@bachoo786
Copy link

I don't think so

@chrismag1c
Copy link

chrismag1c commented Jan 3, 2019

I did get it to work! I had to extra file containing the environmental variable which actually the starts the mm.sh script

I created a magic mirror.config.js in the same folder as the mm.sh

module.exports = {
  apps : [
      {
        name: "MagicMirror",
        script: "./mm.sh",
        watch: true,
        env: {
          "HASSIO_TOKEN": „XXXXXXXXXXXX“
        }
      }
  ]
}

then I started the MM with

pm2 reload magic mirror.config.js

it works quite brilliantly

yes it works, thanks.
Please consider to use the right quotation marks for the token. I copied the code from here and didn't notice.
It should be "HASSIO_TOKEN": "XXXXXXXXXXXX" instead of „XXXXXXXXXXXX“.

I created a new magic.config.js (MagicMirror/installers) with the content from above.
I stopped mm:
pm2 stop mm

And started:
pm2 start magic.config.js

@Kugelfang666
Copy link

thanks for the finding, I must have screwed up the "" when removing my token, thanks for the finding. I edited my original post accordingly!

@wangjiwang
Copy link

I did get it to work! I had to extra file containing the environmental variable which actually the starts the mm.sh script

I created a magic mirror.config.js in the same folder as the mm.sh

module.exports = {
  apps : [
      {
        name: "MagicMirror",
        script: "./mm.sh",
        watch: true,
        env: {
          "HASSIO_TOKEN": "XXXXXXXXXXXX“
        }
      }
  ]
}

then I started the MM with

pm2 reload magic mirror.config.js

it works quite brilliantly

EDIT1: Changed the rotation mark according to chrismag1c (see below)

sorry, I don not have mm.sh file. I only have a run-start.sh file in the MagicMirror folder. and it contains these code:
if [ -z "$DISPLAY" ]; then #If not set DISPLAY is SSH remote or tty
export DISPLAY=:0 # Set by default display
fi
electron js/electron.js $1

I usually use the command "npm start "to run magic mirror. Can you tell me more details?

@Kugelfang666
Copy link

i see, I just updated my MM and in the newer releases it works a bit different:
in \MagicMirror\installers

I changed the pm2-MagicMirror.json to look like this:

{
  "apps" : [{

    "name"        : "MagicMirror",
    "script"      : "/home/pi/MagicMirror/installers/mm.sh",
    "watch"       : ["/home/pi/MagicMirror/config/config.js"],
    "env"         : {
      "HASSIO_TOKEN": "YOUR TOKEN HERE",
    }
  }]
}

then I created a new file in the same folder called mm.sh which noch contains:

cd ~/MagicMirror
DISPLAY=:0 npm start

this way pm2 starts the first file which contains the token. In turn this file then starts the mm.sh wich initiates the magidmirror.

@ivanovd
Copy link

ivanovd commented Apr 8, 2019

@Kugelfang666 I have absolutely the same setup as yours but I still get only up to the point where the module is loaded but it keeps on “Loading” in MM

I do have the long lasting token in HA set up and copied to pm2-MagicMirror.json

However my HA is only accessed via HTTPS, so the only change in my config is that https is true not false, however I tried both true and false and it still fails.

Any thoughts?

@ivanovd
Copy link

ivanovd commented Apr 8, 2019

I’ve made some progress after following the instructions in #8

Now the module loads the sensors names, but the readings are “undefined” …

The HA log shows “Login attempt or request with invalid authentication from XXX.XXX.X.X (the IP of the MM)”, however the LL token is correct. I deleted and created a new one and copy-pasted it in the json … don’t know what is going wrong...

@Kugelfang666
Copy link

very wired. did you create a dedicated user for the mirror? if not try it. It could be that HASSIO does not like the identical user being logged in via different machines.

What I did was to create a dedicated user on my HA. then I logged in as such and created a new permanent token

@ivanovd
Copy link

ivanovd commented Apr 9, 2019

Yeah, I thought the same then I read through the whole topic again and noticed that you have separate accounts and tried that as well - didn't work ...

MaxthonSnap20190409100820

I think SSL is what breaks things ...

@Kugelfang666
Copy link

Are you using a self signed certificate or letsencypt? And did you try without ssl?

@ivanovd
Copy link

ivanovd commented Apr 9, 2019

I am using LetsEncrypt in the DuckDNS addon.

Here is my starting point as of now:

configuration.yaml

http:
  base_url: https://XXXXXX.duckdns.org
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
  api_password: !secret http_password #I use this for IFTTT recipies that triger scripts
  ssl_profile: intermediate

DuckDNS config:

{
  "lets_encrypt": {
    "accept_terms": true,
    "certfile": "fullchain.pem",
    "keyfile": "privkey.pem"
  },
  "token": "XXXXXXXXX",
  "domains": [
    "XXXXXX.duckdns.org"
  ],
  "seconds": 300
}

pm2_MagicMirror.json

~/MagicMirror/installers $ cat pm2_MagicMirror.json
{
  "apps" : [{
    "name"        : "MagicMirror",
    "script"      : "/home/pi/MagicMirror/installers/mm.sh",
    "watch"       : ["/home/pi/MagicMirror/config/config.js"],
    "env"         : {
      "HASSIO_TOKEN": "XXXXXXXXX"
    }
  }]
}

config.js

{
                 module: "MMM-HASS",
                 header: "Сензори",
                 position: "top_left",
                 config: {
                   host: "192.168.1.19", // I have tried hassio.local xxxxx.duckdns.org as well
                   port: "8123",
                   apipassword: false,
                   hassiotoken: true,
                   https: true,
                   devices: [
                     { deviceLabel: "Температура",
                         deviceReadings: [
                         { sensor: "sensor.temperature_158d000273cb7c", icon: "fa fa-temperature-high", suffix: " C"}
                          ]
                     },
                     { deviceLabel: "Влажност",
                         deviceReadings: [
                         { sensor: "sensor.humidity_158d000273cb7c", icon: "fa fa-humidity", suffix: " %",}
                           ]
                     },
                     { deviceLabel: "Налягане",
                         deviceReadings: [
                         { sensor: "sensor.pressure_158d000273cb7c", icon: "fa fa-cloudscale", suffix: " hPa"}
                          ]
                     }
                  ]
                }
},

I will keep trying without the SSL and DuckDNS to see if that makes difference and will report later.

Thanks for the support @Kugelfang666

@ivanovd
Copy link

ivanovd commented Apr 9, 2019

I am getting the following error in the pm2 logs on MM no matter if I use SSL or not:

TypeError: Cannot read property 'entity_id' of undefined
    at Request._callback (/home/pi/MagicMirror/modules/MMM-HASS/node_helper.js:185:26)
    at self.callback (/home/pi/MagicMirror/modules/MMM-HASS/node_modules/request/request.js:185:22)
    at Request.emit (events.js:182:13)
    at Request.onRequestError (/home/pi/MagicMirror/modules/MMM-HASS/node_modules/request/request.js:881:8)
    at ClientRequest.emit (events.js:182:13)
    at TLSSocket.socketErrorListener (_http_client.js:382:9)
    at TLSSocket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
ATTENTION: default value of option force_s3tc_enable overridden by environment.
(node:20699) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

@Kugelfang666
Copy link

the configuration of the pm2 file and the config.js looks ok to me,

maybe just a simple question: from the error message I would assume that the actual connection is ok. did you try polling another sensor?

Beyond this I'm afraid thy my expertise ends here :-)

@Kugelfang666
Copy link

one more thing: does the SSL protocol also listen to port 8123??

@ivanovd
Copy link

ivanovd commented Apr 9, 2019

@Kugelfang666 yeah I am out of ideas as well ...

The connection is fine, I can see that MM is trying to pull the data from HA but fails to authenticate, so trying any other sensor also fails. I am sure that there is something fishy with the authentication but don't know what ... I tried with and without SSL, same thing ... I updated the module, raspbian, HA ... everything is up-to-date but still:
MaxthonSnap20190409192757

I will leave that for now :) If I get inspired another day I will try harder to solve this :D

Thanks for your input though :)

@ax42
Copy link

ax42 commented Nov 7, 2019

Maybe it helps others in my situation. I run MM in a docker container in server-only mode. For this, add

-e HASSIO_TOKEN="XXXXXXXXXX"

to your docker command (instead of adding it to the pm2 json).

@iorifly
Copy link

iorifly commented Jan 8, 2020

[03:20:26.810] [ERROR] Error: Cannot find module 'underscore'
at Module._resolveFilename (internal/modules/cjs/loader.js:602:15)
at Function.Module._resolveFilename (/home/pi/MagicMirror/node_modules/electron/dist/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._resolveFilename (/home/pi/MagicMirror/node_modules/module-alias/index.js:49:29)
at Function.Module._load (internal/modules/cjs/loader.js:528:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object. (/home/pi/MagicMirror/modules/MMM-HASS/node_helper.js:14:9)
at Object. (/home/pi/MagicMirror/modules/MMM-HASS/node_helper.js:227:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:722:10)
[03:20:26.819] [LOG] Whoops! There was an uncaught exception...
[03:20:26.822] [ERROR] { Error: Cannot find module 'underscore'
at Module._resolveFilename (internal/modules/cjs/loader.js:602:15)
at Function.Module._resolveFilename (/home/pi/MagicMirror/node_modules/electron/dist/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._resolveFilename (/home/pi/MagicMirror/node_modules/module-alias/index.js:49:29)
at Function.Module._load (internal/modules/cjs/loader.js:528:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object. (/home/pi/MagicMirror/modules/MMM-HASS/node_helper.js:14:9)
at Object. (/home/pi/MagicMirror/modules/MMM-HASS/node_helper.js:227:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:722:10) code: 'MODULE_NOT_FOUND' }
[03:20:26.855] [LOG] MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection?
[03:20:26.858] [LOG] If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues

@Kugelfang666
Copy link

@Kugelfang666 yeah I am out of ideas as well ...

The connection is fine, I can see that MM is trying to pull the data from HA but fails to authenticate, so trying any other sensor also fails. I am sure that there is something fishy with the authentication but don't know what ... I tried with and without SSL, same thing ... I updated the module, raspbian, HA ... everything is up-to-date but still:
MaxthonSnap20190409192757

I will leave that for now :) If I get inspired another day I will try harder to solve this :D

Thanks for your input though :)

After some Time I had to reset my token and ran into the same problem. After some digging I found the solution: There seems to be a bug so that pm2 does not read the pm2_MagicMirror.json correctly. SSH into the MM and going into the installers Folger I ran pm2 reload pm2_MagicMirror.json and the problem was gone

@wormiedk
Copy link

wormiedk commented Feb 8, 2021

Replace line 130 in hode_helper.js with:
post_options.headers = { 'Authorization' : 'Bearer ' + config.token };
and line 175 in node_helper.js with:
get_options.headers = { 'Authorization' : 'Bearer ' + config.token };

Then add a token parameter to your config:

		{
			module: "MMM-HASS",
			position: "top_left",
			config: {
					host: "serverip",
					port: "8123",
					hassiotoken: true,
					token: "redacted",
					https: false,
					devices: [
					{ deviceLabel: "Ude",
							deviceReadings: [
							{ sensor: "sensor.ude_zb_temperature", icon: "wi wi-thermometer", suffix: "°"},
							{ sensor: "sensor.ude_zb_humidity", icon: "wi wi-humidity", suffix: "%"},
							{ sensor: "sensor.ude_min", icon: "wi wi-thermometer", su""}
							]
					},
					{ deviceLabel: "Inde",
							deviceReadings: [
							{ sensor: "sensor.alrum_motion_temperature", icon: "wi wi-thermometer", suffix: "°", notification: "INDOOR_TEMPERATURE"}
							]
					}
					]
			  }
		},

Then just install with npm install and no need for environment parameters :)

@alejandrogonzalez01
Copy link

Maybe it helps others in my situation. I run MM in a docker container in server-only mode. For this, add

-e HASSIO_TOKEN="XXXXXXXXXX"

to your docker command (instead of adding it to the pm2 json).

legend. Here we are in 2022, needing to apply work arounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests