I’m wondering is it possible to trigger automation base on error message in log? For example, I would like the system to reboot itself when certain component breaks down.
Hi @masterkenobi, not tested, but i think you could use the Command line Binary Sensor as trigger.
binary_sensor:
- platform: command_line
command: if grep -i 'Fatal read error on socket transport' .homeassistant/home-assistant.log >/dev/null; then echo ON; else echo OFF; fi
Here’s what I did to control if mosquitto server (mqtt) is not running properly when home assistant is started:
Sensor:
### MQTT invalid config - mosquitto not running properly ###
- platform: template
sensors:
mqtt_offline:
value_template: >-
{% if '[mqtt]' in states.persistent_notification.invalid_config.state %}
Yes
{% else %}
No
{% endif %}
Automation:
### If Mosquitto config fails, notify and restart mosquitto and then HASS ###
- alias: 'Restart if Mosquitto not OK'
trigger:
platform: state
entity_id: 'sensor.mqtt_offline'
to: 'Yes'
action:
- delay:
seconds: 60
- service: notify.iOS
data:
message: 'Home Assistant will be restarted as MQTT was not running properly.'
title: 'Home Assistant issue'
- service: script.mosquitto_hass_fix
This might give you ideas for what you need.
What is in this script?
Script:
### Mosquitto + Home Assistant restart ###
mosquitto_hass_fix:
alias: "Mosquitto + Home Assistant restart"
sequence:
- service: shell_command.mosquitto_hass_fix
Shell command:
### Mosquitto + Home Assistant restart ###
mosquitto_hass_fix: "sudo service mosquitto restart && sudo systemctl restart [email protected]"
For user homeassistant to be able to run these commands, I needed to add the following file in /etc/sudoers.d
Cmnd_Alias MYSHELL = /usr/sbin/service, /bin/systemctl
homeassistant ALL=(ALL)NOPASSWD:MYSHELL
Hope this helps.
I used the code for the sensor:
{% if ‘[mqtt]’ in states.persistent_notification.invalid_config.state %}
Yes
{% else %}
No
{% endif %}
But it does not detect me correctly when it fails, any solution?
Screen:
Thanks!
Fix:
{% if ‘mqtt’ in states.persistent_notification.invalid_config.attributes.message %}
yes
{% else %}
NO
{% endif %}
Really old, but this may still be useful. In the 2024 version of HA Core, I use the following automation to restart an integration when a certain error pops up (search for the integration entity_id in the file .storage/core.config_entries
in the config directory:
- alias: "91004 - Samsung FamilyHub Integration error"
trigger:
platform: event
event_type: system_log_event
event_data:
level: ERROR
logger: custom_components.samsung_familyhub_fridge.api
condition:
condition: template
value_template: '{{ "Unexpected error fetching File ID refresher data:" in trigger.event.data.message[0] }}'
action:
service: homeassistant.reload_config_entry
data:
entry_id: ***