Hello there, I am trying to implement a basic events log to have a log of the last 8 events that I manually choose to display through with my automations and to show them in the Lovelace dashboard.
Until today I used an input_select
helper: every element in the list was an event like title|subtitle|date|color|icon
and then I used jinjia in Lovelace to extract every variable. A script help me write new events and deleting the old one, in the list. That worked but every Home Assistant restart, every element in the list was reset to their default one.
I then decided to shift to a JSON based approach. Using a file where I can write the events and retrieve it.
The reading works like that:
command_line:
- sensor:
command: 'cat /config/www/event_log.json'
name: 'Last events list'
icon: mdi:history
unique_id: logbook_json
scan_interval: 10
json_attributes:
- events
value_template: '"OK"'
But I cannot make the “writing” part work. For the writing, I am using a shell_command
action with jq
:
shell_command:
log_event: >-
jq --argjson new_event '{{ new_event }}' '.events = [$new_event] + .events[:7]' /config/www/event_log.json > /config/www/event_log.tmp && mv /config/www/event_log.tmp /config/www/event_log.json
If I execute it in the terminal, with an example JSON text, it works perfectly. It doesn’t work if I use it within my script:
fields:
title:
name: Title of the event
required: true
selector:
text:
multiline: false
type: text
subtitle:
name: Text (one line) of the event
required: false
selector:
text:
multiline: true
type: text
icon:
name: Icon to show
required: false
selector:
icon: null
color:
name: Color of the icon
required: false
selector:
text:
multiline: false
type: text
sequence:
- variables:
new_event:
title: "{{ title }}"
subtitle: "{{ subtitle }}"
date: "{{ now() }}"
icon: "{{ icon if icon is not none else 'mdi:information' }}"
color: "{{ color if color is not none else 'blue' }}"
- action: shell_command.log_event
data:
new_event: "{{ new_event }}"
mode: queued
icon: mdi:tooltip-plus-outline
max: 5
I think there is a mismatch between how the script passes the new_event
variable to the shell_command
and how jq
expects this JSON text… but I am not able to debug it.
Can you help me with that?