Write to log

It would be handy to have a service that would let you write to the log file. An automation could use the service to write a comment, or the current value of some entity, to the log.

Before the advent of integrated debuggers, writing comments and variable values to the log was a time-honored way to debug code. In python you can use logger.info for this purpose. However, there’s no native way to do it in an automation (that I know of).

In this thread, the author’s automation calls a script which, in turn, writes the desired value or comment to the log file.

That’s a neat workaround but must be used judiciously because you can’t call a script that’s already running.

The proposed feature would look something like this when used in an automation:

  action:
    - service: logwriter.info
      data_template:
         message: "Front door is {{ states('binary_sensor.front_door') }}"

Does this not work. Maybe not the log file but write to your own debugging file?

That’s close except the goal is write to syslog. However, your suggestion made me look more closely at the Notification component and its many platforms including: Syslog!

I haven’t had a chance to try it yet but it looks very promising.

The Syslog notification platform effectively fulfills my feature request. It allows me to write to the log (syslog).

In configuration.yaml:

notify:
  - name: loginfo
    platform: syslog

Example usage: Log an entity’s current state.

- alias: 'Test loginfo'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: light.kitchen
  action:
    - service: notify.loginfo
      data_template:
        message: "Kitchen light is {{states('light.kitchen')}}."

Output in syslog:

Jan 25 19:05:51 hass_server Home Assistant[685]: Kitchen light is on.
1 Like

I use this

service: logbook.log
data_template:
  name: Locking
  message: L************ {{ trigger.to_state.state }}******LOCKED_STATUS******* {{ trigger.to_state.state }}******LOCKED_STATUS*
1 Like

Thanks! I wasn’t aware of it.

I tried it and it works just fine.

logger-log

However, displaying the logbook (which is sourced from the SQLite DB) is uncomfortably slow (on my system) whereas displaying the contents of syslog is dramatically faster. Nevertheless, I appreciate your help.

You can use system_log.write for this as well. It is not documented for some reason, but you can see the arguments required here:

3 Likes

Thank you! I missed that one as well. :face_with_raised_eyebrow:

action:
  - service: system_log.write
    data_template:
      message: "The state of the switch is {{states('switch.something')}}"
      level: info

It also resolves a problem I encountered while using the syslog notification platform, namely it does not work when Home Assistant runs in a docker container.

I’ve concluded it has to do with the fact the syslog notification platform uses the syslog python library whereas most everything else, including the system_log component, use python’s logging library. For whatever reason, Home Assistant as a docker container doesn’t seem to support the syslog library.

3 Likes

I am running as a container also and I tried this system_log.write service but I see nothing. Is it just writing it to the standard log? Can it write to the container console?

it just plain does not work

https://hastebin.com/umemomizib.bash

2020-01-26 14:21:30 INFO (MainThread) [homeassistant.components.automation] Executing html notification test
2020-01-26 14:22:06 INFO (MainThread) [homeassistant.components.automation] Executing html notification test
2020-01-26 14:22:08 INFO (MainThread) [homeassistant.components.automation] Executing html notification test
2020-01-26 14:22:09 INFO (MainThread) [homeassistant.components.automation] Executing html notification test
2020-01-26 14:22:11 INFO (MainThread) [homeassistant.components.automation] Executing html notification test

is all I see in the logs, any thoughts?

Change level from info to warning.

Cool! That was it! Thank you

Or update the configuration.yaml file to include the following. This is because by default only warning is logged.

# https://www.home-assistant.io/integrations/logger/
logger:
  default: info

How do I access the syslog? I am running on a raspberry pi and via the web interface I have several logs to choose from but its not clear to me if any are the actual syslog