this post was submitted on 28 Sep 2023
20 points (95.5% liked)

homeassistant

12080 readers
4 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 1 year ago
MODERATORS
 

I'm trying to get a script built. I want to check if a variable exist and include it if it does. Just really struggling to figure out the formatting. Something like

script:
  sequence:
    target:
      entity_id: "{{ entity }}"
    {% if variable is defined %}
      data: "{{ variable }}"
    {% endif %}
you are viewing a single comment's thread
view the rest of the comments
[–] Otkaz@lemmy.world 2 points 1 year ago* (last edited 1 year ago) (1 children)

To give more context I'm working on a media control dashboard. The script or rather scripts I have to send commands to kodi is as follows

kodi_control:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data:
      method: '{{ kodi_method }}'

kodi_control_playback:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data:
      method: '{{ kodi_method }}'
      playerid: '{{ kodi_playerid }}'

kodi_control_subtitles:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data:
      method: '{{ kodi_method }}'
      action: '{{ kodi_action }}'

kodi_control_seek:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data:
      method: '{{ kodi_method }}'
      playerid: '{{ kodi_playerid }}'
      value: '{{ kodi_value }}'

kodi_control_playlist:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data:
      method: '{{ kodi_method }}'
      window: '{{ kodi_window }}'
      parameters: '{{ [ kodi_parameters ] }}'

I would like to condense all of this down to a single script using "is defined" to omit the parts not needed for certain commands so something like

kodi_control:
  sequence:
  - service: kodi.call_method
    target:
      entity_id: '{{ kodi_entity }}'
    data: >-
      method: '{{ kodi_method }}'
      {% if kodi_playerid is defined %}
        playerid: '{{ kodi_playerid }}'
      {% endif %}
      {% if kodi_action is defined %}
        action: '{{ kodi_action }}'
      {% endif %}
      {% if kodi_value is defined %}
        value: '{{ kodi_value }}'
      {% endif %}
      {% if kodi_window is defined %}
        window: '{{ kodi_window }}'
      {% endif %}
      {% if kodi_parameters is defined %}
        parameters: '{{ [ kodi_parameters ] }}'
      {% endif %}

Problem with the above is I get "result is not a dictionary"