371 lines
11 KiB
Markdown
371 lines
11 KiB
Markdown
---
|
||
title: Homeassistant Part 1
|
||
description:
|
||
published: 1
|
||
date: 2024-02-18T08:04:27.217Z
|
||
tags:
|
||
editor: markdown
|
||
dateCreated: 2024-02-03T20:23:54.798Z
|
||
---
|
||
|
||
|
||
|
||
Need to access time sensor
|
||
|
||
alternatively, use now
|
||
|
||
|
||
template:
|
||
- trigger:
|
||
- platform: time_pattern
|
||
seconds: "/1"
|
||
sensor:
|
||
- name: time
|
||
state: "{{ now().strftime('%H:%M:%S') }}"
|
||
|
||
{{ now().strftime("%I:%M %p") }}
|
||
|
||
{{ as_timestamp(state_attr("device_tracker.android_9050f88e39898022", "last_time_reachable")) | timestamp_local }}
|
||
{##{{ state_attr("device_tracker.android_9050f88e39898022", "last_time_reachable").strftime('%H:%M:%S') }}##}
|
||
|
||
- condition: template
|
||
value_template: '"{{ now().timestamp()+300 < (as_timestamp(state_attr("device_tracker.android_9050f88e39898022", "last_time_reachable")) | float)
|
||
}}"'
|
||
|
||
|
||
|
||
|
||
Change attribute value via automation
|
||
|
||
https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/21
|
||
- id: automation 3
|
||
trigger:
|
||
platform: time_pattern
|
||
minutes: '/1'
|
||
action:
|
||
service: python_script.set_state
|
||
data_template:
|
||
entity_id: sensor.cold_water_rate
|
||
state: 42
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# new HA OS on RaPi4
|
||
http://192.168.178.52:8123/
|
||
• ==create user
|
||
◦ user: bora, pass: bora
|
||
• select location==
|
||
• activate advanced mode under profile settings
|
||
• install add ons
|
||
◦ Samba share
|
||
▪ username: homeassistant, password: bora, workgroup: WORKGROUP
|
||
▪ add a new network location: \\192.168.178.52\config
|
||
◦ file editor
|
||
◦ duckdns
|
||
▪ domains:
|
||
- habora.duckdns.org
|
||
token: 799093a4-0b34-454f-99cb-25a4637bf404
|
||
aliases: []
|
||
lets_encrypt:
|
||
accept_terms: true
|
||
algo: secp384r1
|
||
certfile: fullchain.pem
|
||
keyfile: privkey.pem
|
||
seconds: 300
|
||
•
|
||
◦ MariaDB
|
||
▪ databases:
|
||
- homeassistant
|
||
logins:
|
||
- username: homeassistant
|
||
password: password1234
|
||
- username: read_only_user
|
||
password: password1234
|
||
rights:
|
||
- username: homeassistant
|
||
database: homeassistant
|
||
- username: read_only_user
|
||
database: homeassistant
|
||
privileges:
|
||
- SELECT
|
||
▪ recorder:
|
||
db_url: mysql://homeassistant:password@core-mariadb/homeassistant?charset=utf8mb4
|
||
▪
|
||
◦ Nginx Proxy Manager
|
||
▪ Open WebUI → admin@example.com / changeme
|
||
▪ Hosts – Proxy Hosts → Add Proxy Host
|
||
▪ Domain Names: habora.duckdns.org
|
||
▪ Als Scheme wählt Ihr http, unter „Forward Hostname / IP“ tragt Ihr die IP eures Home Assistant ein. → 192.168.178.52
|
||
▪ Bei Forward Port gebt Ihr den Port ein, unter dem euer Home Assistant erreichbar ist. (Standardmäßig ist das der Port 8123)
|
||
▪ Aktiviert abschließend noch den Punkt „Websockets Support„.
|
||
▪ Anschließend wechselt Ihr auf den Reiter SSL.
|
||
▪ Hier wählt ihr unter „SSL Certificate“ den Eintrag „Request a new SSL Certificate“ um ein neues Zertifikat zu erstellen aus.
|
||
▪ Aktiviert nun den Punkt „Force SSL„, tragt eine gültige Email-Adresse für eure Zertifikat ein und akzeptiert die Let’s Encrypt Bedingungen durch aktivieren der Option am unteren Rand der Dialog.
|
||
▪ Bestätigt nun die Eingaben durch einen Klick auf „Save“ und wartet, bis der Vorgang abgeschlossen ist.
|
||
▪ Dann werden im Hintergrund die Zertifikate von Let’s Encrypt generiert. Das kann einen Augenblick in Anspruch nehmen.
|
||
▪ Das registrieren des Zertifikates funktioniert nur, wenn die eingetragene Domain auch auf eure externe IP zeigt, und die Ports 80 und 443 auf die Home Assistant IP weitergeleitet sind.
|
||
• <span style="color:green;font-weight:700;font-size:20px"><mark>PICTURE</mark></span>
|
||

