docs: update homeassistant/HA_part_1

This commit is contained in:
admin_bora 2024-02-18 09:04:31 +01:00 committed by admin_bora
parent 04d89fd157
commit 6463f7dc4e
1 changed files with 147 additions and 2 deletions

View File

@ -2,7 +2,7 @@
title: Homeassistant Part 1 title: Homeassistant Part 1
description: description:
published: 1 published: 1
date: 2024-02-18T07:54:46.237Z date: 2024-02-18T08:04:27.217Z
tags: tags:
editor: markdown editor: markdown
dateCreated: 2024-02-03T20:23:54.798Z dateCreated: 2024-02-03T20:23:54.798Z
@ -62,7 +62,7 @@ https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/
new HA OS on RaPi4 # new HA OS on RaPi4
http://192.168.178.52:8123/ http://192.168.178.52:8123/
• ==create user • ==create user
◦ user: bora, pass: bora ◦ user: bora, pass: bora
@ -215,6 +215,151 @@ MQTT protocol: 5
# 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 its 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
'''