Send multiple mqtt messages depending of a sensor state

Hi,
I have an automation that is triggered when a mqtt message arrives from my solar inverter.
If its the first massage of the day it sets sensor.solar_first_update with current time.
It then sends off another mqtt message with current time for recording the last solar mqtt message.
Both sensor.solar_first_update and sensor.solar_last_update are set back to zero at midnight.

The first and last message is used to record when the solar started and finished producing power for the day, and written to a influxDB.

It also sends the data (json.Data array) I want from the inverter json array in another mqtt message (solar/output) to update the sensors with power production etc.

Below is what I have now, which works, but is there a better way to do it.
Is it possible to use a template that can then do more that one action/service eg:

if first message
   send mqtt message to set first update time
   send mqtt message to reset all solar sensor to zero
else
   send mqtt message to set last update time
   send mqtt message to set all solar sensor to current production values
end if

I use the the mqtt topic solar/blank because I couldn’t workout how to not send a mqtt message if it was not the first message of day.

CURRENT AUTOMATION:

  - alias: solar_data_republish
    trigger:
    - platform: mqtt
      topic: "loc/SWP876JHY"
    action:
      - service: mqtt.publish
        data_template:
          topic: >
            {% if states.sensor.solar_first_update.state == "0" %}
              solar/first_message
            {% else %}
              solar/blank
            {% endif %}
          payload: >
            {% if states.sensor.solar_first_update.state == "0" %}
              {"Data":"{{ now().strftime("%H:%M") }}"}
            {% else %}
              ''
            {% endif %}
          retain: true
      - service: mqtt.publish
        data:
          topic: "solar/last_message"
          retain: true
          payload: >
            {"Data":"{{ now().strftime("%H:%M") }}"}
      - service: mqtt.publish
        data_template:
          topic: "solar/output"
          retain: true
          payload: >
            {"Data":{{ trigger.payload_json.Data }} }

I have tried CONDITIONS and SERVICE_TEMPLATE but couldn’t get them to work.

Have you tried using the choose action?

Also please see the warning box here.

@tom thanks for the help.

I have changed the way I access the states.

I have also tried the choose action, this is what I have come up with, is this ok or is there a better way ?

    action:
      - choose:
          # IF first message
          - conditions:
              - condition: template
                value_template: "{{ states('sensor.solar_first_update') == '0' }}"
            sequence:
              - service: mqtt.publish
                data:
                  topic: "solar/first_message"
                  payload: >
                     {"Data":"{{ now().strftime('%H:%M') }}"}
              - service: mqtt.publish
                data:
                  topic: "solar/data_reset"
                  payload: " "
              - service: mqtt.publish
                data:
                  topic: "solar/last_message"
                  payload: >
                    {"Data":"{{ now().strftime("%H:%M") }}"}
                  retain: true
              - service: mqtt.publish
                data_template:
                  topic: "solar/output"
                  payload: >
                    {"Data":{{ trigger.payload_json.Data }} }
                  retain: true
        default:
          - service: mqtt.publish
            data:
              topic: "solar/last_message"
              retain: true
              payload: >
                {"Data":"{{ now().strftime("%H:%M") }}"}
          - service: mqtt.publish
            data_template:
              topic: "solar/output"
              retain: true
              payload: >
                {"Data":{{ trigger.payload_json.Data }} }

I wont be able to tell if it works until it cycles through 24 hours.

The above action choose is working.

Thanks for your help tom