Hello all,
Currently I have a running Home assistant setup. However I’m having trouble with the way the lights turned on number is being calculated. It looks like it has a large delay in counting the lights on or off / not all light entitites state changes are registered instantly. I have a lights-groups.yaml file where I put certain lights in certain groups. I use light.all_lights_group in the code instead of just searching for all light entities since I do not wish all lights to be counted. In that group are only the lights I wish to be part of the count.
This is currently my template.yaml file.
#### Custom template sensors ##########################################################
#### Sprinkler Total Expected Precipitation trigger ###################################
- trigger:
- platform: time_pattern
minutes: /1
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.openweathermap
response_variable: my_forecast
sensor:
- name: Sprinkler Total Expected Precipitation
unique_id: eed1164d-2f8a-4197-88cf-80846bcbac63
icon: mdi:weather-pouring
unit_of_measurement: mm
state: >
{% set ns = namespace(totalprecipitation=0) %}
{% if my_forecast and 'forecast' in my_forecast['weather.openweathermap'] %}
{% for daypart in range(0, 7) %}
{% set forecast = my_forecast['weather.openweathermap']['forecast'] %}
{% if daypart < forecast | length %}
{% set precipitation = forecast[daypart].precipitation %}
{% set precipitation_probability = forecast[daypart].precipitation_probability / 100 %}
{% if precipitation_probability > 0 %}
{% set precipitation = precipitation * precipitation_probability %}
{% endif %}
{% set ns.totalprecipitation = ns.totalprecipitation + precipitation %}
{% endif %}
{% endfor %}
{% endif %}
{{ ns.totalprecipitation | float(0) | round(2) }}
#### Custom daily forecast sensor ###################################
- trigger:
- platform: time_pattern
hours: /1
- platform: homeassistant
event: start
action:
- service: weather.get_forecasts
data:
type: daily
target:
entity_id: weather.openweathermap
response_variable: daily_forecast
sensor:
- name: daily_forecast_info
state: "{{ states('weather.openweathermap') }}"
attributes:
forecast: "{{ daily_forecast['weather.openweathermap'].forecast }}"
#### Sprinkler Total Rain Fallen Plus Expected ###################################
- name: Sprinkler Total Rain Fallen Plus Expected
unique_id: 2524c618-14f4-4db6-876f-8111b3c19bdf
icon: mdi:weather-pouring
unit_of_measurement: mm
state: >
{% set total_rain_fallen = states('input_number.sprinkler_total_rain_fallen') | float(0) %}
{% set total_expected_precipitation = states('sensor.sprinkler_total_expected_precipitation') | float(0) %}
{{ (total_rain_fallen + total_expected_precipitation) | round(2) }}
#### Sprinkler Days Since Last Watering Day ####################################
- name: Sprinkler Days Since Last Watering Day
unique_id: f5af5762-f4d6-418e-801d-a24be0109d59
icon: mdi:calendar
state: >
{{ ((as_timestamp(now()) - as_timestamp(states("input_datetime.sprinkler_last_Watering_day"))) / 86400) | float | round(2) }}
#### Sprinkler Current Temperature Is Higher ###################################
- name: Sprinkler Current Temperature Is Higher
unique_id: 92d2df9d-dbe7-4bb9-94ce-9a368ac39be4
icon: mdi:check
state: >
{%- if states('sensor.openweathermap_temperature') | float > states('input_number.sprinkler_maximum_temperature_measured') | float -%}
true
{%- else -%}
false
{%- endif %}
#### Count how many lights are on ###################################
- name: "Lights On Count"
state: >
{{ expand('light.all_lights_group') | selectattr('state', 'eq', 'on') | list | count }}
#### Count how many doors and windows are open ###################################
- name: "Open Doors and Windows Count"
state: >
{{ expand('binary_sensor.all_doors_and_windows') | selectattr('state', 'eq', 'on') | list | count }}
#### Count how many devices have low battery ###################################
- name: "Low Battery Devices Count"
state: >
{% set threshold = states('input_number.battery_threshold') | int %}
{% set battery_sensors = namespace(count=0) %}
{%- for state in states.binary_sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('state', '==', 'on') -%}
{% set battery_sensors.count = battery_sensors.count + 1 %}
{%- endfor -%}
{%- 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('state', 'is_number') -%}
{%- if state.state | int <= threshold -%}
{% set battery_sensors.count = battery_sensors.count + 1 %}
{%- endif -%}
{%- endfor -%}
{{ battery_sensors.count }}
#### Check if trashcollection is today, tomorrow, the day after or in x days ###################################
- name: "Trash Tomorrow"
unique_id: "sensor_trash_tomorrow"
state: >
{% set target_date = states('sensor.afvalwijzer_next_date') %}
{% if target_date %}
{% set today = now().date() %}
{% set target_date_obj = strptime(target_date, '%Y-%m-%d').date() %}
{% set diff_days = (target_date_obj - today).days %}
{% if diff_days == 0 %}
today
{% elif diff_days == 1 %}
tomorrow
{% elif diff_days == 2 %}
the day after tomorrow
{% elif diff_days > 2 %}
in {{ diff_days }} days
{% else %}
unknown
{% endif %}
{% else %}
unknown
{% endif %}
#### Calculate the remaining time for the washing machine ###################################
- name: "Wasmachine Remaining Time v2"
unique_id: wasmachine_remaining_time_v2
state: >-
{% set rem_h = (as_timestamp(states.sensor.wasmachine_washer_completion_time.state) - as_timestamp(now())) | timestamp_custom('%-H', false) %}
{% set rem_m = (as_timestamp(states.sensor.wasmachine_washer_completion_time.state) - as_timestamp(now())) | timestamp_custom('%-M', false) %}
{% if int(rem_h) > 0.9 %} {{ rem_h }} hour(s) and {{ rem_m }} minute(s) {% else %} {{ rem_m }} minute(s) {% endif %}
#### Create custom greeting sensor ###################################
- name: "Greeting Sensor"
unique_id: random_greeting_sensor
state: >
{% set hour = now().hour %}
{% if hour < 12 %}
{% set greetings = ["Goedemorgen", "Hallo"] %}
{{ greetings | random }}
{% elif hour < 18 %}
{% set greetings = ["Goedemiddag", "Hallo"] %}
{{ greetings | random }}
{% else %}
{% set greetings = ["Goedenavond", "Fijne avond", "Hoi", "Een prettige avond gewenst"] %}
{{ greetings | random }}
{% endif %}
Am I correct to note that all sensors under the “Custom daily forecast sensor” are only triggered once every hour? Ofcourse thats not what I want. If anyone can take a look for me and help me out or clean up the code I really appreciate it.
I also noticed just now that the service: get.weather_forecasts is giving me “string does not match the pattern of “deprecated”” Is this changed again recently?
Kind regards,
Remco