Want to log which alarm sensor is being triggered

I have a number of alarm sensors, and want to keep a log of when each is being triggered. Is there any way of extending the automation below to write an entry to the log containing the name/id of the specific trigger when any of them is triggered?

alias: Sensor triggered - log only
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.pir_hall
    to: 'on'
  - platform: state
    entity_id: binary_sensor.sonoff_pir_kitchen
    to: 'on'
  - platform: state
    entity_id: binary_sensor.pir_study
    to: 'on'
  - platform: state
    entity_id: binary_sensor.pir_lounge
    to: 'on'
  - platform: state
    entity_id: binary_sensor.pir_garage
    to: 'on'
  - platform: state
    entity_id: binary_sensor.front_door
    to: 'on'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 1E667A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 1E667A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 0A720A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 0A720A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/zigbee-bridge/door/SENSOR
    payload: '1'
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 085B0A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 085B0A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 412B0A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 412B0A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 08420A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 08420A
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge/RESULT
    payload: 08420E
    value_template: '{{ value_json.RfReceived.Data}}'
  - platform: mqtt
    topic: tele/rf-bridge2/RESULT
    payload: 08420E
    value_template: '{{ value_json.RfReceived.Data}}'

I use the notification integration to write data to a file.

here is an example when I was trying to determine if my propane cyclinder scale was linearly temperature sensitive:

first create the notification integration (note you have to manually create the empty text file first):

notify:
  - platform: file
    name: Propane Data Record
    filename: propane_data.txt
    timestamp: true

then the automation to write the data:

automation:
  - alias: Smoker Propane Data Record
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.dark_sky_temperature
    action:
      - service: notify.propane_data_record
        data:
          message: >
            temp: {{ states('sensor.dark_sky_temperature' ) }}, 
            smoker raw: {{ states('sensor.smoker_propane_raw_weight') }},
            smoker deviation: {{ states('sensor.smoker_propane_deviation') }},
            smoker deviation percent:  {{ states('sensor.smoker_propane_percent_deviation') }}

you’ll need to come up with the template for the data to record but you’ll start with using the trigger data of the entity that triggered the automation (trigger.entity_id or trigger.id if you actually created an id for your triggers).

Thanks for the tip. So I now have this defined in configuation.yaml:

  - platform: file
    name: sensorlog
    filename: sensorlog.txt
    timestamp: true

…and this in my action:

action:
  - service: notify.sensorlog
    data:
      message: Sensor triggered {{ trigger.entity_id ) }}

But I get this error:

template value should be a string for dictionary value @ data['action'][0]['data']

???

you have an extra " ) " in there…

Doh - thanks!

1 Like

I have another question - is there any way to use the notify.file method to perform housekeeping on the log file, so that it doesn’t get too big? e.g. automatically clearing it at the end of each week/month?

as far as I see there is nothing built into the notifier.

you might be able to do something using a shell command to delete (or rename) and recreate the file periodically.