     stop_id = if stop_id do stop_id else uuid end
 
     {:ok, %{
+      trigger: trigger,
       plugin_id: plugin_id,
       stop_id: stop_id,
     }}

    GenServer.cast(rule_id, {:trigger, params})
  end

  ## Server impl

  def init(rule) do
    Logger.info("Starting rule #{inspect rule}")
    uuid = rule["uuid"]

    {:ok, trigger} = start_trigger(uuid, rule["when"])

    Logger.debug("Got trigger #{inspect trigger}")


    end
  end

  @doc ~S"""
  Prepares the state to start executing acitons at next :continue mesage.

  It does not execute the actions straight away, as it blocks the process whicle
  executing the acitons. So there should be some checking of the messages just
  in case there is a special event, as stop.

  Also it must discard and log extra triggers while executing, or the message
  queue will force execution after execution.

  So it sets at :running the list of actions to perform. The first item will be
  next action and so on. If the aciton is a conditional, it will place the
  proper branch on top of the list of actions to execute.

  When the list is empty, all actions have been executed.

  For all this to work, the :continue message has to be inserted after every
  action is performed.

  """
  def handle_cast({:trigger, params}, state) do
    if state.running == false do
      Logger.debug("Trigger action: #{inspect state, pretty: true}")
      when_id = Map.get(state.rule.rule["when"], "id", "A")
      params = Map.merge( state.rule.rule["when"]["params"], params )
      uuid = state.rule.uuid
      trigger_state = Map.put(%{ "uuid" => uuid}, when_id, params)

      state = %{ state |
        running: state.rule.rule["actions"],
        state: trigger_state,
        }
      GenServer.cast(self(), :continue) # yields execution of actions.

      #Logger.info("Trigger! #{inspect params} #{inspect state}")
      #{:ok, rule_final_state} = execute_actions(uuid, state.rule.rule["actions"], trigger_state)
      #Logger.info("Final state: #{inspect rule_final_state}")

      {:noreply, state}
    else
      Logger.info("Ignoring retrigger of #{inspect state.uuid}. Already executing actions.", rule: state.rule)
      {:noreply, state}
    end
  end

  def handle_cast(:continue, %{ running: [] } = state) do
    # Logger.debug("Action chain finished. Final state is #{inspect state.state, pretty: true}")

    state = %{ state |
      running: false,
    }

    {:noreply, state}
  end
  def handle_cast(:continue, state ) do
    %{ running: [ action | rest], uuid: uuid, state: rule_state} = state
    # Logger.debug("#{inspect self()}: Continue executing actions: now: #{inspect action, pretty: true},\n   later: #{inspect rest, pretty: true}")


    { head, rule_state } = execute_action( uuid, action, rule_state)

    state = %{ state |
      running: head ++ rest,
      state: rule_state
    }
    # Logger.debug("Running queue is now: #{inspect state.running, pretty: true}")
    GenServer.cast(self(), :continue)

    {:noreply, state}
  end
  def execute_action(uuid, %{
      "type" => "action",
      "action" => action,
      "params" => params
      } = actiondef, state) do
    # Logger.info("Execute action: #{inspect action}(#{inspect params}) // #{inspect state}")
    result = Serverboards.Action.trigger_wait(action, params, "rule/#{uuid}")

    result = if Enum.count(result)==1 and Map.has_key?(result, :result) do
      result[:result] else result end

    action_id = Map.get(actiondef, "id")
    # Logger.info("Result #{inspect action_id} is #{inspect result}")
    state = if action_id do Map.put(state, action_id, result)
      else state end

    {[], state}
  end

  def execute_action(uuid, %{
      "type" => "condition",
