Logging to logbook - Using a template

Any help is appreciated!

I’m wanting to log (most for some debugging) what states are when certain actions happen, so after an action … Say a light is powered off I want to log the states of certain things to make sure that what I’m wanting is happening. I am testing this out in a very simple scenario and not getting what I need - See below. The “message” line has the template that I want, but it will not start with that line. Anyone know how I can use a template in a logbook entry?

- alias: "Testing logging"
  trigger:
    platform: time
    hours: 16
    minutes: 03
    seconds: 00
  action:
    event: LOGBOOK_ENTRY
    event_data:
      name: "Upstairs Temp: "
      message: {{states.sensor.main_floor_temperature.state}}

(Removing the template and using dummy words in the “message” works btw. So I can only assume it is my template causing the startup problem.)

Using the developer tools and logbook.log with the following payload works, so I assume it is possible and I’m not constructing my action properly!
{"name":"Upstairs Temp ","message":"{{states.sensor.main_floor_temperature.state}}"}

1 Like

This way of constructing the action also works… without using an actual template:

- alias: "Testing logging"
  trigger:
    platform: time
    hours: 16
    minutes: 20
    seconds: 00
  action:
    service: logbook.log
    data:
      name: "Upstairs Temp: "
      message: "Not using a template"

Not sure how to get this template in there!

Spent most of last night trying to figure this out and finally gave up and I came across a page that had the answer… One that I had missed several times! Here is the working way!

- alias: "Testing logging"
  trigger:
    platform: time
    hours: 16
    minutes: 34
    seconds: 00
  action:
    service: logbook.log
    data:
      name: "Upstairs Temp: "
      message: >
        {{states.sensor.main_floor_temperature.state}}

A couple of important things:

  1. message: >
  2. the indentation of: {{states.sensor.main_floor_temperature.state}}

Hope this helps someone down the road with at least one night less of banging their head.

18 Likes

I’m trying to do a similar thing with weight. But even if i copy and paste your code it doesn’t work. Is yours still working all this time later?

Screen Shot 2020-12-05 at 8.33.02 AM

Your code is missing the data section and also does not have a message to log. The following is working for me. It’s a script but the service call syntax is the same as for an automation…

alias: HVAC Log - Cooling
sequence:
  - service: logbook.log
    data:
      name: Nest Thermostat
      message: >
        cooling {{ state_attr('climate.living_room','current_temperature') }}
      entity_id: climate.living_room
mode: single

Reference: Logbook - Home Assistant

1 Like

You should also be able to put the line in a single quote, like this:

      message: '{{states.sensor.main_floor_temperature.state}}'
3 Likes