# Youtube channel Real Smart Home

# Each declaration can be in configuration file only once, inside by indentation you can add another of the same type.

# Sonoff LAN integration HACS
# video how to install HACS
sonoff:                       # declaration sonoff
    username: !secret_email
    password: !secret_password
    sensors: [power]          # sensor type what I want to find and add
    force_update: [power]     # sensor type what I want to update more frequently
    scan_interval: '00:00:05' # update statuses every 5 seconds
    reload: always            # update device from eWeLink every restart
    mode: local               # LAN mode, but not all devices may work in it

# casting entities to another type (device_class)
devices:
    10008c8efa:                    # sensor unique code from the .sonoff.json file in the HA directory
        device_class: [light, fan] # double switch - the first will be a light and the second a fan
        name: Bathroom             # new switch name
    10007558d4:
        device_class: light
        name: Kitchen

# Sonoff Bridge
fbridge:
    hall:                    # sensor name in the eWeLink application
        device_class: motion # sensor type: door, window or motion
        timeout: 120         # time in seconds after which the state automatically switches to OFF


# Tasmota integration
# casting entities into light
light:                                # declaration of light
  - platform: switch                  # original type
    name: "Bathroom"                  # the new entity name will be "light.bathroom"
    entity_id: switch.bathroom_light  # original entity

# casting entities to a fan
fan:                   # fan declaration
  - platform: template # can only be done with the help of a template
    fans:              # we will add more fans according to the template
        bathroom:      # the new entity name will be "fan.bathroom"
            friendly_name: "Bathroom" # a more user-friendly display-only name
            value_template: "{{ states('switch.bathroom_fan') }}" # entity status via value template
            turn_on:                              # what is the on state
                service: switch.turn_on           # service turn on the switch
                entity_id: switch.bathroom_fan    # entity fan
            turn_off:                             # what is the off state
                service: switch.turn_off          # service turn off the switch
                entity_id: switch.bathroom_fan    # entity fan

# Tasmota Bridge (Sonoff originally)
binary_sensor:                # binary sensor declaration
  - platform: mqtt            # type of communication
    device_class: motion      # sensor type: motion, door, window
    name: 'hall_motion'       # name the entity
    payload_on: "EC2D2E"      # on sensor code
    payload_off: "EC2D2Eoff"  # off sensor code (optional)
    off_delay: 20             # auto-off time in seconds; 0 remains on
    state_topic: "tele/bridge/RESULT"                  # constanttant
    value_template: '{{ value_json.RfReceived.Data }}' # constant
  - platform: mqtt            # type of communication
    device_class: window      # sensor type: motion, door, window
    name: 'kitchen_window'    # name the entity
    payload_on: "EC4D4E"      # on sensor code
    payload_off: "EC4D4Eoff"  # off sensor code (optional)
    off_delay: 0              # auto-off time in seconds; 0 remains on
    state_topic: "tele/bridge/RESULT"                  # constanttant
    value_template: '{{ value_json.RfReceived.Data }}' # constant


# Variable HACS integration variables
var:                  # variable declaration
    window_left:      # variable name
        value: false  # default value
    window_right:
        value: false
    volume_last:
        value: 4
    volume_mute
        value: false
    video_id:
        value: ''
    video_position:
        value: 0
    mirror_program:
        value: 1
    mirror_youtube:
        value: 1


input_number:
# Volume slider
    volume_now:
        name: Volume
        initial: 4
        min: 1
        max: 7
        step: 1
        icon: mdi:volume-high


# Telegram communication
# video how to create your bot
telegram_bot:                     # telegram robot declaration
  - platform: polling            
    api_key: !secret_key          # generated api key
    allowed_chat_ids: 123456789   # assigned communication ID
notify:                           # communication client declaration
  - name: telegram                # client name
    platform: telegram            # client service
    chat_id: 123456789            # client id

