IF statement to assign a variable in automation

HI,

I’m using this in an automation to send a message when there is an update available

outdated_items: |-
    {{ states. Update
        | selectattr('state', 'eq', 'on')
        | selectattr('attributes.auto_update', 'eq', false)
        | selectattr('attributes.in_progress', 'eq', false)
        | sort(attribute='attributes.installed_version')
        | map(attribute='entity_id')
        | list }}
  next_to_update: "{{ outdated_items | last }}"

it works great but, when the update is related to shelly or esp the Title attribute is set to None

state update.shellyplug_s_f18d2c_firmware_update=on; 
auto_update=False, 
installed_version=20230503-101129/v1.13.0-g9aed950, 
in_progress=False,
latest_version=20230913-113421/v1.14.0-gcb84623, 
release_summary=None, 
release_url=None, 
skipped_version=None, 
**title=None,** 
device_class=firmware, 
entity_picture=https://brands.home-assistant.io/_/shelly/icon.png, 
friendly_name=shellyplug-s-F18D2C firmware update, 
supported_features=5 @ 2023-10-15T12:29:26.496877+02:00

when I use to send a notification the message shows None

service: notify.mobile_app_phone
data:
  title: Start the update for {{state_attr(next_to_update,'title')}}
  message: New version {{state_attr(next_to_update,'latest_version')}}
  data:
    clickAction: "{{state_attr(next_to_update,'release_url')}}"

the workaround for now is set another automation, add a filter for firmware

| selectattr('attributes.device_class', 'match', 'firmware')

and use {{next_to_update}} instead of {{state_attr(next_to_update,‘title’)}}

  title: Start the update for {{next_to_update}}

I’m wondering if I can add a condition to evaluate if the Title value is None and set a variable, something like

condition: template
value_template:  >-
          {% if state_attr(next_to_update,'title') =='None' %}
            NEW_VARIABLE = "Something"
          {% else %}
            NEW_VARIABLE = {{state_attr(next_to_update,'title')}}
          {% endif %}

then use it in the title

service: notify.mobile_app_phone
data:
  title: Start the update for NEW_VARIABLE
  message: New version {{state_attr(next_to_update,'latest_version')}}

any ideas?

Use this for the variables:

outdated_items: |-
    {{ states.update
        | selectattr('state', 'eq', 'on')
        | selectattr('attributes.auto_update', 'eq', false)
        | selectattr('attributes.in_progress', 'eq', false)
        | sort(attribute='attributes.installed_version')
        | map(attribute='entity_id')
        | list }}
  next_to_update: "{{ outdated_items | last }}"
  title: "{{ state_attr(next_to_update, 'title') | default('Something', true) }}'

Then use it like this

service: notify.mobile_app_phone
data:
  title: Start the update for {{ title }}
  message: New version {{ state_attr(next_to_update,'latest_version') }}
  data:
    clickAction: "{{ state_attr(next_to_update,'release_url') }}"

Thank you @TheFes , can you point me to the documentation the understand the logic and the syntax?
I’m trying this, but I get an error

  title: >-
    {{ state_attr(next_to_update, 'title') | default( {{next_to_update}}, true)
    }}

You are nesting templates. Remove the curly brackets around next_to_update

there is something wrong, I get this error now

Error: Error rendering data template: TypeError: state_attr() missing 1 required positional argument: 'name'

it can be the title: None is not available when I try to evaluate it? can be that None is not a string/text value?

What do you have now?

Error: Error rendering data template: TypeError: state_attr() missing 1 required positional argument: ‘name’

I meant the template.
This should work

title: >-
    {{ state_attr(next_to_update, 'title') | default( next_to_update, true) }}

Yep, copy that

alias: Set up variables for the actions
variables:
  action_yes: "{{ \"YES_\" ~ context.id }}"
  action_no: "{{ \"NO_\" ~ context.id }}"
  outdated_items: |-
    {{ states.update
        | selectattr('state', 'eq', 'on')
        | selectattr('attributes.auto_update', 'eq', false)
        | selectattr('attributes.in_progress', 'eq', false)
        | sort(attribute='attributes.installed_version')
        | map(attribute='entity_id')
        | list }}
  next_to_update: "{{ outdated_items | last }}"
  title: "{{ state_attr(next_to_update, 'title') | default( next_to_update, true) }}"

then I can use

service: notify.mobile_app_phone
data:
  message: >-
    Do you want to update\n{{ title }}?\n\n The actual version is
    {{state_attr(next_to_update,'installed_version')}}\n The new version is
    {{state_attr(next_to_update,'latest_version')}}
  data:
    actions:
      - action: "{{action_yes}}"
        title: "Yes"
      - action: "{{action_no}}"
        title: "No"

thank you so much

1 Like