Advanced Jinja (for me at least)

So I am trying to step up my Automation game so I have dived into Jinja templating…

I have the following code working PERFECTLY in the template Dev sandbox but I don’t quite understand how to integrate it into the YAML files. If anyone can shine some light on this, I’ll pay it forward and add it to the documentation website. :slight_smile:

######################################################################################################
###Script to run every 30 minutes to check if 433 RF Switches are in the state they should be.
###  Sample Call
#  action:
#   - service: script.reliability
######################################################################################################
    
{% macro swTest(onoff='') %}    - service: switch.turn_{{ states(onoff) }}
      entity_id: {{ onoff }}{% endmacro %}

switch_check:
  sequence:  
{{ swTest("switch.foyer_outlet") }}
{{ swTest("switch.printer_outlet") }}
{{ swTest("switch.garage_outlet") }}
{{ swTest("switch.den_outlet") }}
{{ swTest("switch.living_room_outlet") }}
{{ swTest("switch.living_room_amp") }}

Here is a link to my source Repo : https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/script/Reliability.yaml.Broken

Thanks for any help.
CARLO

Update: Until I can see if there is a better way, I decided to just do it the manual way. Not very Pythonic but it’ll do for now.

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/script/Reliability.yaml

@CCOSTAN, Since this file is no longer available on your GitHub account, I want to know how you managed to integrate the macros you made. Specifically, were you able to call a service from a macro inside the yaml files?

Unfortunately I was never able to get the macros to properly work. :(. I don’t believe they are supported as I haven’t seen any working examples of them.

Well thanks for your help. Coming from a programming background, the builtin functionality of Home Assistant’s yaml / jinja is somewhat annoying since I can’t get a simple IF THEN ELSE going with anything other than values. For example, I can’t (without calling scripts from scripts) get an IF then call service A, ELSE IF call service B and service C, ELSE call service D.

Conditions halt the script entirely instead of allowing a fallthrough, so they don’t have an ELSE, just an IF.
service templates don’t let you call multiple services sometimes and sometimes not, etc.
I don’t want to write an AppDaemon.

All in all it’s a little frustrating.