climate:                                     # thermostat declaration
# Thermostat
  - platform: generic_thermostat             # softwarový termostat
    name: Living room                        # room thermostat name
    heater: switch.topeni                    # heater head switch
    target_sensor: sensor.air98_temperature  # room temperature sensor
    min_temp: 20               # minimum temperature on the thermostat
    max_temp: 24               # maximum temperature on the thermostat
    # target_temp: 23          # desired temperature set after HA restart
    cold_tolerance: 0.5        # start heating when the temperature drops 0.5° C below the required temperature
    hot_tolerance: 0           # stop heating when the required temperature is reached
    min_cycle_duration:        # if the heating has been switched on, for example, it cannot be
        seconds: 30            # next 30 seconds off (or on)
    initial_hvac_mode: "off"   # what should the thermostat do after restart - heat or not
    away_temp: 15              # Away mode desired temperature when you are not at home
    precision: 0.1             # take into account the inaccuracy of the temperature measurement
    ac_mode: false             # zařízení je klimatizace, netopí, ale chladí


sensor:                              # sensor declaration
# Youtube channel integration HACS
  - platform: youtube                        # youtube channel sensor
        channel_id: UCuNSczBLfH6tstVsi4584XA # copy the channel address from the end of the web address of the youtube channel
  - platform: template               # based on the template
    sensors:                         # we will add more sensors according to the template
