Hello,
I’ve built an automation which should restart my Shelly devices whenever they are unavailable by HA but it is still possibe to access through http that I check with the NMAP integration. Together with automation this I’ve built two python scripts that handle the Shelly API Gen1 and Gen2.
Starting by the error:
2024-03-30 17:01:23.070 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'split' when rendering '{{ device_attr('{{ trigger.to_state.state.entity_id }}','configuration_url').split('/')[2].title()}}'
2024-03-30 17:01:23.071 ERROR (MainThread) [homeassistant.components.automation.shelly_restart_when_unavailable] Error rendering variables: UndefinedError: 'None' has no attribute 'split'
This works on the template editor:
{{ device_attr('sensor.shelly_luz_garagem_rssi','configuration_url').split('/')[2].title()}}
Result: 192.168.1.129
However this fails in the automation (btw I’m not proficient in yaml…) and no doubt the issue is here and all the other variables that use the the trigger.to_state
I’ve already tried different combinations… before open this topic but without success.
{{ device_attr('{{ trigger.to_state.state.entity_id }}','configuration_url').split('/')[2].title()}}
Here is the automation that might be usefull for somebody:
- id: shelly_restart_when_unavailable
alias: shelly_restart_when_unavailable
description: 'Reboot Shelly if Unavailable for x Minutes'
trigger:
- platform: state
entity_id:
- sensor.shelly_luz_escadas_sotao_e_arrecadacao_rssi
- sensor.shelly_luz_escadas_andar_1_rssi
- sensor.shelly_luz_garagem_rssi
- sensor.shelly_em_maquina_lavar_secar_rssi
- sensor.shelly_em_quadro_sotao_rssi
- sensor.shellyplug_4aa5fc_rssi
- sensor.shelly_recuperador_calor_rssi
- sensor.receptor_satelite_sala_rssi
- sensor.shelly_luz_wc_andar_1_rssi
- sensor.shelly_luz_wc_servico_rssi
- sensor.shelly_luz_wc_suite_rssi
- sensor.shelly_pro_3em_quadro_principal_rssi
to:
- unavailable
for:
minutes: 5
variables: # missing code for Shelly Gen3
shelly_name: >
{{ device_attr('{{ trigger.to_state.state.entity_id }}','name_by_user')}}
shelly_ip: >
{{ device_attr('{{ trigger.to_state.state.entity_id }}','configuration_url').split('/')[2].title()}}
shelly_gen: >
{{ device_attr('{{ trigger.to_state.state.entity_id }}','hw_version').split(' ')[0]}}
shelly_device_tracker: >
{% set ip_dev= device_attr('{{ trigger.to_state.state.entity_id }}','configuration_url').split('/')[2].title()%}
{{ (states.device_tracker | map(attribute='entity_id') | select('is_state_attr', 'ip', ip_dev) | list)[0] }}
service_gen: >
{% if (device_attr('{{ trigger.to_state.state.entity_id }}','hw_version').split(' ')[0]) == 'gen1' %}
pyscript.shelly_gen1_reboot
{% else %}
pyscript.shelly_gen2_reboot
{% endif %}
action:
- if:
- condition: template
value_template: >-
{{ states('{{ shelly_device_tracker }}') == 'home' }}
then:
- service: '{{ service_gen }}'
data:
ip: '{{ shelly_ip }}'
- wait_template: "{{ trigger.to_state.state != 'unavailable' and trigger.to_state.state != 'unknown' }}"
timeout:
minutes: 3
- if:
- "{{ wait.completed }}"
then:
- service: persistent_notification.create
data:
title: >
Shelly Available after Restart!
message: >
Dispositivo: {{ shelly_name }}{{ '\n' -}}
Generation: {{ shelly_gen }}{{ '\n' -}}
Endereço IP: {{ shelly_ip }}{{ '\n' -}}
Shelly Device Tracker: {{ states('{{ shelly_device_tracker }}') }}{{ '\n' -}}
Em {{as_timestamp(now()) | timestamp_custom('%d/%m/%Y as %T')}}
else:
- service: notify.joao_ios_mobile
data:
title: "Shelly Unavailable After Restart! \u26a0\ufe0f"
message: >
Generation: {{ shelly_gen }}{{'\n'}}
Endereço IP: {{ shelly_ip }}{{'\n'}}
Em {{ as_timestamp(now()) | timestamp_custom('%d/%m/%Y as %T') }}
data:
subtitle: "{{ shelly_name }}"
url: /lovelace/main-control
presentation_option:
- alert
- badge
push:
interruption-level: time-sensitive
badge: 191
sound: none
else:
- service: notify.joao_ios_mobile
data:
title: "Shelly Unavailable & Offline! \u26a0\ufe0f"
message: >
Generation: {{ shelly_gen }}{{'\n'}}
Endereço IP: {{ shelly_ip }}{{'\n'}}
Em {{ as_timestamp(now()) | timestamp_custom('%d/%m/%Y as %T') }}
data:
subtitle: "{{ shelly_name }}"
url: /lovelace/main-control
presentation_option:
- alert
- badge
push:
interruption-level: time-sensitive
badge: 191
sound: none
mode: parallel
max: 15