Can you share your final automation code altogether ?
Sure! This is the code for the full automation.
What it does is to check the contact sensor I have installed at the washing machine door. If it detects a state change to closed it means that the washing machine is loaded so it turns on the power switch.
Then it checks the time and date because I have two different electric periods, summer and winter, so it checks if we are on the period of the year for the summer or winter period.
This automation is for the summer, I have a cloned automation just for the winter (surely both can be merged but I haven’t managed it).
So, if we are on summer period and the hour is between 23 and 13h (the cheap electric period), it allows the washing machine to continue.
If its outside this time frame it will stop the energy to the washing machine and it will resume the cycle when the cheap period starts (at 23h).
Once the washing machine is finished it checks the forecast until next Wednesday and sends me a message to unload the washing machine and tells me if it’s going to rain to load the clothes in the dryer. If rain is not forecasted until next Wednesday the message sent says that I can put the cloths to sun dry.
alias: Iniciar la lavadora en horario de verano
description: ''
trigger:
- platform: state
entity_id: binary_sensor.lavadora_contact
to: 'off'
condition: []
action:
- service: switch.turn_on
target:
entity_id: switch.lavadora
data: {}
- choose:
- conditions:
- condition: time
after: '23:00'
before: '13:00'
- condition: template
value_template: '{{ now().timetuple().tm_isdst > 0 }}'
sequence:
- wait_template: '{{ is_state(''sensor.lavadora_run_state'', ''Lavado'') }}'
- service: telegram_bot.send_message
data:
title: '*Lavadora*'
target: -470985966
message: Pongo la lavadora en el tramo eléctrico barato.
- wait_template: '{{ is_state(''sensor.lavadora'', ''off'') }}'
- service: switch.turn_off
target:
device_id: e600a54e4007130fee3befce30b9543c
data: {}
- choose:
- conditions:
- condition: template
value_template: >-
{{ 'rainy' in state_attr('weather.openweathermap',
'forecast')[:days] | map(attribute='condition') }}
sequence:
- service: telegram_bot.send_message
data_template:
title: '*Lavadora*'
target: -470985966
message: >-
La lavadora ha terminado. Dan lluvia los próximos
{{days}} días. Pon la secadora.
default:
- service: telegram_bot.send_message
data_template:
title: '*Fin de lavado*'
target: -470985966
message: >-
La lavadora ha terminado. No dan lluvia los próximos
{{days}} días. Puedes tender fuera.
- conditions:
- condition: time
after: '13:00'
before: '23:00'
- condition: template
value_template: '{{ now().timetuple().tm_isdst > 0 }}'
sequence:
- wait_template: '{{ is_state(''sensor.lavadora_run_state'', ''Lavado'') }}'
- service: switch.turn_off
target:
device_id: e600a54e4007130fee3befce30b9543c
data: {}
- service: telegram_bot.send_message
data:
title: '*Lavadora*'
target: -470985966
message: >-
Has encendido la lavadora dentro del tramo eléctrico caro. La
apago y reanudo el lavado a las 23.
- wait_template: '{{ states.sensor.time.state == ''23:00'' }}'
- service: telegram_bot.send_message
data:
title: '*Lavadora*'
target: -470985966
message: Reanudo el lavado dentro del tramo horario barato.
- service: switch.turn_on
target:
device_id: e600a54e4007130fee3befce30b9543c
data: {}
- wait_template: '{{ is_state(''sensor.lavadora'', ''off'') }}'
- choose:
- conditions:
- condition: template
value_template: >-
{{ 'rainy' in state_attr('weather.openweathermap',
'forecast')[:days] | map(attribute='condition') }}
sequence:
- service: telegram_bot.send_message
data_template:
title: '*Lavadora*'
target: -470985966
message: >-
La lavadora ha terminado. Dan lluvia los próximos
{{days}} días. Pon la secadora
default:
- service: telegram_bot.send_message
data_template:
title: '*Fin de lavado*'
target: -470985966
message: >-
La lavadora ha terminado. No dan lluvia los próximos
{{days}} días. Puedes tender fuera.
default: []
variables:
days: |
{% set num_days = 2 - now().weekday()%}
{{ num_days if num_days > 0 else (7 - num_days | abs) }}
mode: restart
Lovely. Thank you very much.
Thanks, that’s exactly what resolved my issue with rain detection in 2023!
sensor:
- platform: template
sensors:
forecast_total:
friendly_name: "Rain detector"
value_template: "{{state_attr('weather.forecast_starovice_svecovi', 'forecast')[0].precipitation}}"
unit_of_measurement: "mm"
device_class: precipitation
@tomas.svec36 could you explain this template sensor and how to use it in an automation for an inexperienced person in python programming, yaml ninja? Thank you.
Sorry, I do not use python to edit this, I edit the yaml directly in Visual Studio Code: Home Assistant Community Add-on: Visual Studio Code
You basically edit the YAML, restart the Home Assistant and it shows up among your sensors in the dashboard.
Can you explain the logic behind the ‘days’ variable calculation used in this automation for a beginner in python and jinja syntax and programming logic in general? Thank you.
Hi guys. Is this still working for you?
Hi there! Yes, it’s still working fine for me!
I get this error:
None has no element 0
Hi!. I’m sorry, but my Jinja knowdlegde is quite short. Different persons from the comunity helped me to write this code and I didn’t need the detail of the milimeters of rain, so my code is simpler.
Because forecasts aren’t part of the weather entity anymore. It’s now a service call. There are many, many posts about this and the alternative is illustrated in the weather docs.
As an example, here’s my sensor for tomorrow’s rain:
- trigger:
- platform: state
entity_id: weather.openweathermap_daily
action:
- service: weather.get_forecasts
target:
entity_id: weather.openweathermap_daily
data:
type: daily
response_variable: daily
sensor:
- name: Weather Forecast Precipitation Tomorrow
state: >-
{{ daily['weather.openweathermap_daily']['forecast'][1]['precipitation'] }}
device_class: precipitation
unit_of_measurement: "mm"
icon: >-
{% if daily['weather.openweathermap_daily']['forecast'][1]['precipitation'] > 0 %}
mdi:water
{% else %}
mdi:water-off
{% endif %}