# Period of day template sensor
        period_of_day:               # sensor name
            friendly_name: "Time of day" # more user-friendly name
            value_template: >-           # entity status via value template
              {% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
                  dusk
                {% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}

                  dawn
                {% elif (states.sun.sun.attributes.elevation) < -4 %}
                  night
                {% else %}
                  day
                {% endif %}

            icon_template: >-            # specifying an icon based on the state of the entity via a value template
                {% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
                  mdi:weather-sunset-down
                {% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
                  mdi:weather-sunset-up
                {% elif (states.sun.sun.attributes.elevation) < -4 %}
                  mdi:weather-night
                {% else %}
                  mdi:weather-sunny
                {% endif %}

# Air traffic light outdoors
        air_color:
            friendly_name: "Air traffic light"
            value_template: >-
               {% if states('sensor.pm_1')|float > 55 or states('sensor.pm_25')|float > 70 or states('sensor.pm_10')|float > 100 %}
                  red
               {% elif states('sensor.pm_1')|float > 25 or states('sensor.pm_25')|float > 35 or states('sensor.pm_10')|float > 45 %}
                  yellow
               {% elif states('sensor.pm_1')|float > 0 and states('sensor.pm_25')|float > 0 and states('sensor.pm_10')|float > 0 %}
                  green
               {% else %}
               {% endif %}

            icon_template: mdi:traffic-light
# Pressure converted to sea level
        pressure_outside:
            device_class: pressure
            #unit_of_measurement: 'hPa' # the unit of measure reduces the font and readability, better not to specify it
            friendly_name: "Pressure converted to sea level"
            value_template: "{{ ( states('sensor.pressure')|float / (1-0.0065*360/ (states('sensor.temperature_outside')|float + 273.15) ) ** (5.255) )|round(0) }}"
# uRADMonitor SMOGGIE-PM sensors
  - platform: rest                   # communication type
    device_class: temperature        # sensor type
    resource: http://192.168.0.177/j # sensor IP address
    name: Temperature uRadMonitor    # sensor name
    unit_of_measurement: "°C"        # measurement unit
    value_template: '{{ (value_json.data.temperature - 3.7)|round(1) }}'
    # calibration value of 3.7 is subtracted from the sensor value, round(1): rounded to one decimal place
  - platform: rest
    device_class: humidity
    resource: http://192.168.0.177/j
    name: Humidity
    unit_of_measurement: "%"
    value_template: '{{ value_json.data.humidity|float|round(2) + 9.45 }}'
    # float: the value is cast to a decimal point number
  - platform: rest
    device_class: pressure
    resource: http://192.168.0.177/j
    name: Pressure
    unit_of_measurement: "Pa"
    value_template: '{{ ((value_json.data.pressure|float - 360) / 100) }}'
  - platform: rest
    resource: http://192.168.0.177/j
    name: PM 1
    unit_of_measurement: "µg/m³"
    value_template: '{{ value_json.data.pm1 }}'
  - platform: rest
    resource: http://192.168.0.177/j
    name: PM 25
    unit_of_measurement: "µg/m³"
    value_template: '{{ value_json.data.pm25 }}'
  - platform: rest
    resource: http://192.168.0.177/j
    name: PM 10
    unit_of_measurement: "µg/m³"
    value_template: '{{ value_json.data.pm10 }}'


rest_command:
# Vektiva Smarwi
    window_open:  # open ventilation
        url: 'http://192.168.0.199/cmd/open'
    window_close: # close ventilation
        url: 'http://192.168.0.199/cmd/close'
    window_stop:  # stop movement
        url: 'http://192.168.0.199/cmd/stop'
    window_fix:   # unlock the window for manual opening; lock by closing
        url: 'http://192.168.0.199/cmd/fix'


switch:                # switch declaration
  - platform: template # based on a template
        switches:      # we will add more template switches
# window Vektiva
            smarwi:    # the name of the custom switch
                friendly_name: "Open the window"
                value_template: "{{ is_state('var.window_right', 'True') }}" # state for turn_on
                turn_on:              # what happens when switching on
                  - condition: state  # always avoid repeating, you don't have to do it in automation
                    entity_id: var.window_right
                    state: 'False'
                  - service: var.set
                    data:
                        entity_id: var.window_right
                        value: True
                  - service: rest_command.window_open  # open the window
                  - service: climate.set_preset_mode   # heating interrupt - use preset_mode
                    entity_id: climate.living_room     # then keep switching for time logic 
                    data:
                        preset_mode: "away"
                turn_off:             # what happens when you shut down
                  - condition: state
                    entity_id: var.window_right
                    state: 'True'
                  - service: var.set
                    data:
                        entity_id: var.window_right
                        value: False
                  - service: rest_command.window_close
                  - service: climate.set_preset_mode
                    entity_id: climate.living_room
                    data:
                        preset_mode: "none"
                icon_template: >-
                    {% if is_state('var.window_right', 'True') %}
                        mdi:window-open-variant
                    {% else %}
                        mdi:window-closed-variant
                    {% endif %}
# Windows volume
           
volume_mute:
                friendly_name: "Mute"
                value_template: "{{ is_state('var.volume_mute', 'True') }}"
                turn_on:
                  - condition: state
                    entity_id: var.volume_mute
                    state: 'False'
                  - service: var.set
                    data:
                        entity_id: var.volume_mute
                        value: True
                  - service: mqtt.publish
                    data:
                        topic: pc/mute
                turn_off:
                  - condition: state
                    entity_id: var.volume_mute
                    state: 'True'
                  - service: var.set
                    data:
                        entity_id: var.volume_mute
                        value: False
                  - service: mqtt.publish
                    data:
                        topic: pc/mute
                icon_template: >-
                    {% if is_state('var.volume_mute', 'True') %}
                        mdi:volume-mute
                    {% else %}
                        mdi:volume-low
                    {% endif %}


# Declare only if auto-login is required. If you don't know what you're doing, don't do it.
homeassistant:
    auth_providers:
      - type: trusted_networks
# Approved IP addresses
        trusted_networks:
          - 192.168.0.111                                   # Chromecast IP address
        trusted_users: 
            192.168.0.111: bb4013f64d8s4e90bcg25316h4sxc257 # your user ID, not the user name
        allow_bypass_login: true # Access without the need for a login (auto-login)
      - type: homeassistant      # Need to finally approve access for HA (otherwise user error)