Mapping values are not allowed here error

Does anyone know why I am getting the following error when trying to add a command_line sensor to check the status of a service:

2019-01-29 18:53:08 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 76
2019-01-29 18:53:08 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 76

I am using the configuration:

sensor:
 - platform: command_line
   name: Plex Service
   command: systemctl status plexmediaserver.service | grep -oP '(?<=Active: ).\w+'

line 188, column 76 is the : in (?<=Active: )

When the above code is run as a command line at a console it works ok, giving active or inactive as the result depending on the service status

Searching the forums does find others with this error, but none of the solutions for them fixes the case here.

Have you tried wrapping the command in double quotes?

command: "systemctl status plexmediaserver.service | grep -oP '(?<=Active: ).\w+'"

I just tried, and this gives different errors:

2019-01-29 20:00:19 ERROR (SyncWorker_0) [homeassistant.util.yaml] while scanning a double-quoted scalar
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 13
found unknown escape character 'w'
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 82
2019-01-29 20:00:19 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning a double-quoted scalar
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 13
found unknown escape character 'w'
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 188, column 82

column 13 is the first " and 82 is the w of .\w+

I think you have to escape some characters in the searchstring, but i don’t know how.
What about the other way?

command: 'systemctl status plexmediaserver.service | grep -oP "(?<=Active: ).\w+"'

I can only guess, maybe there’s another way to extract the value,
Would you post the output from

systemctl status plexmediaserver.service

this is strange, when I try the other way there are no errors, but also no new sensors in HA (I have a 2nd entry for the appdaemon service too, I often forget to start appdaemon after an HA restart)

here is the first few lines of the output:

systemctl status plexmediaserver.service
● plexmediaserver.service - Plex Media Server for Linux
   Loaded: loaded (/lib/systemd/system/plexmediaserver.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-01-19 23:27:53 UTC; 1 weeks 2 days ago
 Main PID: 1054 (sh)
    Tasks: 122 (limit: 4915)
   CGroup: /system.slice/plexmediaserver.service

ha, it did work with the config:

 - platform: command_line
   name: Plex Service
   command: 'systemctl status plexmediaserver.service | grep -oP "(?<=Active: ).\w+"'
 - platform: command_line
   name: AppDaemon Service
   command: 'systemctl status [email protected] | grep -oP "(?<=Active: ).\w+"'

image

User error with the way the new entities are added in Lovelace to the Unused entities menu :slight_smile:

Thanks for your help @VDRainer

1 Like

Looked up the grep options that you use, --perl-regexp is something that i never understand. :upside_down_face:

I would have tried it with:
systemctl status plexmediaserver.service | grep Active | awk '{print $2}'

Happy automating!

1 Like

To be honest, I don’t really understand it, I modified a couple of examples found on the internet and managed to get the output I needed.
Your solution works well too, and I prefer your solution, I understand it :slight_smile:

Now time to add some HA automation to alert me for when I next forget to start appdaemon :slight_smile: