How to use variables in automation / actions

Hi,
I’m a HASS newbee and learning something about automation.
Therefore I’d like to use the value of “last_changed” in a script, but it doesn’t work as expected.

{{ as_timestamp(states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed) }}

fine!
But

{% set entity = 'states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed' %}
{{ entity }}
{{ as_timestamp(entity) }}

doesn’t work.
ValueError: Template error: as_timestamp got invalid input ‘states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed’ when rendering template ‘{% set entity = ‘states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed’ %}
{{ entity }}
{{ as_timestamp(entity) }}’ but no default was specified

Can anybody explain me, how to use variables in this case?
Thanks!

Remove the single-quotes surrounding states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed

{% set entity = states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed %}
{{ entity }}
{{ as_timestamp(entity) }}

The quotes make it into a string instead of what you want it to do (get the value of the binary_sensor’s last_changed property).

OK, thanks! A simple but also very helpful tip!
But let’s go to advanced topics, which will cause another problem:

My var “entity” is set by a variable from a blueprint.

variables:
  sensor: !input contact_sensor
  entity: states.{{sensor}}.last_changed
...
...
action:
message: "Send this demo-message with {{ as_timestamp(entity) }}"

As far as I could see, this will be used as a string again and causing the following error:

Error: Error rendering data template: ValueError: Template error: as_timestamp got invalid input 'states.binary_sensor.hm_sci_3_fm_keq1234567_1_state.last_changed' when rendering template 'Send this demo-message with {{ as_timestamp(entity) }}' but no default was specified

Do you have any further idea?

See should work but only if the value of sensor is a string.

message: "Send this demo-message with {{ as_timestamp(states[sensor].last_changed) }}"

Thanks!!!
After trying many hours, this solution works for me!