Data template with If statement not working

Hi,

I’m trying all the possible ways I can think of to get this to work but to no avail. Please show me what I’m doing wrong. I’m thinking it’s the formatting. I tried with Template Editor and the If statement worked okay. If I comment out the If statement, it works. Thanks.

  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if is_state("switch.camry", "off") %}
          - automation.tasker_away_stationary
          - automation.tasker_away_moving
        {% endif %}

don’t think that’s gonna work. Easiest would be to add a condition in your actions before your service call:

  - condition: state
    entity_id: switch.camry
    state: 'off'
  - service: homeassistant.turn_on
    entity_id:
      - automation.tasker_away_stationary
      - automation.tasker_away_moving

You can’t return a list with a template.

  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if is_state("switch.camry", "off") %}
           automation.tasker_away_stationary, automation.tasker_away_moving
        {% endif %}

Might work, if not you’ll need to put the two automations in a group and use the group entity_id in the template.

In addition to that, your template is going to error whenever that runs because you have no ‘else’ so whenever switch.camry is off the template can’t be resolved.

You should use a condition instead

- condition: state 
  entity_id: switch.camry 
  state: on
- service: homeassistant.turn_on
  entity_id:
    - automation.tasker_away_stationary
    - automation.tasker_away_moving

Ugh, not this again :smile:

Are you like one of my alternate personalities or something?

lol I call this plagiarism. :laughing:

In fairness, I was thinking that if I had a multiple personality disorder that I didn’t know about it would be weird for them to be 140 miles away, but then maybe the GPS is skewed by the awesome tinfoil hat we’re wearing :crazy_face:

@anon43302295 & @lolouk44, you guys are hilarious! :slight_smile:

I agree with using a condition, assuming there is no other step after that one that still would need to run in either case. But if there is, you could do something similar to mf_social’s first suggestion:

  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if is_state("switch.camry", "off") %}
          automation.tasker_away_stationary, automation.tasker_away_moving
        {% else %}
          automation.none
        {% endif %}

This works because 1) the entity_id parameter can take a string which is more than one entity_id separated by commas, and 2) most services, including automation.turn_on (which homeassistant.turn_on will call), accepts (and ignores) entity_id’s that don’t exist.

BTW, if you’re turning on an automation, you should probably just call automation.turn_on directly instead of homeassistant.turn_on.

Thank you all for the response and suggestions! I have another service call prior to the above service call. At first, I thought putting the condition in there would apply to all or maybe I put the condition not in the proper place when I first played with it awhile back, so the result wasn’t what I expected. Saw your replies so tried putting the condition just right above it and that worked!

I’m new to HA and still trying to understand templates. Haven’t had much time so I’ve been using Tasker to run a bunch of automations instead. Now, I’m trying to transfer most to HA.

I have another question and I may as well ask here instead of creating another thread as it’s related to what I’m trying to accomplish.

For running any automation every x minutes/, only can be done via trigger or there’s another way? Or maybe the whole thing can be done away altogether. Right now I have 4 automations that get triggered at intervals to report my location and battery status. This is WIP and I may not need all but just learning process. Trying to see if I can have all in one automation.

  1. At home, running every hour.
  2. Driving my camry, running every 5 mins.
  3. At some place, running every 30 mins.
  4. On the move not with camry, running every 10 mins.

Trigger the automation every 5 minutes, and use a template condition to define whether it actually does trigger based on that logic

trigger:
  platform: time 
  minutes: '/5'
condition:
  condition: template
  value_template: >
    {% (is_state(DRIVING_CAMRY)) or
       (is_state(YOU_HOME) and AUTOMATION_LAST_TRIGGERED > 1 HOUR) or
       (is_state(MOVING) and AUTOMATION_LAST_TRIGGERED > 10 MINUTES) or
       (is_state(YOU NOT_HOME) and AUTOMATION_LAST_TRIGGERED > 1/2 HOUR)  %}
action:
  - YOUR UNCONDITIONAL ACTION 
  - condition: state
    entity_id: switch.camry
    state: 'off'
  - service: homeassistant.turn_on
    entity_id:
      - automation.tasker_away_stationary
      - automation.tasker_away_moving

What are you using to determine your location and battery status? I ask because typically the HA component you’re using to detect that (e.g., a device_tracker or sensor) will be periodically polling already. So not quite sure why you need to run an automation periodically for this as well.

If you still have your feet halfway in Tasker and halfway in HA, ok, I can understand that. But ultimately you (probably) shouldn’t need polling automations for this.

@pnbruckner

I’m using Tasker exclusively for that and report back in Owntracks format. Main reason is that I found I have complete control of when it should be done and HA update pretty much without delay once it’s executed. I did use Owntracks app but inconsistent in term of updating my location. I added BT beacons but got disconnect issue as I kept getting enter/exit zones. I also tried Owntracks and HA apps on my iPad and not working as well as I like. I also use router for device tracking and not good enough.

I have a few automations that I rely on HA update to be quick, such as updating my presence as being home when I drive into the garage after dark with WiFi connected, and turn on living room lights if not already on when I open the door leading up to it. Had fair share of times when it didn’t execute because HA not yet update my home status and had to walk in the dark. Bit inconvenient with my baby daughter. I’ll play around more when I have time.

@anon43302295

Thank you very much. Right now I only have two virtual switches, home and driving, with the other two being set up to use proximity’s attributes in addition to not driving. My automations are either very simple or just scrap together, quite messy. Templates will take care of that problem, just that I don’t have good grasp of it right now. I guess I’ll make two more switches. Will give it a go this weekend. Thanks again.

1 Like