123
(Taras)
November 14, 2022, 7:36pm
8
You can use a Trigger-based Template Sensor to store data as a dictionary, list, boolean, etc.
Here are two examples:
A separate entity for each variable. The entity’s state is limited to storing a string (like all entities) but its attributes can store other data types.
Yes. I you can use a trigger template sensor like this:
template:
- trigger:
- platform: event
event_type: set_my_sensor
sensor:
- name: My sensor
state: "{{ trigger.event.data.state }}"
And then you can change it in a script or automation like this:
action:
- alias: Update my sensor
event: set_my_sensor
event_data:
state: new_value
Can even add attributes if you want, just pass more data in the event and use it in attributes in the sensor config. Plus trigger template …
A single entity for multiple variables. Each variable is stored as a separate attribute (therefore its type can be dictionary, list, etc). Variables can be defined dynamically.
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 …
1 Like