Hey guys, creating a title was already difficult task haha
Short version of question:
I have sensors with name: “medication_med1”, “medication_med2”, etc. Every sensor have attributes pill_name, time_1, time_2, time_3 and time_4.
Is it possible to search attributes named time_* under all sensors named sensor.medication_* and list as pill_name attribute if value is “08:00:00” for example ?
template:
sensor:
name: "medication_med1"
state: "{{ states('counter.medication_med1') | round(0) }}"
attributes:
pill_name: "Medication Med1"
time_1: "08:00:00"
usage_1: "{{ 1 }}"
note_1: >
{{states.attributes
| selectattr('entity_id', 'search', 'sensor.medication_')
| selectattr('attribute_name', 'search', 'time_')
| selectattr('attribute_value', 'eq' , 'state_attr('sensor.medication_med1', 'time_1')')
| map('sensor_name', ') | list}}
Long version of question:
I’m trying to improve medication reminder/tracker that is running as a package(s) in Home Assistant. I have one yaml file for each kind of medication. Each yaml contains different time schedule for reminders. There is room for improvement, for example, sometimes I have to take 2 different pills at the same time and 2 reminders at the same time doesn’t always work as I’d like. Take a look at example of one file:
counter:
medication_med1:
initial: 20
step: 1
template:
sensor:
name: "medication_med1"
state: "{{ states('counter.medication_med1') | round(0) }}"
attributes:
pill_name: "Medication Med1"
time_1: "08:00:00"
usage_1: "{{ 1 }}"
note_1: >
{{states.attributes
| selectattr('entity_id', 'search', 'sensor.medication_')
| selectattr('attribute_name', 'search', 'time_')
| selectattr('attribute_value', 'eq' , 'state_attr('sensor.medication_med1', 'time_1')')
| map('sensor_name', ') | list}}
time_2: "20:00:00"
usage_2: "{{ 3 }}"
note_2: >
{{states.attributes
| selectattr('entity_id', 'search', 'medication_')
| selectattr('attribute_name', 'search', 'time_')
| selectattr('attribute_value', 'eq' , 'state_attr('sensor.medication_med1', 'time_1')')
| map(attribute='pill_name')', ') | list}}
automation:
- alias: "alarm_msg_1"
trigger:
platform: time
at: "{{ state_attr('sensor.medication_med1', 'time_1') }}"
condition:
- condition: template
value_template: "{{ as_timestamp(state_attr('notify.mobile_app_moto_g42','last_triggered')) > 1 }}"
action:
- service: notify.mobile_app_moto_g42
data:
message: "Take {{ note_1 }}"
- alias: "alarm_msg_2"
trigger:
platform: time
at: "{{ state_attr('sensor.medication_med1', 'time_2') }}"
condition:
- condition: template
value_template: "{{ as_timestamp(state_attr('notify.mobile_app_moto_g42','last_triggered')) > 1 }}"
action:
- service: notify.mobile_app_moto_g42
data:
message: "Take {{ note_2 }}"
So attributes note_1 and note_2 is what I don’t know how to solve. I tried with selectattr(‘search’) and with for loop, but its not worth to post my attempts here and I’m not even sure is it even possible.
I’d like to have in note_1: Medication Med1, Medication Med4
I’m still quite new to this so all suggestions are welcome.
edit: made it more clear I hope