|
||
▪ configuration.yaml
|
||
• http:
|
||
• use_x_forwarded_for: true
|
||
• trusted_proxies:
|
||
• - 127.0.0.1
|
||
• - 172.30.33.0/24
|
||
• - ::1
|
||
◦ Let's Encrypt
|
||
▪ domains:
|
||
- habora.duckdns.org
|
||
email: boraers@googlemail.com
|
||
keyfile: privkey.pem
|
||
certfile: fullchain.pem
|
||
challenge: http
|
||
dns: {}
|
||
▪ add to configuration.aml
|
||
http:
|
||
ssl_certificate: /ssl/fullchain.pem
|
||
ssl_key: /ssl/privkey.pem
|
||
◦ influxdb
|
||
▪ no adaptation of configuration was needed
|
||
▪ http://192.168.178.52:8123/api/hassio_ingress/FJZMGvrT7OnlTzBKdUP9jKiKEe4yp4V4D5VT4zHP5cQ/sources/0/hosts
|
||
▪ http://192.168.178.52:8123/hassio/ingress/a0d7b954_influxdb
|
||
▪ create database: home_assistant
|
||
▪ create user: homeassistant, pass: homeassistant, permissions: ALL
|
||
◦ mosquitto
|
||
▪ Logins:
|
||
- username: note4
|
||
password: note4
|
||
▪ MQTT integration configuration:
|
||
127.0.0.1 ← not core-mosquitto
|
||
1883
|
||
note4
|
||
note4
|
||
MQTT protocol: 5
|
||
◦ Grafana
|
||
▪ env_vars:
|
||
- name: GF_SECURITY_ALLOW_EMBEDDING
|
||
value: "true"
|
||
- name: GF_AUTH_ANONYMOUS_ORG_NAME
|
||
value: Main Org.
|
||
- name: GF_AUTH_ANONYMOUS_ENABLED
|
||
value: "true"
|
||
- name: GF_AUTH_DISABLE_LOGIN_FORM
|
||
value: "false"
|
||
▪ SSL: false or true, depending on having ssl set up
|
||
▪ Network:
|
||
3000
|
||
▪ http://192.168.178.52:8123/hassio/ingress/a0d7b954_grafana
|
||
▪ in web panel
|
||
• add data source: InfluxDB
|
||
• URL: http://a0d7b954-influxdb:8086
|
||
• database, user, pass
|
||
•
|
||
◦ Terminal and SSH
|
||
• integrations
|
||
◦ HACS
|
||
▪ github credentials required
|
||
◦ OpenWeatherMap
|
||
▪ API Ke:
|
||
◦ solarman
|
||
• re-use old config files
|
||
◦ automations
|
||
◦ blueprints
|
||
◦ bosch_shc
|
||
◦ custom_components
|
||
◦ group
|
||
◦ images
|
||
◦ python_scripts
|
||
◦ www
|
||
◦ alarm_control_panel.yaml
|
||
◦ automations.yaml
|
||
◦ climate.yaml
|
||
◦ configuration.yaml
|
||
◦ influxdb.yaml
|
||
◦ mqtt.yaml
|
||
◦ scenes.yaml
|
||
◦ scripts.yaml
|
||
◦ secrets.yaml
|
||
◦ sensor.yaml
|
||
• move to external SSD
|
||
• che
|
||
|
||
<span style="color:green;font-weight:700;font-size:20px"><mark>PICTURE</mark></span>
|
||

