Walber
July 26, 2023, 2:18pm
#1
2 Likes
Walber
July 26, 2023, 2:19pm
#2
Sensor que informa se o Home Assistant foi desligado corretamente.
Nos atributos é informado os ultimos 10 eventos
template.yaml
- trigger:
- platform: event
event_type:
- homeassistant_started
- homeassistant_stop
sensor:
- name: Start Stop
state: >
{% if trigger.event.event_type == 'homeassistant_started' %}
{{ iif(this.state|default('unknown') == 'shutdown', 'start', 'interrupt') }}
{% else %}
shutdown
{% endif %}
attributes:
history: >
{% set current = this.attributes.get('history', []) %}
{% set new = [{
"event": trigger.event.event_type[14:],
"time": now().isoformat() }] %}
{{ (new + current)[:10] }}
1 Like
Walber
July 26, 2023, 2:20pm
#3
Sensor que informa se a luz foi acionado por automação, Interruptor ou algum usuario (dashboard).
Usando:
<<: &triggered_by
e
<<: *triggered_by
Não é necessario repetir o state para cada sensor.
template.yaml
- trigger:
- platform: state
entity_id: light.quarto
to: ["on", "off"]
sensor:
- name: "Quarto acionado por"
<<: &acionado_por
state: >
{% set c_id = trigger.to_state.context.id %}
{% set c_parent = trigger.to_state.context.parent_id %}
{% set c_user = trigger.to_state.context.user_id %}
{% set light_state = trigger.to_state.state %}
{% set p = states.person | selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) | list %}
{% if c_id != none and c_parent == none and c_user == none %}
Interruptor {{light_state}}
{% elif c_id != none and c_parent == none and c_user != none %}
{{ p[0].attributes.friendly_name|title if p | count == 1 else 'unknown' }} {{light_state}}
{% elif c_id != none and c_parent != none and c_user == none %}
Automação {{light_state}}
{% else %}
Desconhecido {{light_state}}
{% endif %}
- trigger:
- platform: state
entity_id: light.sanca_sala
to: ["on", "off"]
sensor:
- name: "Sanca Sala acionado por"
<<: *acionado_por
- trigger:
- platform: state
entity_id: light.cozinha
to: ["on", "off"]
sensor:
- name: "Cozinha acionado por"
<<: *acionado_por
1 Like
Walber
July 26, 2023, 2:20pm
#4
Sensor que mostra quais sensores MQTT estão com bateria abaixo do nivel XX.
Criar o input_number.nivel_de_bateria_baixa.
template.yaml
- name: "Dispositivos bateria baixa"
unique_id: 86809d8e-1b46-485a-8ef9-956da8da36de
icon: mdi:battery-low
state: >
{% set threshold = states('input_number.nivel_de_bateria_baixa') | int %}
{%- set ns = namespace(sensors=[]) -%}
{%- for state in states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.state_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| selectattr('entity_id', 'in', integration_entities('mqtt'))
| selectattr('state', 'is_number') -%}
{%- if state.state | int <= threshold -%}
{%set ns.sensors = ns.sensors + [dict(name = state.name | replace(' battery', '') | replace(' Baterry', ''), state = state.state | int)] %}
{%- endif -%}
{%- endfor -%}
{%- set batt = ns.sensors | sort(attribute='state') %}
{%- set ns = namespace(batt='') -%}
{%- for state in batt -%}
{% set ns.batt= ns.batt + (state.name ~ ' (' ~ state.state ~'%)' ~ "\n") %}
{% endfor -%}
{% if ns.batt | count > 0 %}
{{ ns.batt | truncate(255, true, '...') }}
{%else %}
{{ 'NA' }}
{% endif %}
3 Likes
Walber
July 26, 2023, 2:21pm
#5
Automação para criar Grupos automaticamente.
Ex Grupo com todas as luzes(exceto xxx, yyyy, zzzz), sensores update e device_tracker para visitantes.
alias: Group Set
description: ""
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: group.set
data:
object_id: update
name: Updates
entities: |
{{ states.update | map(attribute='entity_id') | join(',') }}
alias: Update
- service: group.set
data:
object_id: visitante
name: Visitante
entities: >
{{ states.device_tracker | map(attribute='entity_id') | select('search',
'.*visitante*.') | reject('search', '.*walber*.') | join(',') }}
alias: Visitante
- service: group.set
data:
object_id: luzes
name: Luzes
entities: >
{{ states.light | map(attribute='entity_id') | reject('search',
'.*floodlight*.')| reject('search', '.*status_led*.') | reject('search',
'.*infra_red*.') | join(',') }}
alias: Luzes
mode: single
1 Like