1033 lines
41 KiB
YAML
1033 lines
41 KiB
YAML
- alias: 'Battery Alert: Notification'
|
|
description: This is a combined battery Alert Notification automation. Based on
|
|
the value of input_select.notifier_format the notification is delivered using
|
|
the notify integration or as a persistent notification. Notifications using the
|
|
Default notification are automatically dismissed after 15 minutes. Persistent
|
|
notification will be cleared every 15th minute if the sensor alerts are cleared.
|
|
triggers:
|
|
- at: input_datetime.low_battery_notification_check
|
|
trigger: time
|
|
id: time
|
|
- entity_id:
|
|
- input_number.battery_alert_threshold_min
|
|
- input_number.battery_alert_threshold_max
|
|
trigger: state
|
|
id: threshold_changed
|
|
- minutes: /15
|
|
seconds: 0
|
|
trigger: time_pattern
|
|
id: every_15_minutes
|
|
- trigger: state
|
|
entity_id:
|
|
- input_select.notifier_format
|
|
id: notifier_format_changed
|
|
- trigger: state
|
|
entity_id:
|
|
- input_boolean.low_batteries
|
|
enabled: true
|
|
id: low_batteries_boolean_changed
|
|
conditions:
|
|
- condition: or
|
|
conditions:
|
|
- alias: If Notifier Format is Persistent then trigger must either be theshold
|
|
or 15 min trigger or notifier format changed
|
|
condition: and
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_select.notifier_format
|
|
state: Persistent
|
|
- condition: trigger
|
|
id:
|
|
- every_15_minutes
|
|
- threshold_changed
|
|
- notifier_format_changed
|
|
- condition: and
|
|
conditions:
|
|
- condition: not
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_select.notifier_format
|
|
state: Persistent
|
|
- condition: not
|
|
conditions:
|
|
- condition: trigger
|
|
id:
|
|
- every_15_minutes
|
|
alias: If Notifier Format is Default or Slack do not allow 15 min triggering
|
|
- alias: If Low Batteries is on and trigger is every 15 minutes
|
|
condition: and
|
|
conditions:
|
|
- condition: trigger
|
|
id:
|
|
- every_15_minutes
|
|
- condition: state
|
|
entity_id: input_boolean.low_batteries
|
|
state: 'on'
|
|
enabled: true
|
|
- condition: and
|
|
conditions:
|
|
- condition: trigger
|
|
id:
|
|
- low_batteries_boolean_changed
|
|
- condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ trigger.to_state.context.parent_id == none }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.to_state.context.user_id != none }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.to_state.context.id != none }}'
|
|
alias: If the state change was likely caused by a UI interaction
|
|
alias: If Low Batteries boolean was changed from the UI
|
|
enabled: true
|
|
actions:
|
|
- variables:
|
|
notification_tag: low_battery_alert
|
|
low_batteries: "{% macro battery_level() %} {% for entity_id in states.group.battery_status.attributes.entity_id\
|
|
\ if (\n not (\n is_state_attr(entity_id, 'battery_alert_disabled',\
|
|
\ true)\n or is_state_attr(entity_id, 'restored', true)\n )\n\
|
|
\ and states(entity_id) is not none\n and (\n (\n (\n states(entity_id)\
|
|
\ is number\n or states(entity_id) | length == states(entity_id)| int(0)\
|
|
\ | string | length\n or states(entity_id) | length == states(entity_id)|\
|
|
\ float(0) | string | length\n )\n and states(entity_id) | int(0)\
|
|
\ <= states('input_number.battery_alert_threshold_max') | int(0)\n and\
|
|
\ states(entity_id) | int(0) >= states('input_number.battery_alert_threshold_min')\
|
|
\ | int(0)\n )\n or states(entity_id) | lower == 'low'\n or (states('input_number.battery_alert_threshold_min')\
|
|
\ | int(0) == -1 \n and (states(entity_id) | lower == 'unknown'\n \
|
|
\ or states(entity_id) | lower == 'unavailable'))\n )\n ) -%}\n{{\
|
|
\ state_attr(entity_id, \"friendly_name\") }} ({{ states(entity_id) }}) {%\
|
|
\ endfor -%} {% endmacro %} {{ battery_level() | trim != \"\" }}\n"
|
|
notifier: '{{ iif(''notify.'' in states(''input_text.notifier_name''), states(''input_text.notifier_name''),
|
|
''notify.notify'') }}'
|
|
enabled: true
|
|
- alias: Conditionally clear notifications before progressing
|
|
if:
|
|
- condition: or
|
|
conditions:
|
|
- condition: and
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_boolean.low_batteries
|
|
state: 'on'
|
|
- condition: template
|
|
value_template: '{{ not low_batteries}}'
|
|
alias: No batteries are currently within threshold
|
|
alias: Low battery bool do not match current low batteries variable
|
|
- condition: trigger
|
|
id:
|
|
- notifier_format_changed
|
|
- threshold_changed
|
|
then:
|
|
- alias: Clear Persistent or Default notifications
|
|
choose:
|
|
- conditions:
|
|
- condition: or
|
|
conditions:
|
|
- alias: Trigger is NOT notifier_format_changed and Notifier Format is Default
|
|
condition: template
|
|
value_template: '{{ trigger.id != "notifier_format_changed" and is_state(''input_select.notifier_format'',''Default'')
|
|
}}'
|
|
- alias: Trigger is notifier_format_changed and from state was Default
|
|
condition: template
|
|
value_template: '{{ trigger.id == "notifier_format_changed" and trigger.from_state.state
|
|
== "Default" }}'
|
|
alias: Test if Default notifications should be cleared
|
|
sequence:
|
|
- alias: Clear Notification
|
|
action: '{{ notifier }}'
|
|
data:
|
|
message: clear_notification
|
|
data:
|
|
tag: '{{ notification_tag }}'
|
|
- conditions:
|
|
- condition: or
|
|
conditions:
|
|
- alias: Trigger is NOT notifier_format_changed and Notifier Format is Persistent
|
|
condition: template
|
|
value_template: '{{ trigger.id != "notifier_format_changed" and is_state(''input_select.notifier_format'',''Persistent'')
|
|
}}'
|
|
- alias: Trigger is notifier_format_changed and from state is Persistent
|
|
condition: template
|
|
value_template: '{{ trigger.id == "notifier_format_changed" and trigger.from_state.state
|
|
== "Persistent" }}'
|
|
alias: Test if Persistent notifications should be cleared
|
|
sequence:
|
|
- action: persistent_notification.dismiss
|
|
metadata: {}
|
|
data:
|
|
notification_id: '{{ notification_tag }}'
|
|
- alias: Send message if there are batteries within threshold
|
|
if:
|
|
- alias: Test if Low Batteries is true (meaning that one or more battery sensors
|
|
have values in the threshold interval )
|
|
condition: template
|
|
value_template: '{{ low_batteries }}
|
|
|
|
'
|
|
then:
|
|
- variables:
|
|
message_text: "{% macro battery_level() %} {% for entity_id in \nstates.group.battery_status.attributes.entity_id\
|
|
\ if (\n not (\n is_state_attr(entity_id, 'battery_alert_disabled',\
|
|
\ true)\n or is_state_attr(entity_id, 'restored', true)\n )\n\
|
|
\ and states(entity_id) is not none\n and (\n (\n (\n states(entity_id)\
|
|
\ is number\n or states(entity_id) | length == states(entity_id)|\
|
|
\ int(0) | string | length\n or states(entity_id) | length == states(entity_id)|\
|
|
\ float(0) | string | length\n )\n and states(entity_id) | int(0)\
|
|
\ <= states('input_number.battery_alert_threshold_max') | int(0)\n \
|
|
\ and states(entity_id) | int(0) >= states('input_number.battery_alert_threshold_min')\
|
|
\ | int(0)\n )\n or states(entity_id) | lower == 'low'\n or (states('input_number.battery_alert_threshold_min')\
|
|
\ | int(0) == -1 \n and (states(entity_id) | lower == 'unknown'\n\
|
|
\ or states(entity_id) | lower == 'unavailable'))\n )\n) -%}\n \
|
|
\ {{ state_attr(entity_id, \"friendly_name\") }} ({{ states(entity_id) }})\n\
|
|
{% endfor -%} {% endmacro %} {{ battery_level() }}\n"
|
|
delay_timer: 900
|
|
- choose:
|
|
- conditions:
|
|
- condition: state
|
|
entity_id: input_select.notifier_format
|
|
state: Default
|
|
- condition: trigger
|
|
id:
|
|
- threshold_changed
|
|
- time
|
|
- notifier_format_changed
|
|
sequence:
|
|
- action: '{{ notifier }}'
|
|
data:
|
|
title: Low Battery Levels
|
|
message: '{{ message_text }}'
|
|
data:
|
|
group: battery
|
|
tag: '{{ notification_tag }}'
|
|
- alias: Wait for time defined in variable delay_timer
|
|
delay: '{{ delay_timer }}'
|
|
- alias: Clear Notification
|
|
action: '{{ notifier }}'
|
|
data:
|
|
message: clear_notification
|
|
data:
|
|
tag: '{{ notification_tag }}'
|
|
- conditions:
|
|
- condition: state
|
|
entity_id: input_select.notifier_format
|
|
state: Slack
|
|
- condition: trigger
|
|
id:
|
|
- threshold_changed
|
|
- time
|
|
- notifier_format_changed
|
|
sequence:
|
|
- action: '{{ notifier }}'
|
|
data:
|
|
message: Low Battery Levels
|
|
data:
|
|
attachments:
|
|
- color: '#52c0f2'
|
|
title: These devices have low battery levels
|
|
text: '{{ message_text }}'
|
|
- conditions:
|
|
- condition: state
|
|
entity_id: input_select.notifier_format
|
|
state: Persistent
|
|
- condition: trigger
|
|
id:
|
|
- every_15_minutes
|
|
- threshold_changed
|
|
- notifier_format_changed
|
|
sequence:
|
|
- action: persistent_notification.create
|
|
metadata: {}
|
|
data:
|
|
title: Low Battery Levels
|
|
notification_id: '{{ notification_tag }}'
|
|
message: '{{ message_text }}'
|
|
- alias: Set Low Battery boolean to the correct state
|
|
if:
|
|
- alias: Test if Low Batteries is true (meaning that one or more battery sensors
|
|
have values in the threshold interval )
|
|
condition: template
|
|
value_template: '{{ low_batteries }}
|
|
|
|
'
|
|
then:
|
|
- if:
|
|
- condition: state
|
|
entity_id: input_boolean.low_batteries
|
|
state: 'off'
|
|
then:
|
|
- action: input_boolean.turn_on
|
|
metadata: {}
|
|
data: {}
|
|
target:
|
|
entity_id: input_boolean.low_batteries
|
|
alias: If Low Batteries bool is off turn it on
|
|
else:
|
|
- alias: If Low Batteries bool is on turn it off
|
|
if:
|
|
- condition: state
|
|
entity_id: input_boolean.low_batteries
|
|
state: 'on'
|
|
then:
|
|
- action: input_boolean.turn_off
|
|
metadata: {}
|
|
data: {}
|
|
target:
|
|
entity_id: input_boolean.low_batteries
|
|
mode: restart
|
|
initial_state: true
|
|
- alias: 'Battery Alert: Create Sensor From Attributes'
|
|
description: This automation creates battery sensors based on other sensor who has
|
|
battery data in their attributes. Sensors can be excluded from this automation
|
|
by defining a 'battery_sensor_creation_disabled' attribute in the customize configuration.
|
|
trigger:
|
|
- platform: event
|
|
event_type: state_changed
|
|
condition:
|
|
- condition: template
|
|
value_template: '{{ trigger is not none }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data is not none }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.old_state is not none }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state is not none }}'
|
|
- condition: template
|
|
value_template: '{{ ''battery'' not in trigger.event.data.new_state.name | lower
|
|
}}'
|
|
- condition: template
|
|
value_template: '{{ not is_state_attr(trigger.event.data.entity_id, ''device_class'',
|
|
''battery'') }}'
|
|
- condition: template
|
|
value_template: '{{ not is_state_attr(trigger.event.data.entity_id, ''battery_sensor_creation_disabled'',
|
|
true) }}'
|
|
- condition: template
|
|
value_template: '{{ not is_state_attr(trigger.event.data.entity_id, ''restored'',
|
|
true) }}'
|
|
- condition: or
|
|
conditions:
|
|
- condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery_level
|
|
is defined }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery_level
|
|
is not none }}'
|
|
- condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery is defined
|
|
}}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery is not
|
|
none }}'
|
|
- condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery_critical
|
|
is defined }}'
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.new_state.attributes.battery_critical
|
|
is not none }}'
|
|
actions:
|
|
- action: mqtt.publish
|
|
data_template:
|
|
topic: homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/config
|
|
retain: true
|
|
payload: "{\n {% if trigger.event.data.new_state.attributes.battery_level is\
|
|
\ defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_level\
|
|
\ -%}\n {%- set attribname = 'battery_level' -%}\n {%- elif trigger.event.data.new_state.attributes.battery\
|
|
\ is defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery\
|
|
\ -%}\n {%- set attribname = 'battery' -%}\n {% elif trigger.event.data.new_state.attributes.battery_critical\
|
|
\ is defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_critical\
|
|
\ -%}\n {%- set attribname = 'battery_critical' -%}\n {%- endif -%}\n\
|
|
\ \"name\": \"{{ trigger.event.data.new_state.name }} Battery\",\n \"state_topic\"\
|
|
: \"homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/state\"\
|
|
,\n {% if trigger.event.data.new_state.attributes.battery_template is defined\
|
|
\ -%}\n \"value_template\": \"{{ trigger.event.data.new_state.attributes.battery_template\
|
|
\ }}\",\n \"unit_of_measurement\": \"%\",\n \"device_class\": \"battery\"\
|
|
,\n {% elif trigger.event.data.new_state.attributes.battery_template_string\
|
|
\ is defined -%}\n \"value_template\": \"{{ trigger.event.data.new_state.attributes.battery_template_string\
|
|
\ }}\",\n \"icon\": \"mdi:battery\",\n {% elif trigger.event.data.new_state.attributes.battery_critical\
|
|
\ is defined -%}\n \"value_template\": \"{{ \"{{\" }} 'low' if value_json.value\
|
|
\ else 'full' {{ \"}}\" }}\",\n \"icon\": \"mdi:battery\",\n {% else -%}\n\
|
|
\ \"value_template\": \"{{ \"{{\" }} value_json.value | int(0) {{ \"}}\"\
|
|
\ }}\",\n {% if attribval | int(0) == attribval or attribval | float(0)\
|
|
\ == attribval or attribval | length == attribval | float(0) | string | length\
|
|
\ or attribval | length == attribval | int(0) | string | length -%}\n \"\
|
|
unit_of_measurement\": \"%\",\n \"device_class\": \"battery\",\n {%- elif\
|
|
\ attribval == \"High\" or attribval == \"Full\" -%}\n \"icon\": \"mdi:battery\"\
|
|
,\n {%- elif attribval == \"Medium\" or attribval == \"Med\"-%}\n \"icon\"\
|
|
: \"mdi:battery-50\",\n {%- elif attribval == \"Low\" -%}\n \"icon\": \"\
|
|
mdi:battery-alert\",\n {%- else -%}\n \"icon\": \"mdi:battery-unknown\"\
|
|
,\n {%- endif %}\n {% endif -%}\n \"unique_id\": \"{{ trigger.event.data.entity_id.split('.')[1]\
|
|
\ }}_battery\",\n \"json_attributes_topic\": \"homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1]\
|
|
\ }}_battery/attributes\"\n}"
|
|
- action: mqtt.publish
|
|
data_template:
|
|
topic: homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/state
|
|
retain: true
|
|
payload: "{\n {% if trigger.event.data.new_state.attributes.battery_level is\
|
|
\ defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_level\
|
|
\ -%}\n {%- elif trigger.event.data.new_state.attributes.battery is defined\
|
|
\ -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery\
|
|
\ -%}\n {%- elif trigger.event.data.new_state.attributes.battery_critical\
|
|
\ is defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_critical\
|
|
\ -%}\n {%- endif -%}\n \"value\": {% if attribval | int(0) == attribval\
|
|
\ -%}\n {{ attribval | int(0) }}\n {%- elif attribval | float(0) == attribval\
|
|
\ -%}\n {{ attribval | float(0) }}\n {%- elif attribval | length == attribval\
|
|
\ | float(0) | string | length -%}\n {{ attribval | float(0) }}\n {%-\
|
|
\ elif attribval | length == attribval | int(0) | string | length -%}\n \
|
|
\ {{ attribval | int(0) }}\n {%- else -%}\n \"{{ attribval }}\"\n \
|
|
\ {%- endif %}\n}"
|
|
- action: mqtt.publish
|
|
data_template:
|
|
topic: homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/attributes
|
|
retain: true
|
|
payload: "{\n {% if trigger.event.data.new_state.attributes.battery_level is\
|
|
\ defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_level\
|
|
\ -%}\n {%- set attribname = 'battery_level' -%}\n {%- elif trigger.event.data.new_state.attributes.battery\
|
|
\ is defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery\
|
|
\ -%}\n {%- set attribname = 'battery' -%}\n {%- elif trigger.event.data.new_state.attributes.battery_critical\
|
|
\ is defined -%}\n {%- set attribval = trigger.event.data.new_state.attributes.battery_critical\
|
|
\ -%}\n {%- set attribname = 'battery_critical' -%}\n {%- endif -%}\n\
|
|
\ \"entity_id\": \"{{ trigger.event.data.entity_id }}\",\n {% if attribname\
|
|
\ is defined -%}\n \"{{ attribname }}\": \"{{ attribval }}\",\n {%- endif\
|
|
\ %}\n \"delete_battery_sensor\": \"homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1]\
|
|
\ }}_battery\"\n}"
|
|
- alias: 'Battery Alert: Update Status Group Members'
|
|
description: Updates the group.battery_status each minute with all known battery
|
|
sensors
|
|
initial_state: true
|
|
triggers:
|
|
- trigger: time_pattern
|
|
minutes: /1
|
|
- trigger: state
|
|
entity_id:
|
|
- automation.battery_alert_update_status_group_members
|
|
to: 'on'
|
|
for:
|
|
seconds: 1
|
|
actions:
|
|
- action: group.set
|
|
data_template:
|
|
object_id: battery_status
|
|
entities: "{% for item in states.sensor if (\n not is_state_attr(item.entity_id,\
|
|
\ 'hidden', true)\n and (\n is_state_attr(item.entity_id, 'device_class',\
|
|
\ 'battery')\n or (item.entity_id | lower).endswith('_battery')\n or\
|
|
\ (item.name | lower).endswith('_battery')\n ) \n )\n-%}\n {{ item.entity_id\
|
|
\ }}{% if not loop.last %}, {% endif %}\n{%- endfor -%}"
|
|
- alias: 'Battery Alert: Delete a Sensor'
|
|
description: 'Enables the deletion of battery sensors created using MQTT in the
|
|
''Battery Alert: Create Sensor From Attributes'' automation. enter the name of
|
|
the sensor in input_text.delete_battery_sensor in either MQTT format (homeassistant/sensor/foo)
|
|
or in normal homeassistant notation (sensor.foo).'
|
|
triggers:
|
|
- entity_id:
|
|
- input_text.delete_battery_sensor
|
|
trigger: state
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ ''homeassistant/sensor/'' in states(''input_text.delete_battery_sensor'')
|
|
or ''sensor.'' in states(''input_text.delete_battery_sensor'')[0:7] }}'
|
|
alias: 'If mqtt format or default homeassistant notation is used '
|
|
actions:
|
|
- variables:
|
|
sensor_to_delete: '{% set sensor_name = states(''input_text.delete_battery_sensor'')
|
|
%}
|
|
|
|
{{ iif(''sensor.'' in sensor_name[0:7] , ''homeassistant/''+sensor_name.replace(''.'',''/''),sensor_name)
|
|
}}
|
|
|
|
'
|
|
alias: Define vaiable to hold mqtt sensor name
|
|
- data_template:
|
|
topic: '{{ sensor_to_delete }}'
|
|
retain: true
|
|
action: mqtt.publish
|
|
- data_template:
|
|
topic: '{{ sensor_to_delete }}/attributes'
|
|
retain: true
|
|
action: mqtt.publish
|
|
- data_template:
|
|
topic: '{{ sensor_to_delete }}/state'
|
|
retain: true
|
|
action: mqtt.publish
|
|
- data_template:
|
|
topic: '{{ sensor_to_delete }}/config'
|
|
retain: true
|
|
action: mqtt.publish
|
|
- action: input_text.set_value
|
|
metadata: {}
|
|
data:
|
|
value: '# MQTT delete message sent #'
|
|
target:
|
|
entity_id: input_text.delete_battery_sensor
|
|
- delay:
|
|
hours: 0
|
|
minutes: 0
|
|
seconds: 2
|
|
milliseconds: 0
|
|
- action: input_text.set_value
|
|
metadata: {}
|
|
data:
|
|
value: ''
|
|
target:
|
|
entity_id: input_text.delete_battery_sensor
|
|
- alias: 'Livetrack: Initialize Values at Homeassistant Start'
|
|
id: livetrack_initialize
|
|
description: Will run at HA start. Only need to run once. Can be disabled afterwards
|
|
trigger:
|
|
- trigger: homeassistant
|
|
event: start
|
|
conditions:
|
|
- condition: or
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_text.livetrack_notifier_name
|
|
state: ''
|
|
- condition: state
|
|
entity_id: input_text.livetrack_local_json_path
|
|
state: ''
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: state
|
|
entity_id: input_text.livetrack_local_json_path
|
|
state: ''
|
|
sequence:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: https://homeassistant.local:8123/local/livetrack/empty_garmin.json
|
|
target:
|
|
entity_id: input_text.livetrack_local_json_path
|
|
- conditions:
|
|
- condition: state
|
|
entity_id: input_text.livetrack_notifier_name
|
|
state: ''
|
|
sequence:
|
|
- stop: Stopping automation. No further values set.
|
|
- action: input_text.set_value
|
|
data:
|
|
value: notify.notify
|
|
target:
|
|
entity_id: input_text.livetrack_notifier_name
|
|
- action: shell_command.create_empty_garmin_json_file
|
|
data: {}
|
|
- alias: 'Hue motion sensor: Multihub fetch data when input selects change'
|
|
description: Update Value when input select changes
|
|
triggers:
|
|
- trigger: state
|
|
entity_id:
|
|
- input_select.hue_hubs
|
|
id: hub_change
|
|
- trigger: template
|
|
value_template: '{{ is_state(''sensor.hue_motion_sensor_hub_data'',''unknown'')
|
|
}}'
|
|
alias: When hub identifier number is unknown
|
|
id: hub_identifier_unknown
|
|
- trigger: state
|
|
entity_id:
|
|
- input_select.hue_hubs
|
|
from: Undefined
|
|
id: after_start
|
|
condition: []
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- hub_change
|
|
sequence:
|
|
- choose:
|
|
- conditions:
|
|
- condition: template
|
|
value_template: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[0].name
|
|
== states(''input_select.hue_hubs'') }}'
|
|
sequence:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[0].ip
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_ip
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[0].user
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_username
|
|
- conditions:
|
|
- condition: template
|
|
value_template: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[1].name
|
|
== states(''input_select.hue_hubs'') }}'
|
|
sequence:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[1].ip
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_ip
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[1].user
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_username
|
|
alias: Update Hub IP and username
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- hub_identifier_unknown
|
|
- after_start
|
|
sequence:
|
|
- repeat:
|
|
count: '{{ (state_attr(''input_select.hue_hubs'',''options'') | length)-1
|
|
}}'
|
|
sequence:
|
|
- action: input_select.select_next
|
|
data:
|
|
cycle: true
|
|
target:
|
|
entity_id: input_select.hue_hubs
|
|
- choose:
|
|
- conditions:
|
|
- condition: template
|
|
value_template: '{{ (states(''input_text.hue_hubs'',''options'') |
|
|
from_json)[0].name == states(''input_select.hue_hubs'') }}'
|
|
sequence:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[0].ip
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_ip
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[0].user
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_username
|
|
- conditions:
|
|
- condition: template
|
|
value_template: '{{ (states(''input_text.hue_hubs'',''options'') |
|
|
from_json)[1].name == states(''input_select.hue_hubs'') }}'
|
|
sequence:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[1].ip
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_ip
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ (states(''input_text.hue_hubs'',''options'') | from_json)[1].user
|
|
}}'
|
|
target:
|
|
entity_id: input_text.hue_hub_username
|
|
alias: Update Hub IP and username
|
|
enabled: true
|
|
- action: homeassistant.update_entity
|
|
data: {}
|
|
target:
|
|
entity_id: sensor.hue_motion_sensor_hub_data
|
|
alias: Get hub data for the selected sensor
|
|
enabled: true
|
|
- wait_template: '{{ not is_state(''sensor.hue_motion_sensor_hub_data'',''unknown'')
|
|
}}'
|
|
continue_on_timeout: true
|
|
alias: Wait for sensor sensitivity to change or on timeout
|
|
timeout: 00:00:05
|
|
enabled: true
|
|
- if:
|
|
- condition: template
|
|
value_template: '{{ state_attr(''input_select.hue_motion_sensitivity'',''options'')[int(state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivity''),0)]
|
|
!= states(''input_select.hue_motion_sensitivity'') }}'
|
|
alias: Selected sensitivity in UI differs from sensor sensitivity
|
|
then:
|
|
- action: input_select.select_option
|
|
data:
|
|
option: '{{ state_attr(''input_select.hue_motion_sensitivity'',''options'')[int(state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivity''),0)]
|
|
}}'
|
|
target:
|
|
entity_id: input_select.hue_motion_sensitivity
|
|
alias: Set sensitivity input select to actual configured value
|
|
- stop: Hub data found on other hub
|
|
mode: parallel
|
|
max: 10
|
|
- alias: 'Hue motion sensor: Fill input selects with data'
|
|
description: Based on the attribute sensitivitymax, the sensitivity selector has
|
|
3 or 5 levels to choose from
|
|
triggers:
|
|
- trigger: template
|
|
value_template: '{{ state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivitymax'')==2
|
|
}}'
|
|
id: sensitivity_basic
|
|
alias: When sensitivity of sensor is basic (three levels)
|
|
- trigger: template
|
|
value_template: '{{ state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivitymax'')==4
|
|
}}'
|
|
id: sensitivity_advanced
|
|
alias: When sensitivity of sensor is advanced (five levels)
|
|
- trigger: homeassistant
|
|
event: start
|
|
id: start
|
|
- trigger: event
|
|
id: reload
|
|
event_type: event_template_reloaded
|
|
condition: []
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: trigger
|
|
id: sensitivity_basic
|
|
sequence:
|
|
- action: input_select.set_options
|
|
data:
|
|
options:
|
|
- Low
|
|
- Medium
|
|
- High
|
|
target:
|
|
entity_id: input_select.hue_motion_sensitivity
|
|
- conditions:
|
|
- condition: trigger
|
|
id: sensitivity_advanced
|
|
sequence:
|
|
- action: input_select.set_options
|
|
data:
|
|
options:
|
|
- Very low
|
|
- Low
|
|
- Medium
|
|
- High
|
|
- Very high
|
|
target:
|
|
entity_id: input_select.hue_motion_sensitivity
|
|
- conditions:
|
|
- condition: trigger
|
|
id: start
|
|
sequence:
|
|
- action: input_select.set_options
|
|
data:
|
|
options: '{{ states.binary_sensor | selectattr(''attributes.device_class'',
|
|
''defined'') | selectattr(''attributes.device_class'',''eq'',''motion'')
|
|
| selectattr(''state'',''ne'',''unavailable'') | rejectattr(''attributes.entity_id'',
|
|
''defined'') | rejectattr(''attributes.type'', ''defined'') | map(attribute=''entity_id'')
|
|
| list }}'
|
|
target:
|
|
entity_id: input_select.motion_sensor_entities
|
|
alias: Use Service to add all Hue motion sensors to input select
|
|
- action: input_select.set_options
|
|
data:
|
|
options: '{{ states(''input_text.hue_hubs'') | from_json | map(attribute=''name'')
|
|
| list }}'
|
|
target:
|
|
entity_id: input_select.hue_hubs
|
|
alias: Update hubs in hue hub input select
|
|
- action: input_select.select_option
|
|
data:
|
|
option: '{{ state_attr(''input_select.hue_motion_sensitivity'',''options'')[int(state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivity''),0)]
|
|
}}'
|
|
target:
|
|
entity_id: input_select.hue_motion_sensitivity
|
|
alias: Set sensitivity input select to match motion sensor value
|
|
enabled: true
|
|
mode: single
|
|
- alias: 'Hue motion sensor: Refresh data when selected sensor changes'
|
|
description: Update value when input select changes
|
|
triggers:
|
|
- trigger: state
|
|
entity_id:
|
|
- input_select.motion_sensor_entities
|
|
id: entity_updated
|
|
condition: []
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- entity_updated
|
|
sequence:
|
|
- action: homeassistant.update_entity
|
|
data: {}
|
|
target:
|
|
entity_id: sensor.hue_motion_sensor_hub_data
|
|
alias: Get hub data for the selected sensor
|
|
- action: input_select.select_option
|
|
data:
|
|
option: '{{ state_attr(''input_select.hue_motion_sensitivity'',''options'')[int(state_attr(''sensor.hue_motion_sensor_hub_data'',''sensitivity''),0)]
|
|
}}'
|
|
target:
|
|
entity_id: input_select.hue_motion_sensitivity
|
|
alias: Set sensitivity input select to actual configured value
|
|
mode: parallel
|
|
max: 10
|
|
- alias: 'Pollen: Trigger update of pollen sensors'
|
|
id: pollen_trigger_update_of_pollen_sensors
|
|
description: If pollen sensors are not updated today, try to update every 30 mins
|
|
during the afternoon
|
|
triggers:
|
|
- trigger: time
|
|
at: '16:00:00'
|
|
id: pre_season_single_time_trigger
|
|
- trigger: time_pattern
|
|
minutes: /30
|
|
id: in_season_recurring_time_trigger
|
|
- trigger: state
|
|
entity_id:
|
|
- input_button.pollen_update_feed
|
|
id: button_trigger
|
|
conditions:
|
|
- condition: or
|
|
conditions:
|
|
- alias: If in pre season and pollen is not in the air
|
|
condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ states(''sensor.pollen_update_time'') | as_timestamp()
|
|
< states(''sensor.pollen_timestamp_start_of_today'') | float(0) }}'
|
|
alias: If Pollen sensors was not updated today
|
|
- alias: If between January 1 and March 15
|
|
condition: template
|
|
value_template: '{{ iif((1,1) <= (now().month, now().day) <= (3,15), true,
|
|
false) }}'
|
|
- alias: Pollen is not in the air
|
|
condition: template
|
|
value_template: '{{ is_state_attr(''binary_sensor.pollen_updated_today'',''in_pollen_season'',false)}}'
|
|
- condition: trigger
|
|
id:
|
|
- pre_season_single_time_trigger
|
|
- alias: If in season and pollen is in the air and in trigger timeslot
|
|
condition: and
|
|
conditions:
|
|
- condition: template
|
|
value_template: '{{ states(''sensor.pollen_update_time'') | as_timestamp()
|
|
< states(''sensor.pollen_timestamp_start_of_today'') | float(0) }}'
|
|
alias: If Pollen sensors was not updated today
|
|
- alias: If between January 15 and October 15
|
|
condition: template
|
|
value_template: '{{ iif((1,15) <= (now().month, now().day) <= (10,15), true,
|
|
false) }}'
|
|
- condition: time
|
|
after: '14:59:00'
|
|
before: '20:00:00'
|
|
- condition: template
|
|
value_template: '{{ is_state_attr(''binary_sensor.pollen_updated_today'',''in_pollen_season'',true)}}'
|
|
alias: Pollen is in the air
|
|
- condition: and
|
|
conditions:
|
|
- condition: trigger
|
|
id:
|
|
- button_trigger
|
|
- condition: template
|
|
value_template: '{{ trigger.from_state.state | as_timestamp(0)+5 < now().timestamp()
|
|
}}'
|
|
alias: It is more than 5 seconds ago since last button press
|
|
actions:
|
|
- action: homeassistant.update_entity
|
|
metadata: {}
|
|
data:
|
|
entity_id:
|
|
- sensor.pollen_update_time
|
|
mode: single
|
|
- alias: 'Zone registration: Initialize start values'
|
|
description: Will run at HA start. Only need to run once. Can be disabled afterwards
|
|
triggers:
|
|
- trigger: homeassistant
|
|
event: start
|
|
condition:
|
|
- condition: or
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_config
|
|
state: ''
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_config
|
|
state: unknown
|
|
alias: If config is either not set or empty string
|
|
action:
|
|
- if:
|
|
- condition: or
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_config
|
|
state: unknown
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_config
|
|
state: ''
|
|
then:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '[]'
|
|
target:
|
|
entity_id: input_text.zone_registration_config
|
|
alias: Set config to empty list if unconfigured
|
|
- if:
|
|
- condition: or
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_comment
|
|
state: unknown
|
|
then:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: ''
|
|
target:
|
|
entity_id: input_text.zone_registration_comment
|
|
alias: Set comment to empty if unknown
|
|
- if:
|
|
- condition: or
|
|
conditions:
|
|
- condition: state
|
|
entity_id: input_text.zone_registration_config_label
|
|
state: unknown
|
|
then:
|
|
- action: input_text.set_value
|
|
data:
|
|
value: ''
|
|
target:
|
|
entity_id: input_text.zone_registration_config_label
|
|
alias: Set label to empty if unknown
|
|
- action: shell_command.create_csv_dir
|
|
data: {}
|
|
alias: Create CSV directory if it does not exist
|
|
- action: shell_command.mv_zone_csv_script
|
|
data: {}
|
|
alias: 'Move script from packages to csv directory '
|
|
- alias: 'Zone registration: Fill input select with data'
|
|
description: Populate input selects with zone and person data
|
|
triggers:
|
|
- trigger: homeassistant
|
|
event: start
|
|
id: start
|
|
- trigger: event
|
|
id: reload
|
|
event_type: call_service
|
|
event_data:
|
|
domain: input_select
|
|
action: reload
|
|
condition: []
|
|
action:
|
|
- delay: 00:00:01
|
|
- action: input_select.set_options
|
|
data:
|
|
options: '{{ states.zone | map(attribute=''entity_id'') | list }}'
|
|
target:
|
|
entity_id: input_select.zones
|
|
alias: Add zones to input select
|
|
- action: input_select.set_options
|
|
data:
|
|
options: '{{ states.person | map(attribute=''entity_id'') | list }}'
|
|
target:
|
|
entity_id: input_select.person_entities
|
|
alias: Add persons to input select
|
|
- action: input_select.set_options
|
|
data:
|
|
options: '{{ states(''input_text.zone_registration_config'').replace(''\'''',''\"'')
|
|
| from_json | map(attribute=''name'') | list }}
|
|
|
|
'
|
|
target:
|
|
entity_id: input_select.zone_registration_config
|
|
alias: Update config names in input select
|
|
mode: single
|
|
- alias: 'Zone registration: Auto update input select when config changes'
|
|
description: Read config from UI and select zone and person
|
|
triggers:
|
|
- trigger: state
|
|
entity_id: input_select.zone_registration_config
|
|
condition: []
|
|
action:
|
|
- variables:
|
|
selected_config: "{%- set input_select_sensor = 'input_select.zone_registration_config'\
|
|
\ %}\n{%- set options = state_attr(input_select_sensor,'options') %}\n{%-\
|
|
\ set options_count = options | count() %} \n{%- for i in range(0,options_count)\
|
|
\ %}\n {%- set selected=options[i] %}\n {%- if selected==states(input_select_sensor)\
|
|
\ %}\n {{ i }}\n {%- break %}\n {%- endif %}\n{%- endfor %}\n"
|
|
selected_person: '{% set options=(states(''input_text.zone_registration_config'',''options'').replace(''\'''',''"'')
|
|
| from_json) %}
|
|
|
|
{{ options[selected_config].person }}
|
|
|
|
'
|
|
selected_zone: "{% set options=(states('input_text.zone_registration_config','options').replace('\\\
|
|
'','\"') | from_json) %} \n{{ options[selected_config].zone }}\n"
|
|
config_name: "{% set options=(states('input_text.zone_registration_config','options').replace('\\\
|
|
'','\"') | from_json) %} \n{{ options[selected_config].name }}\n"
|
|
- action: input_select.select_option
|
|
data:
|
|
option: '{{ selected_person }}'
|
|
target:
|
|
entity_id: input_select.person_entities
|
|
- action: input_select.select_option
|
|
data:
|
|
option: '{{ selected_zone }}'
|
|
target:
|
|
entity_id: input_select.zones
|
|
- action: input_text.set_value
|
|
data:
|
|
value: '{{ config_name }}'
|
|
target:
|
|
entity_id: input_text.zone_registration_config_label
|
|
- action: homeassistant.update_entity
|
|
data: {}
|
|
target:
|
|
entity_id: sensor.zone_registration_statistics
|
|
mode: single
|
|
- alias: 'Zone registration: Register configured zone events'
|
|
description: Test configuration and update CSV based on zone enter and leave events
|
|
triggers:
|
|
- trigger: event
|
|
event_type: state_changed
|
|
condition:
|
|
- condition: template
|
|
value_template: '{{ trigger.event.data.entity_id in state_attr(''binary_sensor.zone_registration_config_valid'',''zones'')
|
|
}}
|
|
|
|
'
|
|
alias: Test if triggering entity id exist as part of zone configuration
|
|
action:
|
|
- variables:
|
|
trigger_person: "{%- if trigger.event.data.new_state.state | int(0) > trigger.event.data.old_state.state\
|
|
\ | int(0) %}\n {{ trigger.event.data.new_state.attributes.persons | first\
|
|
\ if trigger.event.data.new_state.state == 1 else\n trigger.event.data.new_state.attributes.persons\
|
|
\ | reject('in', trigger.event.data.old_state.attributes.persons) | first\
|
|
\ }}\n{% else %}\n {{ trigger.event.data.old_state.attributes.persons | first\
|
|
\ if trigger.event.data.old_state.state == 1 else\n trigger.event.data.old_state.attributes.persons\
|
|
\ | reject('in', trigger.event.data.new_state.attributes.persons) | first\
|
|
\ }}\n{%- endif %}\n"
|
|
- condition: template
|
|
value_template: '{{ trigger_person in state_attr(''binary_sensor.zone_registration_config_valid'',''persons'')
|
|
}}
|
|
|
|
'
|
|
alias: Test if triggering person is part of zone configuration
|
|
- condition: template
|
|
value_template: "{{ ( is_state('input_boolean.zone_registration_enter','on') \
|
|
\ \n and trigger.event.data.new_state.state | int(0) > trigger.event.data.old_state.state\
|
|
\ | int(0) ) \n or ( is_state('input_boolean.zone_registration_leave','on')\
|
|
\ \n and trigger.event.data.new_state.state | int(0) < trigger.event.data.old_state.state\
|
|
\ | int(0) ) }}\n"
|
|
alias: Test zone configuration settings for which events to act upon
|
|
- variables:
|
|
zone_config: '{{ state_attr(''binary_sensor.zone_registration_config_valid'',''json'')
|
|
| selectattr(''person'',''eq'',trigger_person) | first}}
|
|
|
|
'
|
|
filename: '{{ (zone_config.name | regex_replace(find=''[^\\w]'',
|
|
|
|
replace=''_'')).replace(''__'',''_'').lower()+''.csv'' }}
|
|
|
|
'
|
|
- action: shell_command.zone_registration
|
|
data:
|
|
entry: auto
|
|
filename: '{{ filename }}'
|
|
alias: Update CSV file
|
|
- action: homeassistant.update_entity
|
|
data: {}
|
|
target:
|
|
entity_id: sensor.zone_registration_statistics
|
|
mode: single
|