Thanks for this which I’ve implemented and it works a treat. I thought I could adapt it to keep track of who is using my HA instance. It works sometimes, but periodically fails, resetting the trigger entity to unavailable and losing the stored data. I can’t work out how to debug it and the log just contains enigmatic statements which don’t tell me which part of the YAML is failing.
I’ve created 2 sensors: the first successfully keeps track of the most recent user to log on or off:
- sensor:
- name: browser_users_last
state: >
{% set lastone = integration_entities('browser_mod')|select('contains','browser_user')|reject('contains','agent')|expand|sort(attribute='last_updated')|last %}
{{ lastone.name.split(' Browser')[-2] }}
attributes:
when: >
{% set lastone = integration_entities('browser_mod')|select('contains','browser_user')|reject('contains','agent')|expand|sort(attribute='last_updated')|last %}
{{ lastone.last_changed }}
who: >
{% set lastone = integration_entities('browser_mod')|select('contains','browser_user')|reject('contains','agent')|expand|sort(attribute='last_updated')|last %}
{{ lastone.state }}
prev: >
{% set lastone = integration_entities('browser_mod')|select('contains','browser_user')|reject('contains','agent')|expand|sort(attribute='last_updated')|last %}
{% set id = lastone.name.split(' Browser')[-2] %}
{% if id in state_attr('sensor.browser_users_database', 'database') %}
{{ state_attr('sensor.browser_users_database', 'database')[id].when }}
{% else %}
{{ now().isoformat }}
{% endif %}
Then comes the adapted version from the above discussion. It’s triggered by any change of the sensor.browser_users_last
defined above and the database should produce a dict of ids with the time of each id’s most recent change in status, previous change and the accumulated time (ignoring time not logged on).
- trigger:
- platform: state
entity_id: sensor.browser_users_last
sensor:
- name: browser_users_database
unique_id: browser_users_database
state: "testing"
attributes:
when: "{{ trigger.to_state.attributes.when }}"
id: "{{ trigger.to_state.state }}"
olddb: "{{ this.attributes.get('database', {}) }}"
last: >
{% if trigger.to_state.state in this.attributes.get('database', {}) %}
{{ this.attributes.database[trigger.to_state.state].when }}
{% endif %}
database: >
{% set id = trigger.to_state.state %}
{% set others = dict(this.attributes.get('database', {}).items() | rejectattr('0', 'eq', id)) %}
{% set when = trigger.to_state.attributes.when %}
{% set last = trigger.to_state.attributes.prev~'trig' %}
{% if id in this.attributes.get('database', {}) %}
{% set last = this.attributes.get('database')[trigger.to_state.state].when %}
{% endif %}
{% set for = this.attributes.get('database')[trigger.to_state.state].for | float(0) %}
{% if state_attr('sensor.browser_users_last','who') == 'unavailable'%}
{% set for = for + as_timestamp(when, as_timestamp(now())) - as_timestamp(last, as_timestamp(now())) %}
{% endif %}
{% set for = max( for, 0)| round(0) %}
{% set new = {id: {'prev': last, 'when': when, 'for': for }} %}
{{ dict(others, **new) }}
Wen it stops working it does not re-create itself like the variables one above does.
The log contains lines like:
Logger: homeassistant.helpers.sensor
Source: helpers/trigger_template_entity.py:204
First occurred: 15:07:05 (41 occurrences)
Last logged: 17:05:39
Error rendering state template for sensor.browser_users_database: UndefinedError: 'None' has no attribute 'DJI-P9_l-app'
so not telling me which bit went wrong. I haven’t been able to test things using developer tools because of the trigger.
and this.
parts of the YAML.
Any pointers on offer? Please?