Get the mqtt sensor state in value temple

I publish this:

- alias: 'Uppdate alarm sensor'
  trigger:
    platform: state
    entity_id: sensor.alarm_last_update
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:  
    - service: mqtt.publish
      data:
        topic: "alarm/visonic/alarmpanel/active"
        payload_template: "{{ states.sensor.alarm_active }}"
        retain: true

and I recieve this:

template state sensor.alarm_active=True; friendly_name=Larm aktivt, icon=mdi:shield-lock @ 2019-06-02T15:23:45.094645+02:00>

with this sensor definition:

  - platform: mqtt
    name: "alarm_active2"
    state_topic: "alarm/visonic/alarmpanel/active"
    force_update: true

I cannot figure out how to extract the payload “True” in a value template, My best guess so far is this:

value_template: "{{ value_json.sensor.alarm_active }}"

but no… what I am doing wrong?

The following revised version of your automation will publish the state of sensor.alarm_active to the topic alarm/visonic/alarmpanel/active.

  • Replace data with data_template.
  • Replace payload_template with payload.
  • Change payload's template.
- alias: 'Uppdate alarm sensor'
  trigger:
    platform: state
    entity_id: sensor.alarm_last_update
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:  
    - service: mqtt.publish
      data_template:
        topic: "alarm/visonic/alarmpanel/active"
        payload: "{{ states('sensor.alarm_active') }}"
        retain: true

Seems to do the trick. Thanks!