Reduce code in automations / service_template

Hi all
I have an IR Remote wich I use to control a lot of stuff in my living room.
I control sevaral devices with the same remote. As a lot of buttons are used on each device i had to find a way to separate.

I use the ‘var’ hacs-addon to do this.

When i push special buttons on my remote the variable “var.situation” is filled with the string “tv”, “music” or “kodi”, so i know which device has to be controlled.
I also use a second homeassistant instance (venv), wich i only use to gather the events from my remote, wich are processed by lirc and sent to my master homeassistant instance (hassos) through Remote-Homeassistant.

The last keypress ist stored in “var.button_pressed”

Then my automations are working as the following…

- alias: KODI Back on Remote
  trigger:
    platform: state
    entity_id: var.button_pressed
    to: "158"
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: 'var.situation'
        state: 'kodi'
      - condition: state
        entity_id: 'var.situation'
        state: 'music'
  action:
    service: shell_command.kodi_input_back

This works as excepted but the problem is, i have hundreds of them.
So im looking for a nicer, not so code intense/repeating solution.

I tried the following approach but can’t get it to work.

- alias: Denonavr Volume
  trigger:
    - platform: numeric_state
      entity_id: var.button_pressed
      above: "112"
      below: "116" 
  action:
    service_template: >
      {% if trigger.to_state.state == '115') %} switch.toggle
      {% else %} script.turn_on
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.state == "113" %} switch.denonavr_mute
        {% elif trigger.state == "114" %} script.wohnzimmer_volume_up
        {% elif trigger.state =="115" %} script.wohnzimmer_volume_down
        {% endif %}

I allways get

does not match format <domain>.<name> for dictionary value @ data['action'][0]['service_template']. Got None"

Maybe somebody can help me out with a working example, or lead me to a better way doing this.

Thank you

I don’t think numeric_state has a to_state, try this…

- alias: Denonavr Volume
  trigger:
    - platform: state
      entity_id: var.button_pressed
      to:
        - "113"
        - "114"
        - "115"
  action:
    service: >
      {% if trigger.to_state.state == '115' %} switch.toggle
      {% else %} script.turn_on {% endif %}
    data:
      entity_id: >
        {% if trigger.to_state.state == "113" %} switch.denonavr_mute
        {% elif trigger.to_state.state == "114" %} script.wohnzimmer_volume_up
        {% elif trigger.to_state.state =="115" %} script.wohnzimmer_volume_down
        {% endif %}

Also note that _template is deprecated.

Thanks for the quick answer, but still getting the same error.

Invalid config for [automation]: Service {% if trigger.to_state.state == '115') %} switch.toggle {% else %} script.turn_on {% endif %} does not match format <domain>.<name> for dictionary value @ data['action'][0]['service']. Got None.

Also note that _template is deprecated.

Ok. Thank you.

I had a random close bracket in there for some reason - removed it from my post above - try that.

It works. Thanks a lot.