Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save elvenpath/2c8ee2c29503d74fd2615b231663ab1d to your computer and use it in GitHub Desktop.

Select an option

Save elvenpath/2c8ee2c29503d74fd2615b231663ab1d to your computer and use it in GitHub Desktop.

Revisions

  1. elvenpath revised this gist Apr 17, 2022. 1 changed file with 12 additions and 7 deletions.
    19 changes: 12 additions & 7 deletions offline-notification-for-sensors-with-last_seen.yaml
    Original file line number Diff line number Diff line change
    @@ -53,13 +53,18 @@ variables:
    day: !input 'day'
    hours: !input 'hours'
    exclude: !input 'exclude'
    sensors: "{% set result = namespace(sensors=[]) %} {% for state in states.sensor\
    \ | selectattr('attributes.unit_of_measurement', '==', 'lqi') %}\n {% if state_attr(state.entity_id, 'last_seen')\
    \ != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, 'last_seen')))\
    \ > ((hours | int) * 60 * 60) and not state.entity_id in exclude.entity_id %}\n\
    \ {% set result.sensors = result.sensors + [state.name | regex_replace(find=' linkquality', replace='')\n\
    \ ~ ' (' ~ relative_time(strptime(state_attr(state.entity_id, 'last_seen'), '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %}\n\
    \ {% endif %}\n {% endfor %} {{ result.sensors | join(', ') }}"
    sensors: "{% set result = namespace(sensors=[]) %}\
    \ {% for state in states.sensor | selectattr('attributes.unit_of_measurement', '==', 'lqi') %}\
    \ {% if state_attr(state.entity_id, 'last_seen') != None %}\
    \ {% if (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, 'last_seen'))) > ((1 | int) * 60 * 60) %}\
    \ {% set result.sensors = result.sensors + [state.name | regex_replace(find=' linkquality', replace='') ~ ' (' ~ relative_time(
    \ strptime(state_attr(state.entity_id, 'last_seen'), '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %}\
    \ {% endif %}\
    \ {% else %}\
    \ {% set result.sensors = result.sensors + [state.name ~ ' (Unknown)'] %}\
    \ {% endif %}\
    \ {% endfor %}\
    \ {{ result.sensors | join(', ') }}"
    trigger:
    - platform: time
    at: !input 'time'
  2. @Mr-Groch Mr-Groch revised this gist May 22, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion offline-notification-for-sensors-with-last_seen.yaml
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ blueprint:
    the names of sensors being offline.
    selector:
    action: {}
    source_url: https://gist.github.com/
    source_url: https://gist.github.com/Mr-Groch/bf073b142b507e3b6f8154223f81803b
    variables:
    day: !input 'day'
    hours: !input 'hours'
  3. @Mr-Groch Mr-Groch created this gist May 22, 2021.
    71 changes: 71 additions & 0 deletions offline-notification-for-sensors-with-last_seen.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    blueprint:
    name: Offline detection for Z2M devices with last_seen
    description: Regularly test all sensors with 'lqi' unit of measurement (linkquality Z2M sensors) to detect
    offline and if so execute an action.
    domain: automation
    input:
    hours:
    name: Hours not seen
    description: Sensors not seen this amount of time are assumed to be offline.
    default: 24
    selector:
    number:
    min: 1.0
    max: 168.0
    unit_of_measurement: 'h'
    mode: slider
    step: 1.0
    time:
    name: Time to test on
    description: Test is run at configured time
    default: '10:00:00'
    selector:
    time: {}
    day:
    name: Weekday to test on
    description: 'Test is run at configured time either everyday (0) or on a given
    weekday (1: Monday ... 7: Sunday)'
    default: 0
    selector:
    number:
    min: 0.0
    max: 7.0
    mode: slider
    step: 1.0
    exclude:
    name: Excluded Sensors
    description: Linkquality sensors (from devices that you want to exclude) to exclude from detection. Only
    linkquality entities are supported, devices must be expanded!
    default:
    entity_id: []
    selector:
    target:
    entity:
    domain: sensor
    actions:
    name: Actions
    description: Notifications or similar to be run. {{sensors}} is replaced with
    the names of sensors being offline.
    selector:
    action: {}
    source_url: https://gist.github.com/
    variables:
    day: !input 'day'
    hours: !input 'hours'
    exclude: !input 'exclude'
    sensors: "{% set result = namespace(sensors=[]) %} {% for state in states.sensor\
    \ | selectattr('attributes.unit_of_measurement', '==', 'lqi') %}\n {% if state_attr(state.entity_id, 'last_seen')\
    \ != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, 'last_seen')))\
    \ > ((hours | int) * 60 * 60) and not state.entity_id in exclude.entity_id %}\n\
    \ {% set result.sensors = result.sensors + [state.name | regex_replace(find=' linkquality', replace='')\n\
    \ ~ ' (' ~ relative_time(strptime(state_attr(state.entity_id, 'last_seen'), '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %}\n\
    \ {% endif %}\n {% endfor %} {{ result.sensors | join(', ') }}"
    trigger:
    - platform: time
    at: !input 'time'
    condition:
    - '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
    action:
    - choose: []
    default: !input 'actions'
    mode: single