How to use trigger_to state and friendly name in input_select templates

Hi All,

I’ve been playing with this for a while now and can’t seem to figure it out, what I have so far is this…

- alias: Keith in which room
  - platform: state
    to: 'on'
      - binary_sensor.fibaro_system_fgms001_motion_sensor_sensor #bedroom
      - binary_sensor.motion_sensor_158d00015e8e04 #livingroom
      - binary_sensor.motion_sensor_158d0001e08d21 #kitchen
      - binary_sensor.motion_sensor_158d000125a67b #hall
      - binary_sensor.motion_sensor_158d0001de7250 #bathroom
    - condition: state
      entity_id: input_boolean.keithhome
      state: 'on'
  - service: input_select.select_option
    entity_id: input_select.keith_room_status
      option: >
        {% if   is_state('trigger.from_state.attributes.friendly_name', 'ZW Bedroom Eye Motion Detector') %}
        {% elif is_state('trigger.from_state.attributes.friendly_name', 'X_Bathroom_Motion') %}
        {% elif is_state('trigger.from_state.attributes.friendly_name', 'X_Hall_Motion') %}
        {% elif is_state('trigger.from_state.attributes.friendly_name', 'X_Livingroom_Motion') %}
        {% elif is_state('trigger.from_state.attributes.friendly_name', 'X_Kitchen_Motion') %}
        {% endif %}
  id: Keith in which room

No matter what I do to the template the input_select never changes, I know that it triggers OK as you can see it in history.

I’ve tried it like this is_state_attr(‘trigger.from_state’, ‘friendly_name’, ‘X_Kitchen_Motion’), but it never changes the input_select.

Can anyone shed some light on it please :slight_smile:

the trigger object is an object, not a string. When you wrap text in quotes you turn it into a string. Once that happens, only string things can be done with it.

So in these lines of code, you’re changing the trigger object into a string, basically making it useless.

The next thing is, you’re using is_state(). This function takes entity_id’s and entity_id’s only as the first argument. Is this an entity_id?

Lastly, you shouldn’t use from_state. It’s the previous state, you should be using to_state. This is the resulting state of the state change. There are cases when from_state does not exist and to_state does.

To recap:

  1. We can’t use is_state.
  2. We need to keep it a trigger object.
  3. We need to use to_state.

That would result in your if statements looking a bit like this:

{% if trigger.to_state.attributes.friendly_name == 'ZW Bedroom Eye Motion Detector' %}

So putting this all together:

        {% if trigger.to_state.attributes.friendly_name == 'ZW Bedroom Eye Motion Detector' %}
        {% elif trigger.to_state.attributes.friendly_name == 'X_Bathroom_Motion' %}
        {% elif trigger.to_state.attributes.friendly_name == 'X_Hall_Motion' %}
        {% elif trigger.to_state.attributes.friendly_name == 'X_Livingroom_Motion' %}
        {% elif trigger.to_state.attributes.friendly_name == 'X_Kitchen_Motion' %}
        {% endif %}

Hi Petro,

Thanks for the pointers, it now works :slight_smile:

However I still don’t understand why my first go with this :-

is_state_attr(‘trigger.to_state’, ‘friendly_name’, ‘X_Kitchen_Motion’)

didn’t work, as far as I can see from the docs the is_state/attr is what is recommended and from the docs the first two values are single quoted, e.g. is_state_attr(‘device_tracker.paulus’, ‘battery’, 40), now their example tests against a numeric so it isn’t quoted (the 40), since I’m testing against a string I quoted it. I guess the trigger.to_state doesn’t return the entity_id?

because the first argument that’s required is an entity_id. You’re feeding it 'trigger.to_state’ which is not an entity_id. trigger.to_state is a state object. Take a look at this post to help you understand what a state object is:

1 Like