Check entity state using automation

Well, I thought this could fit into the template section

I want to check the state (and other attributes) of an entity by sending the request via mqtt as trigger to an automation rule. Like the sample code below

- action:
  - data_template:
      payload_template: '{{trigger.payload}}'
      topic: my/got_it
    service: mqtt.publish
  alias: 'Fetch device state based on payload'
  condition: []
  id: '1516171819203'
  trigger:
  - platform: mqtt
    topic: my/get_it

When I send ‘states.sun.sun.state’ as payload with mqtt to topic ‘my/get_it’ I expect to receive the evaluated result back in topic ‘my/got_it’

But I only receive the same string back. Any hint is very much appreciated

Best regards, Walter

So you’re using MQTT to send the entity_if that you want a value for? You’ll need to do some jinja manipulation on your payload then. For instance, pop this in your template dev tool
{% set eid = 'sun.sun' %}{{states(eid)}}

So this should work:
{{states(trigger.payload)}}

Just make sure that you’re passing the entity_id (sun.sun).

Thank you very, very much!!!
Works great,

And this gives me the various attributes as well
{% set eid = ‘sun.sun’ %}{{state_attr(eid, ‘azimuth’)}}

Kind regards, Walter