Specific Integration Logger Automation

Hi,

I would like to set the logger to debug a specific integration on the fly. Is there any way to get something like this to work?

input_text:
 log_level_integration:
   name: Integration
   initial: doods

input_boolean:
  log_level_integration:
    name: Log Level Integration
    initial: off

automation:
  - alias: Log Level Integration
    trigger:
      platform: state
      entity_id: input_boolean.log_level_integration
      to: 'on'
    action:
      service: logger.set_level
      data_template:
        homeassistant.components."{{states('input_text.log_level_integration')}}": debug

automation:
  - alias: Log Level Integration
    trigger:
      platform: state
      entity_id: input_boolean.log_level_integration
    action:
      service: logger.set_level
      data_template: >
        {% if trigger.to_state.state == 'on' %}
          homeassistant.components.{{ states('input_text.log_level_integration') }}: debug
        {% else %}
          homeassistant.components.{{ states('input_text.log_level_integration') }}: info
        {% endif %}

hey @tom_l thanks for your help but it failed config check. any ideas?

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See ?, line ?).

However, i pasted it into YAMLLint and it was valid :thinking:

You can’t specify a YAML key (as in “key: value”) via a template. In this case, the key is the module for which you want to change the logging level. However you could do this via a python_script:

mod = hass.states.get("input_text.log_level_integration").state
is_on = hass.states.get("input_boolean.log_level_integration").state == "on"
level = "debug" if is_on else "info"
hass.services.call(
    "logger",
    "set_level",
    {mod, level}
)

Then call it like this (assuming you name the script set_log_level):

automation:
  - alias: Log Level Integration
    trigger:
      platform: state
      entity_id: input_boolean.log_level_integration
    action:
      service: python_script.set_log_level

Hey,

I finally got around to trying this but i get the following error - any ideas?

2020-06-18 22:25:48 ERROR (SyncWorker_13) [homeassistant.components.python_script.set_log_level.py] Error executing script: expected a dictionary
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 560, in validate_dict
    raise er.DictInvalid('expected a dictionary', path)
voluptuous.error.DictInvalid: expected a dictionary

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
    exec(compiled.code, restricted_globals)
  File "set_log_level.py", line 7, in <module>
  File "/usr/src/homeassistant/homeassistant/core.py", line 1182, in call
    self._hass.loop,
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/src/homeassistant/homeassistant/core.py", line 1221, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 276, in __call__
    raise er.MultipleInvalid([e])
voluptuous.error.MultipleInvalid: expected a dictionary

It’s a typo, there’s a comma where there should be a : which makes a set instead of a dictionary. Change this last bit:

to this

hass.services.call(
    "logger",
    "set_level",
    {mod: level}
)
1 Like

@pnbruckner

So i don’t get an error anymore but nothing happens either…are we sure this sort of thing is possible?

The service definitely works and calling services from python does so it should be. You might have to give a bit more info though. What exactly are you trying to do? Looks like you have initial set to doods, is that what you have the input text set to when you flip on debug logging? And what are you doing after that?

I’m looking at the code for doods and it looks like the only debugging logging it does is when processing an image just FYI.

Please post both the python_script and the code that calls it. Also describe how you’re causing it to run.

EDIT: I have marked this as the solution as it shows all the parts.

log_level_integration.yaml (Package)

automation:
  - alias: Log Level Integration
    trigger:
      platform: state
      entity_id: input_boolean.log_level_integration
    action:
      service: python_script.set_log_level

input_text:
  log_level_integration:
   name: Integration
   initial: blank

input_boolean:
  log_level_integration:
    name: Log Level Integration
    initial: off

set_log_level.py

mod = hass.states.get("input_text.log_level_integration").state
is_on = hass.states.get("input_boolean.log_level_integration").state == "on"
level = "debug" if is_on else "info"
hass.services.call(
    "logger",
    "set_level",
    {mod: level}
)

What are you setting input_text.log_level_integration to?

Yeah its working guys thanks! I was stupidly using the text box in like my initial concept. If I type in the proper component name it works.

Much appreciated!

1 Like