Posts in

Home Assistant

  • Play a Spotify podcast

    Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

    This blueprint adds a script to your Home Assistant that allows you to play a podcast through the Spotify integration. You’ll just need the podcast’s URL from the share menu.

  • Determining if people are home or away in Home Assistant

    These snippets help us determine if people are home or away, how long they’ve been away, and what type of away mode the home is currently in. I use these in an upcoming video about controlling your home’s climate in Home Assistant.

    The first is a binary sensor helper (binary_sensor.people_home) for determining if people are home. You could extend this to include readings from presence sensors, but this template only relies on the home zone’s persons attribute that lists the people currently within the zone. If your home zone is different from zone.home, make sure you update the template accordingly.

    {{ state_attr('zone.home', 'persons') | length > 0 }}
    

    The next is an automation that sets a date/time input helper (input_datetime.away_since) when the sensor above changes to zero.

    alias: Set away since timestamp
    description: ""
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.people_home
        to: "0"
    conditions: []
    actions:
      - action: input_datetime.set_datetime
        metadata: {}
        data:
          datetime: "{{ now() }}"
        target:
          entity_id: input_datetime.away_since
    mode: single
    
    

    Bringing it all together, we create a template sensor helper (sensor.away_mode) that represents how long the zone has been empty with one of three options: home, away, and extended.

    • If the zone hasn’t been occupied for at least the last 30 minutes, away is used.
    • If the zone hasn’t been occupied for at least the last 12 hours, extended is used.
    • Otherwise, home is used.
    {% if states('binary_sensor.people_home') == 'on' %}
      home
    {% elif as_local( as_datetime( states('input_datetime.away_since') ) ) < (now() - timedelta(hours=12)) %}
      extended
    {% elif as_local( as_datetime( states('input_datetime.away_since') ) ) < (now() - timedelta(minutes=30)) %}
      away
    {% else %}
      home
    {% endif %}
    

  • Determine if it’s currently daytime with the sun in Home Assistant

    This snippet is for a helper that determines if it’s currently daytime based on the sun.

    Create a new binary template helper and use this as your template:

    {% set next_rising  = as_local( as_datetime( state_attr('sun.sun', 'next_rising' ) ) ) %}
    {% set next_setting = as_local( as_datetime( state_attr('sun.sun', 'next_setting' ) ) ) %}
    {{ 
    not (
      next_rising.date() == now().date()
      or next_setting.date().day == ( now() + timedelta(days=1) ).date().day
    )
    }}
    

    I use sunrise and sunset for this helper, but you can also use dawn and dusk. Just replace next_rising with next_dawn and next_setting with next_dusk.

    Since this helper is a sensor that changes based on the sun states, it can be used in automation triggers like turning on a porch light.

  • Getting the daily temperatures and precipitation from NWS forecasts in Home Assistant

    This is a snippet that you can use to create sensors for the day’s high and low temperature from the NWS integration: high temperature, low temperature, daytime precipitation chance, and nighttime precipitation chance.

    These sensors can be used in automations for your climate control with {{ states('sensors.wx_temp_high') }} or to be displayed on a dashboard.

    It uses the Twice Daily forecast action, so should only be ran once per day. From my experience, the forecast is generated before 6AM central time so I have this running a 6AM.

    To handle situations where the NWS forecast is incorrectly formatted or unavailable, this sets a temperature of -200°F (since the coldest ever recorded is -126°F, this should be a safe number) and precipitation chances to -200%. These should be handled in whatever automations you use the temperature sensors in. I’ve included YAML of an automation to alert you of this error below.

    Since you can’t create template sensors directly in the Home Assistant UI, this snippet needs to be added to your configurations.yml file. You can edit the file directly on your server or by using the File Editor add-on or Studio Code Server add-on.

    You’ll need to change weather.ktcl_daynight to the correct entity ID for your NWS integration.

    template:
      - trigger:
          # Run each day at 6AM
          - trigger: time_pattern
            hours: 6
            minutes: 0
        
        action:
          - action: weather.get_forecasts
            target:
              # Change this to your entity ID
              entity_id: weather.ktcl_daynight
            data:
              type: twice_daily
            response_variable: wx_data
          - variables:
              # Change this to your entity ID
              daytime: >-
                {% for wx in wx_data["weather.ktcl_daynight"]["forecast"] if as_datetime(wx["datetime"]).date() == now().date() and wx["is_daytime"] %}
                  {{ wx }}
                {% else %}
                  {"temperature": -200, "precipitation_probability": -200}
                {% endfor %}
              # Change this to your entity ID
              nighttime: >-
                {% for wx in wx_data["weather.ktcl_daynight"]["forecast"] if as_datetime(wx["datetime"]).date() == now().date() and not wx["is_daytime"] %}
                  {{ wx }}
                {% else %}
                  {"temperature": -200, "precipitation_probability": -200}
                {% endfor %}
        
        sensor:
          ###
          # Create high temperature sensor
          ###
          - name: High temperature
            unique_id: wx_temp_high
            state: "{{ daytime.get('temperature') }}"
            unit_of_measurement: °F
            
          ###
          # Create low temperature sensor
          ###
          - name: Low temperature
            unique_id: wx_temp_low
            state: "{{ nighttime.get('temperature') }}"
            unit_of_measurement: °F
            
          ###
          # Create daytime precipitation sensor
          ###
          - name: Daytime chance of precipitation
            unique_id: wx_precipitation_daytime
            state: "{{ daytime.get('precipitation_probability') }}"
            unit_of_measurement: "%"
            
          ###
          # Create nighttime precipitation sensor
          ###
          - name: Nighttime chance of precipitation
            unique_id: wx_precipitation_nighttime
            state: "{{ nighttime.get('precipitation_probability') }}"
            unit_of_measurement: "%"
    

    Outdoor Temperature Sensor

    Using the daytime sensor, you can create a template sensor that returns the high (daytime) or low (nighttime) temperature.

    {% if states('binary_sensor.daytime') == 'on' %}
      {{ states('sensor.wx_temp_high') }}
    {% else %}
      {{ states('sensor.wx_temp_low') }}
    {% endif %}
    

    Error Notification Automation

    This is an automation for creating a persistent notification when the weather sensors are in an error state.

    alias: Notification on weather sensor error state
    triggers:
      - trigger: numeric_state
        entity_id:
          - sensor.wx_temp_high
        below: -199
      - trigger: numeric_state
        entity_id:
          - sensor.wx_temp_low
        below: -199
      - trigger: numeric_state
        entity_id:
          - sensor.wx_precipitation_daytime
        below: -199
      - trigger: numeric_state
        entity_id:
          - sensor.wx_precipitation_nighttime
        below: -199
    conditions: []
    actions:
      - action: persistent_notification.create
        metadata: {}
        data:
          message: >-
            One or more of the weather sensors from the NWS forecast are in an error
            state as of {{ now().strftime('%m/%d/%Y') }}.
          notification_id: wx_sensors_in_error_state
    mode: single