In a nutshell, I have a “Goodnight” script that turns off lights and stuff. What I would also like it to do is have my google home mini respond back if doors are left open. For example, if the garage door was left open it could say “Garage door is open, please close it.” If the back door and garage door were left open it would say “Garage door is open, please close it. Back door is open, please close it.” The idea is that I could concatenate parts of the status report based on the state of entities. I have been having a really hard time though figuring out how to create a string variable in a script to allow for that concatenation in essentially a bunch of if statements for state checks. Any help would be appreciated.
Here is some pseudo code of what I am trying to do:
variable: textToSay = ""
if cover.garage_door == open
textToSay += "Close the garage door. "
endif
if binary_sensor.back_door == open
textToSay += "Close the back door. "
endif
if textToSay == ""
textToSay == "All clear."
endif
Thanks Taras! This was very very helpful! Sometimes good example is all that is needed. Your example of putting it into a markdown card was great because its easy to test with! I modified it a bit more to add on an additional message about the pool pump.
command_home_status_report:
alias: Status report
sequence:
- service: tts.google_translate_say
data:
entity_id:
- media_player.den_speaker
- media_player.family_room_speaker
message: >-
{% set doors = expand('binary_sensor.garage_sensor_105_2','binary_sensor.garage_sensor_105_3')
| selectattr('state', 'eq', 'on')
| map(attribute='name') | list %}
{% set qty = doors | count %}
{% set p1 = 'are' if qty > 1 else 'is' %}
{% set textToSay = '' %}
{% if qty > 0 %}
{% set textToSay = ' and '.join((doors | join(', ')).rsplit(', ', 1)) + p1 + 'open. ' %}
{% endif %}
{% if is_state('switch.z_wave_pool_pump_switch','on') %}
{% set textToSay = textToSay + 'Pool pump is on.' %}
{% endif %}
{{ textToSay if textToSay | length > 0 else 'All clear.' }}