Trying to template both the service and the entity in an automation

This does not compile when I try to reload the automations, the “check config” does not give any errors. The trigger and condition parts of the automation is a copy of another automation I have that is working, so I guess the error is in the service and entity templating.

Any ideas?

- alias: Test Automation
  trigger:
    - platform: state
      entity_id: input_boolean.test
  condition:
    - condition: template
      value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:
    - service: >
        {% if trigger.to_state.state == 'off' %} automation.trigger
        {% elif states.input_text.active_scene.state = 'scene.scene1' %} scene.turn_on
        {% else %}
        {% endif %}
      data_template:
        entity_id: > 
          {% if trigger.to_state.state == 'off' %} automation.generic_automation
          {% elif states.input_text.active_scene.state = 'scene.scene1' %} scene.scene2
          {% else %}
          {% endif %}

I think you are missing {% endif %} for both if elif else blocks.

Thanks, that could have been it, but sadly not.
Added the missing {% endif %} above.

This is the error message:
Invalid config for [automation]: does not match format . for dictionary value @ data[‘action’][0][‘service’]. Got None.

You aren’t providing an else, so the service is empty as well as the entity_id. Also data_template is deprecated and no longer needed. You can just use data.

Lastly, you should be using choose for this, not a service template and data template.

Thanks, never used chose before, it look cleaner.
Still get errors thought,

- alias: Test mode Automation
  trigger:
    - platform: state
      entity_id: input_boolean.test_mode
  action:
    - choose:
        - conditions: >
            {{ trigger.from_state.state != trigger.to_state.state and trigger.to_state.state == 'off' }}
          sequence:
            - service: automation.trigger
              target:
                entity_id: automation.tests
        - conditions: >
            {{ states.input_text.active_scene.state = 'scene.scene1' }}
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.scene2
      default:
        - service: script.noop

What are the errors.

also, if script.noop does nothing, don’t provide a default… it’s not needed.

ok, then I can remove default. thanks.

Error in log:

Logger: homeassistant.config
Source: config.py:464
First occurred: 13:18:23 (1 occurrences)
Last logged: 13:18:23

Invalid config for [automation]: Expected a dictionary @ data['action'][0]['choose'][1]['conditions'][0]. Got None. (See /config/configuration.yaml, line 152)

The reference to row 152 in config makles no sence. That line has nothing to do this this automation or anything that the autmation is using.

It’s most likely your multi-line template on your conditions. You’re using the shorthand condition ability with templates and I’m not sure if it allows for the multiline character. Either way, it’s complaining about the first condition. Try it as a single line template or write it out as a full condition.

ok, thanks

Hmmm… I managed to recreate the same error with full conditions:

- alias: Test mode Automation
  trigger:
    - platform: state
      entity_id: input_boolean.test_mode
  action:
    - choose:
        - conditions:
            condition: and
            conditions:
              - condition: template
                value_template: " {{ trigger.from_state.state != trigger.to_state.state }} "
              - condition: template
                value_template: " {{ trigger.to_state.state == 'off' }} "
          sequence:
            - service: automation.trigger
              target:
                entity_id: automation.tests
        - conditions: >
            {{ states.input_text.active_scene.state = 'scene.scene1' }}
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.scene2

then you have some other error in your automation file that’s causing the problem.

what’s at line 152, post 10 lines before and after 152 and indicate what line 152 is.

It is refering to the line 152 in configuration.yaml. On line 153 is the include for automations…

group: 
  sparsnas:
    name: Energy Consumption
#    view: no
    icon: mdi:lightning-bolt-circle
    entities:
      - sensor.sparsnas_energy_consumption_momentary
      - sensor.sparsnas_energy_consumption_over_time
      - sensor.sparsnas_battery_remaining
      - sensor.sparsnas_frequency_error
      - sensor.kwh_today
      - sensor.kwh_current_month              <--------------- Line 152
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#############################################################
#                Housekeeing functions                      #
#############################################################    
# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true
# Discover some devices automatically
discovery:
# Allows you to issue voice commands from the frontend in enabled browsers
conversation:
# Enables support for tracking state changes over time
history:
logbook:

I just noticed you’re still using the multiline template in the second condition…

Just try this.

- alias: Test mode Automation
  trigger:
    - platform: state
      entity_id: input_boolean.test_mode
  action:
    - choose:
        - conditions: "{{ trigger.from_state.state != trigger.to_state.state and trigger.to_state.state == 'off' }}"
          sequence:
            - service: automation.trigger
              target:
                entity_id: automation.tests
        - conditions: "{{ states.input_text.active_scene.state == 'scene.scene1' }}"
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.scene2

Actually after copying and pasting it myself I noticed your error.

That’s incorrect. You need 2 equal signs.

{{ states.input_text.active_scene.state == 'scene.scene1' }}

Ahh, wow…thanks… I have probably rewritten all code in this automation 3 times, but probbably copied this part… thanks alot !!!