|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Home-Assistant script template
|
||
|
||
{## Imitate available variables: ##}
|
||
{% set my_test_json = {
|
||
"temperature": 25,
|
||
"unit": "°C"
|
||
} %}
|
||
|
||
The temperature is {{ my_test_json.temperature }} {{ my_test_json.unit }}.
|
||
|
||
{% if is_state("sun.sun", "above_horizon") -%}
|
||
The sun rose {{ relative_time(states.sun.sun.last_changed) }} ago.
|
||
{%- else -%}
|
||
The sun will rise at {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_local }}.
|
||
{%- endif %}
|
||
|
||
For loop example getting entity values in the weather domain:
|
||
|
||
{% for state in states.weather -%}
|
||
{%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
|
||
{{ state.name | lower }} is {{state.state_with_unit}}
|
||
{%- endfor %}.
|
||
|
||
{% set tado_temp = states('sensor.tado_wohnzimmer_temperature')|float(20) %}
|
||
{% set room_temp = states('sensor.meter_plus_90b4_temperature')|float(20) %}
|
||
{% set current_offset = state_attr('climate.tado_wohnzimmer',
|
||
'offset_celsius') %}
|
||
{{ tado_temp|round(1) }}
|
||
{{ room_temp|round(1) }}
|
||
{{ current_offset|round(1) }}
|
||
{{ (-(tado_temp - room_temp) + current_offset)|round(1) }}
|
||
{{ (tado_temp - current_offset)|round(1) }}
|
||
|
||
|
||
{% set average_temp = 0 %}
|
||
{% set active_rooms = 0 %}
|
||
{% set temp_bedroom = states('sensor.radiator_thermostat_bedroom_temperature')|float(15) %}
|
||
{% set climate_bedroom = states('climate.room_climate_bedroom') %}
|
||
{% set temp_top = states('sensor.radiator_thermostat_child_top_temperature')|float(15) %}
|
||
{% set climate_top = states('climate.room_climate_child_s_room_1_top') %}
|
||
{% set temp_bottom = states('sensor.radiator_thermostat_bottom_temperature')|float(15) %}
|
||
{% set climate_bottom = states('climate.room_climate_child_s_room_2_botto') %}
|
||
|
||
{% if climate_bedroom not in ("unavailable", "unknown", "off") %}
|
||
{% set active_rooms = active_rooms + 1 %}
|
||
{% set average_temp = average_temp + temp_bedroom %}
|
||
{% endif %}
|
||
|
||
{{ average_temp/active_rooms|round(1) if active_rooms>0 else 77 }}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Python Scripts
|
||
https://www.home-assistant.io/integrations/python_script
|
||
|
||
## Example 1
|
||
https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/5
|
||
Scripts are placed in a python_scripts folder under the configuration directory, in my case it is /home/homeassistant/.homeassistant/python_scripts. You then need to either restart Home Assistant or click RELOAD SCRIPTS under Configuration, General, Configuration Reloading.
|
||
|
||
For what it’s worth, I have been hunting for a solution to this problem and I finally decided to make a small script:
|
||
```
|
||
# python_scripts/set_state.py
|
||
|
||
#==================================================================================================
|
||
# python_scripts/set_state.py
|
||
#==================================================================================================
|
||
|
||
#--------------------------------------------------------------------------------------------------
|
||
# Set the state or other attributes for the entity specified in the Automation Action
|
||
#--------------------------------------------------------------------------------------------------
|
||
|
||
inputEntity = data.get('entity_id')
|
||
if inputEntity is None:
|
||
logger.warning("===== entity_id is required if you want to set something.")
|
||
else:
|
||
inputStateObject = hass.states.get(inputEntity)
|
||
inputState = inputStateObject.state
|
||
inputAttributesObject = inputStateObject.attributes.copy()
|
||
|
||
for item in data:
|
||
newAttribute = data.get(item)
|
||
logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
|
||
if item == 'entity_id':
|
||
continue # already handled
|
||
elif item == 'state':
|
||
inputState = newAttribute
|
||
else:
|
||
inputAttributesObject[item] = newAttribute
|
||
|
||
hass.states.set(inputEntity, inputState, inputAttributesObject)
|
||
```
|
||
|
||
|
||
With this script in place, the action could be:
|
||
```
|
||
action:
|
||
service: python_script.set_state
|
||
data_template:
|
||
entity_id: Binary_sensor.sensor1
|
||
state: ON
|
||
```
|
||
|
||
|
||
## Example 2
|
||
https://www.home-assistant.io/integrations/python_script#calling-services
|
||
|
||
Start by enabling the Python script and create the first script.
|
||
• Add to configuration.yaml: python_script:
|
||
• Create folder <config>/python_scripts
|
||
• Create a file <config>/python_scripts/hello_world.py in the folder and give it this content:
|
||
```
|
||
# `data` is available as builtin and is a dictionary with the input data.
|
||
name = data.get("name", "world")
|
||
# `logger` and `time` are available as builtin without the need of explicit import.
|
||
logger.info("Hello {} at {}".format(name, time.time()))
|
||
```
|
||
|
||
• Start Home Assistant to reload the script configuration.
|
||
• Call your new python_script.hello_world service (with parameters) from the Services, using the YAML mode.
|
||
```
|
||
service: python_script.hello_world
|
||
data:
|
||
name: "Input-Text"
|
||
Running this script show absolutely no output on the screen, but it logs with level info. You must have the Logger enabled at least for level info.
|
||
Your confiuration.yaml should include something like this.
|
||
logger:
|
||
default: info
|
||
'''
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|