Create Variable from part of sensor data

Hi!

I have a use case where the sensor is text generated from an Android notification, the first word in the text needs to be included in the output text from the automation.

For example, the android notification reads aaaaaa has entered the building. I need to take the part ‘aaaaaa’ and output it to Google TTS - something like ‘aaaaaa is home!’

I have the automation working already without the name part - it just says what I’ve typed in the box - ‘someone is home!’ for now.

I’ve created a input_text helper variable called last_person and tried to update that in the automation using Call a service: input_text Set’ using the last_person as the target. If I write something in the box e.g ‘Dave’ then the helper is updated with the word I type in it, so that is ok… but I need to just get the first word of sensor_xxx_android_last_notification’

I’ve tried:

sensor.xxx_android_last_notification[0].split(' ')
sensor.xxx_android_last_notification - just to see if it copied the text and it didn't
states('sensor.xxx_android_last_notification[0].split(' ')')
states('sensor.xxx_android_last_notification') - just to see if it copied the text and it didn't

I feel like I’m very close but not quite there. Any help appreciated!

Use the template editor under Developer Tools and the documentation, rather than guessing.

You want:

states('sensor.xxx_android_last_notification').split(' ')[0]

Explanation: states() returns the state of an entity if given the entity ID. So states('sensor.xxx_android_last_notification') would be the string “aaaaa has entered the building”.

You then run split(' ') on it to split it at each space, outputting a list of strings: ['aaaaa', 'has', 'entered', 'the', 'building']. You want the first of those, which is indexed using [0].

From your description of what’s happening, I suspect you have automation issues also. If you need help with that, please copy the full YAML automation code here, correctly formatted with the </> button. It should look like this:

id: 'my amazing automation'
trigger:
  - platform: state

and so on.

Wow, thank you for the pointer to the developer tools! With your rewrite of my variable method and the use of the developer tools I have managed to write my complete automation and tested it OK.

My only issue is trying to find a way of getting this information without relying on sending the Android notification to HA but that’s an entirely different problem altogether, and the developer of the app doesn’t really have another way of doing things.