here you go:
Trigger based template sensor:
template:
- trigger:
- platform: event
event_type: set_variable
- platform: event
event_type: remove_variable
sensor:
- unique_id: 4a4c8e53-9e68-4198-9cc5-b336e228ea4d
name: Variables
state: Variables
attributes:
variables: >
{% set current = this.attributes.get('variables', {}) %}
{% if trigger.event.event_type == 'set_variable' %}
{% set new = {trigger.event.data.key: trigger.event.data.value} %}
{{ dict(current, **new) }}
{% elif trigger.event.event_type == 'remove_variable' %}
{{ dict(current.items() | rejectattr('0', 'eq', trigger.event.data.key)) }}
{% endif %}
To set a variable (reusing the key
will overwrite the previous value
)
action:
- alias: Set a variable
event: set_variable
event_data:
key: test
value: 26
To remove a variable from the sensor:
action:
- alias: Remove a variable
event: remove_variable
event_data:
key: test
My sensor.variables
after a few tests:
friendly_name: Variables
variables:
test_int: 10
test_float: 9.99
test_list:
- a
- list
test_dict:
foo: bar
whale: petunia